Human instance segmentation API reference
This topic describes the input and output parameters of the person instance segmentation model. Person instance segmentation is a technique that detects different objects in an image and generates a precise, pixel-level mask to define the border of each object instance.
Related guide: Human instance segmentation
Important
- This document applies only to the China (Beijing) region. You must use an API key from this region.
- The image-instance-segmentation model is currently available for a free trial only. After the free quota is used up, you can no longer call the model. Paid usage is not supported.
Model overview
| Model name | Free quota(View) | Billing unit price | Throttling (including Alibaba Cloud accounts and RAM users) | |
| QPS limit for the task submission API | Number of tasks in simultaneous processing | |||
image-instance-segmentation | 500 images | For free trial only.
| 2 | 1 |
Prerequisites
Obtain an API key and export the API key as an environment variable.
HTTP call
To reduce waiting times and avoid request timeouts, this service is provided in asynchronous mode. You must make two requests:
- Create a task: First, send a request to create a human instance segmentation task. This request returns a task ID.
- Query the result using the task ID: Use the task ID that you retrieved in the previous step to query the result generated by the model.
Step 1: Create a task to obtain a task ID
POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis
Replace {WorkspaceId} with your actual workspace ID.
Request headersContent-Type string Required The content type of the request. The value must be Authorization string Required Use your Alibaba Cloud Model Studio API key. You can also use a DashScope API key. For example, Bearer d1xxx2a. X-DashScope-Async string Required Specifies whether to enable asynchronous processing. Set this parameter to Request bodymodel string Required The model to call. input object Required The basic information of the input image, such as the image URL. | Human instance segmentation |
Responseoutput object The output information of the task. code string The error code for the API call. This parameter is not returned if the request is successful. message string The error message for the API call. This parameter is not returned if the request is successful. request_id string (Optional) The unique ID for this request. | |
Step 2: Query the result based on the task ID
GET https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks/{task_id}
Replace {WorkspaceId} with your actual workspace ID.
Request headersAuthorization string Required The API key. Example: Bearer d1**2a. URL path parameterstask_id string Required The task ID. | Obtain task result |
Responserequest_id string The unique ID for this request. status_code int A value of 200 (HTTPStatus.OK) indicates that the request is successful. Other values indicate that the request failed. If the request fails, you can retrieve the error code from the code field and the error details from the message field. code string If an error code is returned, see the error code table. message string If an operation fails, the content includes failure details. output object The output information of the task. usage object Output statistics. | Task executed successfullyFor this model, the status of a completed task is retained for 24 hours, during which you can query it at any time. After 24 hours, the task and its results are deleted from the system. The task result is an image URL. For security reasons, the URL is valid for only 24 hours. After you retrieve the task result, you must use or save the image. Task in progressTask failed |
Appendix: How to separate masks for preserved and erased people
The following code example shows how to split the resulting human instance segmentation image into an reserved area image and a removed area image. You can then call an image erasing model to remove the unwanted person.
Original image | Human instance segmentation result | Reserved area | Removed area |
|---|---|---|---|
|
mask.png |
reserve.png |
remove.png |
import cv2
import numpy as np
def get_remove_mask(mask_path, remove_mask_path, reserve_ids=[]):
mask = cv2.imread(mask_path)
instance_num = int(mask.max())
new_mask = np.zeros((mask.shape[0], mask.shape[1], 3))
for i in range(1, instance_num+1):
if i in reserve_ids:
continue
new_mask[mask[:, :, 0] == i] = (255, 255, 255)
cv2.imwrite(remove_mask_path, new_mask.astype(np.uint8))
def get_reserve_mask(mask_path, reserve_mask_path, reserve_ids=[]):
mask = cv2.imread(mask_path)
instance_num = int(mask.max())
new_mask = np.zeros((mask.shape[0], mask.shape[1], 3))
for i in range(1, instance_num + 1):
if i in reserve_ids:
new_mask[mask[:,:,0] == i] = (255, 255, 255)
cv2.imwrite(reserve_mask_path, new_mask.astype(np.uint8))
if __name__ == '__main__':
mask_path = 'mask.png'
remove_mask_path = 'remove.png'
reserve_mask_path = 'reserve.png'
get_remove_mask(mask_path, remove_mask_path, reserve_ids=[1])
get_reserve_mask(mask_path, reserve_mask_path, reserve_ids=[1])
Error codes
If the model call fails and returns an error message, see Error codes for resolution.
This API also has the following specific status codes.
HTTP status code | Error code (code) | Error message (message) | Description |
|---|---|---|---|
400 | InvalidParameter.JsonPhrase | input json error | Invalid JSON input. |
400 | InvalidParameter.FileDownload | oss download error | Failed to download the input image. |
400 | InvalidParameter.ImageFormat | read image error | Failed to read the image. |
400 | InvalidParameter.ImageContent | The image content does not comply with green network verification. | The image content is not compliant. |
400 | InvalidParameter | the parameters must conform to the specification: xxx | An input parameter is out of range. |
500 | InternalError.Algo | algorithm process error | An algorithm error occurred. |
500 | InternalError.FileUpload | oss upload error | The file failed to upload. |



