All Products
Search
Document Center

Object Storage Service:Human detection

Last Updated:Jan 10, 2025

You can use the human body detection feature to detect the position of human bodies and obtain the confidence score. This topic describes the parameters that you can configure for this feature and provides examples of how to use this feature.

Scenarios

  • Pedestrian detection: Locate pedestrians in images using the human body detection feature.

  • Path tracking: Analyze the behavior of pedestrians using the human body detection and path tracking features.

  • Human body counting: Count the number of people in images using the human body detection feature for purposes such as pedestrian volume analysis and safety assurance.

Prerequisite

An IMM Project is mapped to your bucket.

For more information about how to map an IMM Project to your bucket by using the OSS console, see Quick Start. For more information about how to map an IMM Project to your bucket by calling an API operation, see AttachOSSBucket.

Usage notes

  • Human body detection supports only synchronous processing by calling the x-oss-process operation.

  • Anonymous access will be denied.

  • You must have the required permissions to use the feature. For more information, see permissions.

Parameters

Name of the operation: image/bodies

Parameter in the request

Parameter

Type

Required

Description

Example

sen

int

No

The sensitivity of the detection results. The higher the sensitivity, the more detected human bodies. Valid values: 0 to 100. Default value: 60.

50

Parameters in the response

Note

For more information about the parameters in the response, see DetectImageBodies - Detect human bodies in images.

Examples

Use OSS SDKs

The following code provides examples of human body detection by using OSS SDKs for common programming languages. Please configure the parameters based on your business requirements.

Java

OSS SDK for Java 3.17.4 or later is required.

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyuncs.exceptions.ClientException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Demo {
    public static void main(String[] args) throws ClientException, ClientException {
        // Specify the endpoint of the region in which the bucket is located.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou.
        String region = "cn-hangzhou";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the bucket.
        String bucketName = "examplebucket";
        // If this image is stored in the root directory of the bucket, enter the name of the image. If the image is not stored in the root directory of the bucket, specify the full path to the image. Example: exampledir/example.jpg.
        String key = "example.jpg";

        // Create an OSSClient instance.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Create a processing instruction for human body detection.
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
            getObjectRequest.setProcess("image/bodies");

            // Use the process parameter of the getObject method to pass the processing instruction.
            OSSObject ossObject = ossClient.getObject(getObjectRequest);

            // Read and display the query results.
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = ossObject.getObjectContent().read(buffer)) != -1) {
                baos.write(buffer, 0, bytesRead);
            }
            String imageBodies = baos.toString("UTF-8");
            System.out.println("Image Bodies:");
            System.out.println(imageBodies);
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            // Shut down the OSSClient instance.
            ossClient.shutdown();
        }
    }
}

Python

OSS SDK for Python 2.18.4 or later is required.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the ID of the Alibaba Cloud region in which the bucket is located.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

# If this image is stored in the root directory of the bucket, enter the name of the image. If the image is not stored in the root directory of the bucket, specify the full path to the image. Example: exampledir/example.jpg.
key = 'example.jpg'

# Create a processing instruction for human body detection.
process = 'image/bodies'

try:
    # Use the process parameter of the getObject method to pass the processing instruction.
    result = bucket.get_object(key, process=process)

    # Read and display the query results.
    image_bodies = result.read().decode('utf-8')
    print("Image Bodies:")
    print(image_bodies)
except oss2.exceptions.OssError as e:
    print("Error:", e)

Go

OSS SDK for Go 3.0.2 or later is required.

package main

import (
	"fmt"
	"io"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain the temporary access credentials from the environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Create an OSSClient instance.
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
	// Specify the ID of the Alibaba Cloud region in which the bucket is located. Example: cn-hangzhou.
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the bucket.
	bucketName := "examplebucket"

	bucket, err := client.Bucket(bucketName)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// If this image is stored in the root directory of the bucket, enter the name of the image. If the image is not stored in the root directory of the bucket, specify the full path to the image. Example: exampledir/example.jpg.
	// Create a processing instruction for human body detection by using the oss.Process method.
	body, err := bucket.GetObject("example.jpg", oss.Process("image/bodies"))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	defer body.Close()

	data, err := io.ReadAll(body)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("data:", string(data))
}

