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.
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.
The API endpoint URL is:
https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg
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. |
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. |
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';
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:
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.
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.