Current Location: Home> Latest Articles> PHP Tutorial: Complete Guide to Integrate Baidu Image Segmentation API Quickly

PHP Tutorial: Complete Guide to Integrate Baidu Image Segmentation API Quickly

gitbox 2025-06-07

1. Introduction

Deep learning has become a popular research field in recent years, with image segmentation as one of its key applications. By leveraging Baidu’s image segmentation API, developers can simplify the implementation process and quickly experience and learn image segmentation technology.

This article will show how to use PHP to call Baidu’s image segmentation API to automatically segment image targets. It assumes readers have a basic knowledge of PHP and HTTP requests.

2. Overview of Baidu Image Segmentation API

The Baidu image segmentation API is a service that provides intelligent image segmentation. After submitting an image, the API automatically separates different targets in the image and returns corresponding mask data, supporting multiple output formats such as PNG and JSON.

2.1 API Endpoint

The API endpoint URL is:

https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg

2.2 API Parameters

Parameter Type Required Description
access_token string Yes Access token obtained via OAuth 2.0 authorization.
image string Yes Base64 encoded string of the image. Supports PNG, JPEG, BMP formats, size limit 4MB.
type string No Format of the returned result. Options: foreground (default), background, score.
threshold float No Segmentation threshold between 0 and 1 indicating the proportion of the cut-out area. Default is 0.5.

2.3 API Response Format

Field Type Description
foreground string Base64 encoded image of the segmented foreground target.
background string Base64 encoded image of the background area.
score float Confidence score of the segmentation result.

3. Calling Baidu Image Segmentation API with PHP

3.1 Getting the Access Token

Before calling the API, you need to obtain an access token from Baidu Developer Platform. Please refer to the official Baidu documentation for detailed steps.

$access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

3.2 Example of Sending HTTP Request

Use PHP’s built-in cURL extension to send a POST request to Baidu’s image segmentation API and receive the JSON response.

$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg';
$image = '/path/to/image.jpg';
$type = 'foreground';
$threshold = 0.5;
<p>$data = array(<br>
'access_token' => $access_token,<br>
'image' => base64_encode(file_get_contents($image)),<br>
'type' => $type,<br>
'threshold' => $threshold,<br>
);</p>
<p>$options = array(<br>
CURLOPT_RETURNTRANSFER => true,<br>
CURLOPT_POST => true,<br>
CURLOPT_POSTFIELDS => $data,<br>
);</p>
<p>$ch = curl_init($url);<br>
curl_setopt_array($ch, $options);<br>
$result = curl_exec($ch);<br>
curl_close($ch);<br>

Explanation:

  • Reads the local image file and encodes it to Base64 for the API request.
  • Sends a POST request via cURL and stores the JSON response in the $result variable.
  • You can decode the response using json_decode() to extract specific fields.

3.3 Parsing the Response and Saving the Image

The JSON response contains the field 'foreground' representing the segmented target image in Base64. Decode it and save it as a local image file.

$result_arr = json_decode($result, true);
if (isset($result_arr['foreground'])) {
    $base64_image = $result_arr['foreground'];
    $image_data = base64_decode($base64_image);
    file_put_contents('/path/to/foreground.png', $image_data);
}

If you want to get the background or confidence score, set the type parameter to 'background' or 'score' respectively and handle those fields accordingly.

4. Conclusion

This article explained how to quickly integrate Baidu’s image segmentation API using PHP, detailed the parameters, request flow, and response parsing, helping developers implement image segmentation features easily. You can extend the code to suit more image processing scenarios as needed.