---
comments: true
---
# Vehicle Attribute Recognition Pipeline Tutorial
## 1. Introduction to Vehicle Attribute Recognition Pipeline
Vehicle attribute recognition is a crucial component in computer vision systems. Its primary task is to locate and label specific attributes of vehicles in images or videos, such as vehicle type, color, and license plate number. This task not only requires accurately detecting vehicles but also identifying detailed attribute information for each vehicle. The vehicle attribute recognition pipeline is an end-to-end serial system for locating and recognizing vehicle attributes, widely used in traffic management, intelligent parking, security surveillance, autonomous driving, and other fields. It significantly enhances system efficiency and intelligence levels, driving the development and innovation of related industries. This pipeline also offers a flexible service-oriented deployment approach, supporting the use of multiple programming languages on various hardware platforms. Moreover, this production line provides the capability for secondary development. You can train and optimize models on your own dataset based on this production line, and the trained models can be seamlessly integrated.
The vehicle attribute recognition pipeline includes a vehicle detection module and a vehicle attribute recognition module, with several models in each module. Which models to use can be selected based on the benchmark data below. If you prioritize model accuracy, choose models with higher accuracy; if you prioritize inference speed, choose models with faster inference; if you prioritize model storage size, choose models with smaller storage.
Vehicle Detection Module:
| Model | Model Download Link | mAP 0.5:0.95 | CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Size (M) | Description |
|---|---|---|---|---|---|---|
| PP-YOLOE-S_vehicle | Inference Model/Trained Model | 61.3 | 9.79 / 3.48 | 54.14 / 46.69 | 28.79 | Vehicle detection model based on PP-YOLOE |
| PP-YOLOE-L_vehicle | Inference Model/Trained Model | 63.9 | 32.84 / 9.03 | 176.60 / 176.60 | 196.02 |
Note: The above accuracy metrics are mAP(0.5:0.95) on the PPVehicle validation set. All GPU inference times are based on an NVIDIA Tesla T4 machine with FP32 precision. CPU inference speeds are based on an Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz with 8 threads and FP32 precision.
Vehicle Attribute Recognition Module:
| Model | Model Download Link | mAP (%) | CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
CPU Inference Time (ms) [Normal Mode / High-Performance Mode] |
Model Size (M) | Description |
|---|---|---|---|---|---|---|
| PP-LCNet_x1_0_vehicle_attribute | Inference Model/Trained Model | 91.7 | 2.32 / 0.52 | 3.22 / 1.26 | 6.7 M | PP-LCNet_x1_0_vehicle_attribute is a lightweight vehicle attribute recognition model based on PP-LCNet. |
Note: The above accuracy metrics are mA on the VeRi dataset. GPU inference times are based on an NVIDIA Tesla T4 machine with FP32 precision. CPU inference speeds are based on an Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz with 8 threads and FP32 precision.
## 2. Quick Start The pre-trained models provided by PaddleX can quickly demonstrate results. You can experience the effects of the vehicle attribute recognition pipeline online or locally using command line or Python. ### 2.1 Online Experience Not supported yet. ### 2.2 Local Experience Before using the vehicle attribute recognition pipeline locally, ensure you have installed the PaddleX wheel package according to the [PaddleX Local Installation Tutorial](../../../installation/installation.en.md). #### 2.2.1 Experience via Command Line You can quickly experience the vehicle attribute recognition pipeline with a single command. Use the [test file](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/vehicle_attribute_002.jpg) and replace `--input` with the local path for prediction. ```bash paddlex --pipeline vehicle_attribute_recognition --input vehicle_attribute_002.jpg --device gpu:0 --save_path ./output/ ``` Parameter Description: ```bash {'res': {'input_path': 'vehicle_attribute_002.jpg', 'boxes': [{'labels': ['red(红色)', 'sedan(轿车)'], 'cls_scores': array([0.96375, 0.94025]), 'det_score': 0.9774094820022583, 'coordinate': [196.32553, 302.3847, 639.3131, 655.57904]}, {'labels': ['suv(SUV)', 'brown(棕色)'], 'cls_scores': array([0.99968, 0.99317]), 'det_score': 0.9705657958984375, 'coordinate': [769.4419, 278.8417, 1401.0217, 641.3569]}]}} ``` For the explanation of the running result parameters, you can refer to the result interpretation in [Section 2.2.2 Integration via Python Script](#222-integration-via-python-script). The visualization results are saved under `save_path`, and the visualization result is as follows:
#### 2.2.2 Integration via Python Script
* The above command line is for quick experience and viewing of results. Generally, in projects, integration through code is often required. You can complete the pipeline's fast inference with just a few lines of code. The inference code is as follows:
The results obtained are the same as those from the command line method.
In the above Python script, the following steps are executed:
(1) The vehicle attribute recognition production line object is instantiated via `create_pipeline()`. The specific parameter descriptions are as follows:
| Parameter | Parameter Description | Parameter Type | Default Value |
|---|---|---|---|
pipeline |
The name of the production line or the path to the production line configuration file. If it is the name of a production line, it must be supported by PaddleX. | str |
None |
config |
Specific configuration information for the production line (if set simultaneously with pipeline, it has higher priority than pipeline, and the production line name must be consistent with pipeline). |
dict[str, Any] |
None |
device |
The device used for production line inference. It supports specifying the specific card number of GPUs, such as "gpu:0", other hardware card numbers, such as "npu:0", and CPUs, such as "cpu". | str |
gpu:0 |
use_hpip |
Whether to enable high-performance inference. This is only available if the production line supports high-performance inference. | bool |
False |
| Parameter | Parameter Description | Parameter Type | Options | Default Value |
|---|---|---|---|---|
input |
The data to be predicted. It supports multiple input types and is required. | Python Var|str|list |
|
None |
device |
The device used for production line inference. | str|None |
|
None |
det_threshold |
Threshold for vehicle detection visualization. | float | None |
|
0.5 |
cls_threshold |
Threshold for vehicle attribute prediction. | float | dict | list | None |
|
0.7 |
| Method | Description | Parameter | Type | Description | Default Value |
|---|---|---|---|---|---|
print() |
Print the result to the terminal. | format_json |
bool |
Whether to format the output content using JSON indentation. |
True |
indent |
int |
Specify the indentation level to beautify the output JSON data, making it more readable. This is only effective when format_json is True. |
4 | ||
ensure_ascii |
bool |
Control whether to escape non-ASCII characters to Unicode. If set to True, all non-ASCII characters will be escaped; False will retain the original characters. This is only effective when format_json is True. |
False |
||
save_to_json() |
Save the result as a JSON file. | save_path |
str |
The file path to save the result. When specified as a directory, the saved file will have the same name as the input file. | None |
indent |
int |
Specify the indentation level to beautify the output JSON data, making it more readable. This is only effective when format_json is True. |
4 | ||
ensure_ascii |
bool |
Control whether to escape non-ASCII characters to Unicode. If set to True, all non-ASCII characters will be escaped; False will retain the original characters. This is only effective when format_json is True. |
False |
||
save_to_img() |
Save the result as an image file. | save_path |
str |
The file path to save the result, supporting both directory and file paths. | None |
print() method, the result will be printed to the terminal. The printed content is explained as follows:
- `input_path`: `(str)` The input path of the image to be predicted.
- `boxes`: `(List[Dict])` The category IDs of the prediction results.
- `labels`: `(List[str])` The category names of the prediction results.
- `cls_scores`: `(List[numpy.ndarray])` The confidence scores of the attribute prediction results.
- `det_scores`: `(float)` The confidence scores of the vehicle detection boxes.
- When calling the save_to_json() method, the above content will be saved to the specified save_path. If a directory is specified, the saved path will be save_path/{your_img_basename}.json. If a file is specified, it will be saved directly to that file. Since JSON files do not support saving numpy arrays, the numpy.array type will be converted to a list format.
- When calling the save_to_img() method, the visualization result will be saved to the specified save_path. If a directory is specified, the saved path will be save_path/{your_img_basename}_res.{your_img_extension}. If a file is specified, it will be saved directly to that file. (In production, there are usually many result images, so it is not recommended to specify a specific file path directly; otherwise, multiple images will be overwritten, and only the last image will be retained.)
* Additionally, it also supports obtaining visualized images with results and prediction results through attributes, as follows:
| Attribute | Description |
|---|---|
json |
Get the prediction result in json format |
img |
Get the visualized image in dict format |
For the main operations provided by the service:
200, and the attributes of the response body are as follows:| Name | Type | Description |
|---|---|---|
logId |
string |
The UUID of the request. |
errorCode |
integer |
Error code. Fixed as 0. |
errorMsg |
string |
Error message. Fixed as "Success". |
result |
object |
The result of the operation. |
| Name | Type | Description |
|---|---|---|
logId |
string |
The UUID of the request. |
errorCode |
integer |
Error code. Same as the response status code. |
errorMsg |
string |
Error message. |
The main operations provided by the service are as follows:
inferGet the vehicle attribute recognition results.
POST /vehicle-attribute-recognition
| Name | Type | Description | Required |
|---|---|---|---|
image |
string |
The URL of the image file accessible by the server or the Base64-encoded content of the image file. | Yes |
result of the response body has the following attributes:| Name | Type | Description |
|---|---|---|
vehicles |
array |
Information about the vehicle's location and attributes. |
image |
string |
The vehicle attribute recognition result image. The image is in JPEG format and is Base64-encoded. |
Each element in vehicles is an object with the following attributes:
| Name | Type | Description |
|---|---|---|
bbox |
array |
The location of the vehicle. The elements in the array are the x-coordinate of the top-left corner, the y-coordinate of the top-left corner, the x-coordinate of the bottom-right corner, and the y-coordinate of the bottom-right corner. |
attributes |
array |
The attributes of the vehicle. |
score |
number |
Detection score. |
Each element in attributes is an object with the following attributes:
| Name | Type | Description |
|---|---|---|
label |
string |
The attribute label. |
score |
number |
Classification score. |
import base64
import requests
API_URL = "http://localhost:8080/vehicle-attribute-recognition" # Service URL
image_path = "./demo.jpg"
output_image_path = "./out.jpg"
# Encode the local image using Base64
with open(image_path, "rb") as file:
image_bytes = file.read()
image_data = base64.b64encode(image_bytes).decode("ascii")
payload = {"image": image_data} # Base64-encoded file content or image URL
# Call the API
response = requests.post(API_URL, json=payload)
# Process the response data
assert response.status_code == 200
result = response.json()["result"]
with open(output_image_path, "wb") as file:
file.write(base64.b64decode(result["image"]))
print(f"Output image saved at {output_image_path}")
print("\nDetected vehicles:")
print(result["vehicles"])
| Scenario | Fine-Tuning Module | Fine-Tuning Reference Link |
|---|---|---|
| Inaccurate vehicle detection | Vehicle Detection Module | Link |
| Inaccurate attribute recognition | Vehicle Attribute Recognition Module | Link |