PHP

OSS SDK for PHP 2.7.0 or later is required.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;

try {
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
    $provider = new EnvironmentVariableCredentialsProvider(); 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    $endpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
    // Specify the name of the bucket.
    $bucket = 'examplebucket';
    // If this image is stored in the root directory of the bucket, enter the name of the image. If the image is not stored in the root directory of the bucket, specify the full path to the image. Example: exampledir/example.jpg.
    $key = 'example.jpg'; 

    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,        
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        // Specify the ID of the Alibaba Cloud region in which the bucket is located. Example: cn-hangzhou.
        "region" => "cn-hangzhou"
    );
    $ossClient = new OssClient($config);
  // Create a processing instruction for human body detection.
  $options[$ossClient::OSS_PROCESS] = "image/bodies";
  $result = $ossClient->getObject($bucket,$key,$options);
  var_dump($result);
} catch (OssException $e) {
  printf($e->getMessage() . "\n");
  return;
}

Use RESTful APIs

If your business requires a high level of customization, you can directly call RESTful APIs. To directly call an API, you need to include the signature calculation in your code. For more information about how to calculate the Authorization header, see (Recommended) Include a V4 signature in the Authorization header.

You can process images by adding the x-oss-process parameter to the GetObject operation. For more information, see GetObject.

Example 1

Sample request

GET /example.jpg?x-oss-process=image/bodies HTTP/1.1
Host: image-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 21 Jul 2023 08:49:07 GMT
Authorization: SignatureValue

Sample response

HTTP/1.1 200 OK
Server: AliyunOSS
Date: Fri, 21 Jul 2023 08:49:09 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 145
Connection: keep-alive
x-oss-request-id: 64BA4685A645AE37313CBD14
ETag: "2CE2EA370531B7CC1D23BE6015CF5DA5"
Last-Modified: Mon, 10 Jul 2023 13:07:30 GMT
x-oss-object-type: Normal
x-oss-hash-crc64ecma: 13420962247653419692
x-oss-storage-class: Standard
x-oss-ec: 0048-00000104
Content-Disposition: attachment
x-oss-force-download: true
x-oss-server-time: 329

{
  "RequestId": "58C0F6A4-7B26-5E22-9C60-EFF90E2082ED",
  "Bodies": [
    {
       "Confidence": 0.733,
       "Boundary": {
         "Left": 115,
         "Top": 97,
         "Height": 103,
         "Width": 63
         }
     }
   ]
}

Example 2

Sample request

In this example, the value of the sen parameter is 50.

GET /example.jpg?x-oss-process=image/bodies,sen_50 HTTP/1.1
Host: image-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 21 Jul 2023 08:54:54 GMT
Authorization: SignatureValue

Sample response

HTTP/1.1 200 OK
Server: AliyunOSS
Date: Fri, 21 Jul 2023 08:54:56 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 145
Connection: keep-alive
x-oss-request-id: 64BA47E05B40CC33396D6AA3
ETag: "2CE2EA370531B7CC1D23BE6015CF5DA5"
Last-Modified: Mon, 10 Jul 2023 13:07:30 GMT
x-oss-object-type: Normal
x-oss-hash-crc64ecma: 13420962247653419692
x-oss-storage-class: Standard
x-oss-ec: 0048-00000104
Content-Disposition: attachment
x-oss-force-download: true
x-oss-server-time: 394

{
  "RequestId": "E9BDE106-1436-5077-9F6E-65D81DC6FEC8",
  "Bodies": [
    {
      "Confidence": 0.733,
      "Boundary": {
        "Left": 115,
        "Top": 97,
        "Height": 103,
        "Width": 63
      }
    },
    {
      "Confidence": 0.732,
      "Boundary": {
        "Left": 214,
        "Top": 121,
        "Height": 86,
        "Width": 63
      }
    },
    {
      "Confidence": 0.656,
      "Boundary": {
        "Left": 63,
        "Top": 102,
        "Height": 100,
        "Width": 82
      }
    }
  ]
}