You can use the vehicle detection feature to detect vehicles and license plates in images. This topic describes the parameters and provides examples for vehicle detection.
Use cases
Traffic management: Use vehicle detection in traffic monitoring and management systems. For example, you can identify vehicles in photos of traffic violations to process the infractions.
Abnormal vehicle investigation: Detect and identify vehicle and license plate information in images uploaded to an OSS bucket.
NoteVehicle information includes the vehicle location, color, and type. License plate information includes the license plate location and text content.
Traffic analysis: Analyze road usage, traffic flow distribution, and other patterns.
How to use
Prerequisites
You have activated IMM.
You have created a bucket in OSS and uploaded the files that you want to process to the bucket.
You have created and attached an IMM project. You can attach a project in the OSS console or by calling the AttachOSSBucket API operation. The IMM project must be in the same region as the bucket.
You have granted the required permissions.
Detect vehicles
The following code provides examples of how to use various SDKs to detect vehicles by specifying processing parameters. If you need to use a different SDK, you can adapt the code based on these examples.
Java
Version 3.17.4 or later of the OSS SDK for Java 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 Demo1 {
public static void main(String[] args) throws ClientException, ClientException {
// Specify the endpoint of the region where your bucket is located.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the region ID that corresponds to the endpoint, for example, cn-hangzhou.
String region = "cn-hangzhou";
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the bucket name.
String bucketName = "examplebucket";
// If the image is in the root directory of the bucket, specify the image name. If it is in a subdirectory, specify the full path, for example, exampledir/example.jpg.
String key = "example.jpg";
// Create an OSSClient instance.
// Call the shutdown() method to release resources when the OSSClient instance is no longer needed.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Build the processing instruction for vehicle detection.
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
getObjectRequest.setProcess("image/cars");
// Use the getObject method and pass the processing instruction by using the process parameter.
OSSObject ossObject = ossClient.getObject(getObjectRequest);
// Read and print the information.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = ossObject.getObjectContent().read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
String imageCars = baos.toString("UTF-8");
System.out.println("Image Cars:");
System.out.println(imageCars);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
// Close the OSSClient instance.
ossClient.shutdown();
}
}
}Python
Version 2.18.4 or later of the OSS SDK for Python is required.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where your bucket is located. For the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the region ID.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)
# If the image is in the root directory of the bucket, specify the image name. If it is in a subdirectory, specify the full path, for example, exampledir/example.jpg.
key = 'example.jpg'
# Build the processing instruction for vehicle detection.
process = 'image/cars'
try:
# Use the get_object method and pass the processing instruction by using the process parameter.
result = bucket.get_object(key, process=process)
# Read and print the information.
image_cars = result.read().decode('utf-8')
print("Image Cars:")
print(image_cars)
except oss2.exceptions.OssError as e:
print("Error:", e)Go
Version 3.0.2 or later of the OSS SDK for Go is required.
package main
import (
"fmt"
"io"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint for your bucket. For the China (Hangzhou) region, specify https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the corresponding endpoints.
// Specify the region ID, for 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 bucket name, for example, examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// If the image is in the root directory of the bucket, specify the image name. If it is in a subdirectory, specify the full path, for example, exampledir/example.jpg.
// Use the oss.Process method to build the processing instruction for vehicle detection.
body, err := bucket.GetObject("example.jpg", oss.Process("image/cars"))
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
Version 2.7.0 or later of the OSS SDK for PHP 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region where your bucket is located. For the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
// Specify the bucket name, for example, examplebucket.
$bucket = 'examplebucket';
// If the image is in the root directory of the bucket, specify the image name. If it is in a subdirectory, specify the full path, for example, exampledir/example.jpg.
$key = 'example.jpg';
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
// Specify the region ID.
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Build the processing instruction for vehicle detection.
$options[$ossClient::OSS_PROCESS] = "image/cars";
$result = $ossClient->getObject($bucket,$key,$options);
var_dump($result);
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}Parameters
Operation name: image/cars
For more information about the response parameters, see DetectImageCars.
API reference
Sample request
GET /example.jpg?x-oss-process=image/cars HTTP/1.1
Host: image-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 21 Jul 2023 08:57:28 GMT
Authorization: SignatureValueSample response
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Fri, 21 Jul 2023 08:57:30 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 250
Connection: keep-alive
x-oss-request-id: 64BA487A5423BA333952334F
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: 552
{
"RequestId" : "0C299A82-5D32-57DE-B66B-5E2A814C60BA",
"Cars": [
{
"LicensePlates": [
{
"Content": "LuA8***8",
"Boundary": {
"Width": 200,
"Height": 300,
"Left": 10,
"Top": 30
},
"Confidence": 0.789
}
],
"CarType": "van",
"CarTypeConfidence": 0.516,
"CarColor": "white",
"CarColorConfidence": 0.604,
"Boundary": {
"Width": 200,
"Height": 300,
"Left": 10,
"Top": 30
},
"Confidence": 0.999
}
]
}Billing
Vehicle detection calls the IMM service, which generates billable items from both OSS and IMM:
OSS: For detailed pricing, see OSS Pricing
API
Billable item
Description
GetObject
GET requests
Fees apply to each successful request.
Outbound traffic over the internet
Calling the GetObject operation from a public endpoint (for example, oss-cn-hangzhou.aliyuncs.com) or a transfer acceleration endpoint (for example, oss-accelerate.aliyuncs.com) generates outbound traffic fees, which are charged based on the data size.
Data retrieval for Infrequent Access
Retrieving data from the Infrequent Access storage class incurs data retrieval fees, which are charged based on the amount of data retrieved.
Transfer acceleration
Using a transfer acceleration domain name to access your bucket incurs transfer acceleration fees, which are charged based on the data size.
HeadObject
GET requests
Fees apply to each successful request.
IMM: For detailed pricing, see IMM billable items.
ImportantStarting from 11:00 on July 28, 2025 (UTC+8), the IMM vehicle detection service will change from a free to a paid service. It will be charged under the billable item ImageDetect. For more information, see the IMM billing adjustment announcement.
API
Billable item
Description
DetectImageCars
ImageDetect
Fees apply to each successful request.
Usage notes
Vehicle detection supports only synchronous processing (using the x-oss-process method).
-
Anonymous access will be denied.