车辆属性识别
更新时间:2024-06-17
该接口已停止更新,为避免影响您的业务使用,建议尽快安排业务迁移
接口描述
传入单帧图像,检测图片中所有车辆,返回每辆车的类型和坐标位置,可识别小汽车、卡车、巴士、三轮车、两轮车、车牌,并针对车辆识别24种外观属性,包括:是否有车窗雨眉、是否有车顶架、副驾驶是否有人、车身颜色、特种车类型、渣土车满载等。
当前主要适用于普通监控场景,如道路、停车场等,无人机高空拍摄的图片,因车辆目标较小,识别效果可能欠佳,后续会扩展支持超高空拍摄场景,如有该场景的需求,请通过QQ群或提交工单,详细沟通测试情况。
普通监控场景示例图片:
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
请求说明
请求示例
HTTP 方法:POST
请求URL:https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 取值范围 | 说明 |
---|---|---|---|---|
image | 和url二选一 | string | 0-255彩色图像(base64编码) | 图像数据,Base64编码字符串,不超过4M。最短边至少50px,最长边最多4096px。支持图片格式:jpg/bmp/png。 注意:图片的base64编码是不包含图片头的,如(data:image/jpg;base64,) |
url | 和image二选一 | string | - | 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少50px,最长边最大4096px,支持jpg/png/bmp格式,当image字段存在时url字段失效。 |
type | 否 | string | 是否选定某些属性输出对应的信息,可从24种输出属性中任选若干,用英文逗号分隔(例如top_holder,skylight,window_rain_eyebrow)。默认输出全部属性 |
附:type字段说明
ID | type | 说明 | 类别数 | 类别 |
---|---|---|---|---|
0 | top_holder | 是否有车顶架 | 2 | 是否有车顶架 |
1 | skylight | 是否有天窗 | 2 | 无天窗、有天窗 |
2 | window_rain_eyebrow | 是否有车窗雨眉 | 2 | 无车窗雨眉、有车窗雨眉 |
3 | vehicle_front_item_placeitems | 是否有车前摆放物 | 2 | 无车前摆放物、有车前摆放物 |
4 | vehicle_front_item_pendant | 是否有后视镜挂件 | 2 | 无后视镜挂件、有后视镜挂件 |
5 | has_copilot | 副驾驶位是否有人 | 2 | 副驾驶无人、副驾驶有人 |
6 | safety_belt_copilot | 副驾驶安全带是否系带 | 2 | 副驾驶未系安全带、副驾驶系安全带 |
7 | safety_belt_pilot | 驾驶员安全带是否系带 | 2 | 驾驶员安全带是否系带 |
8 | sunvisor_pilot | 驾驶员遮阳板是否放下 | 2 | 驾驶员遮阳板未放下、驾驶员遮阳板放下 |
9 | sunvisor_copilot | 副驾驶遮阳板是否放下 | 2 | 副驾驶遮阳板未放下、副驾驶遮阳板放下 |
10 | direction | 车辆行驶方向 | 4 | 车辆正向行驶、车辆背向行驶、车辆左侧行驶、车辆右侧行驶 |
11 | has_plate | 是否无牌车 | 2 | 有车牌、无车牌 |
12 | plate_stained | 是否污损车牌 | 2 | 车牌无污损、车牌污损 |
13 | dangerous_vehicle | 是否为危化品车 | 2 | 非危险品车、危险品车 |
14 | slag_vehicle | 是否为渣土车 | 2 | 非渣土车、渣土车 |
15 | slag_vehicle_cover | 渣土车是否盖板 | 2 | 渣土车未盖板、渣土车苫盖 |
16 | vehicle_inspection | 是否有年检标 | 2 | 无年检标、有年检标 |
17 | vehicle_color | 车身颜色 | 12 | 车身颜色白色;车身颜色灰色;车身颜色黄色;车身颜色粉色;车身颜色红色;车身颜色紫色;车身颜色绿色;车身颜色蓝色;车身颜色棕色;车身颜色黑色;车身颜色橙色;车身颜色混色 |
18 | special_vehicle | 特种车类型 | 9 | 普通车、警车、消防车、救护车、施工工程车、工程抢险车、洒水车、搅拌车、校车 |
19 | vehicle_shielding | 遮挡 | 3 | 无遮挡、0-50%遮挡、50-100%遮挡 |
20 | slag_full_loaded | 渣土车满载 | 2 | 渣土车未满载、渣土车满载 |
21 | slag_refit | 渣土车改装 | 2 | 渣土车未改装、渣土车改装 |
22 | plate_cover | 车牌遮挡 | 2 | 车牌未遮挡、车牌遮挡 |
23 | vehicle_class | 车辆类型识别 | 3 | 两轮车(主要是摩托)、三轮车、四轮车 |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
1curl -i -k 'https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
1<?php
2/**
3 * 发起http post请求(REST API), 并获取REST请求的结果
4 * @param string $url
5 * @param string $param
6 * @return - http response body if succeeds, else false.
7 */
8function request_post($url = '', $param = '')
9{
10 if (empty($url) || empty($param)) {
11 return false;
12 }
13
14 $postUrl = $url;
15 $curlPost = $param;
16 // 初始化curl
17 $curl = curl_init();
18 curl_setopt($curl, CURLOPT_URL, $postUrl);
19 curl_setopt($curl, CURLOPT_HEADER, 0);
20 // 要求结果为字符串且输出到屏幕上
21 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
22 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
23 // post提交方式
24 curl_setopt($curl, CURLOPT_POST, 1);
25 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
26 // 运行curl
27 $data = curl_exec($curl);
28 curl_close($curl);
29
30 return $data;
31}
32
33$token = '[调用鉴权接口获取的token]';
34$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr?access_token=' . $token;
35$img = file_get_contents('[本地文件路径]');
36$img = base64_encode($img);
37$bodys = array(
38 'image' => $img
39);
40$res = request_post($url, $bodys);
41
42var_dump($res);
1package com.baidu.ai.aip;
2
3import com.baidu.ai.aip.utils.Base64Util;
4import com.baidu.ai.aip.utils.FileUtil;
5import com.baidu.ai.aip.utils.HttpUtil;
6
7import java.net.URLEncoder;
8
9/**
10* 车辆分析—车辆属性识别
11*/
12public class VehicleAttr {
13
14 /**
15 * 重要提示代码中所需工具类
16 * FileUtil,Base64Util,HttpUtil,GsonUtils请从
17 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
18 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
19 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
20 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
21 * 下载
22 */
23 public static String vehicle_attr() {
24 // 请求url
25 String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr";
26 try {
27 // 本地文件路径
28 String filePath = "[本地文件路径]";
29 byte[] imgData = FileUtil.readFileByBytes(filePath);
30 String imgStr = Base64Util.encode(imgData);
31 String imgParam = URLEncoder.encode(imgStr, "UTF-8");
32
33 String param = "image=" + imgParam;
34
35 // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
36 String accessToken = "[调用鉴权接口获取的token]";
37
38 String result = HttpUtil.post(url, accessToken, param);
39 System.out.println(result);
40 return result;
41 } catch (Exception e) {
42 e.printStackTrace();
43 }
44 return null;
45 }
46
47 public static void main(String[] args) {
48 VehicleAttr.vehicle_attr();
49 }
50}
1# encoding:utf-8
2
3import requests
4import base64
5
6'''
7车辆分析—车辆属性识别
8'''
9
10request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr"
11# 二进制方式打开图片文件
12f = open('[本地文件]', 'rb')
13img = base64.b64encode(f.read())
14
15params = {"image":img}
16access_token = '[调用鉴权接口获取的token]'
17request_url = request_url + "?access_token=" + access_token
18headers = {'content-type': 'application/x-www-form-urlencoded'}
19response = requests.post(request_url, data=params, headers=headers)
20if response:
21 print (response.json())
1#include <iostream>
2#include <curl/curl.h>
3
4// libcurl库下载链接:https://curl.haxx.se/download.html
5// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
6const static std::string request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr";
7static std::string vehicle_attr_result;
8/**
9 * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
10 * @param 参数定义见libcurl文档
11 * @return 返回值定义见libcurl文档
12 */
13static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
14 // 获取到的body存放在ptr中,先将其转换为string格式
15 vehicle_attr_result = std::string((char *) ptr, size * nmemb);
16 return size * nmemb;
17}
18/**
19 * 车辆分析—车辆属性识别
20 * @return 调用成功返回0,发生错误返回其他错误码
21 */
22int vehicle_attr(std::string &json_result, const std::string &access_token) {
23 std::string url = request_url + "?access_token=" + access_token;
24 CURL *curl = NULL;
25 CURLcode result_code;
26 int is_success;
27 curl = curl_easy_init();
28 if (curl) {
29 curl_easy_setopt(curl, CURLOPT_URL, url.data());
30 curl_easy_setopt(curl, CURLOPT_POST, 1);
31 curl_httppost *post = NULL;
32 curl_httppost *last = NULL;
33 curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
34
35 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
36 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
37 result_code = curl_easy_perform(curl);
38 if (result_code != CURLE_OK) {
39 fprintf(stderr, "curl_easy_perform() failed: %s\n",
40 curl_easy_strerror(result_code));
41 is_success = 1;
42 return is_success;
43 }
44 json_result = vehicle_attr_result;
45 curl_easy_cleanup(curl);
46 is_success = 0;
47 } else {
48 fprintf(stderr, "curl_easy_init() failed.");
49 is_success = 1;
50 }
51 return is_success;
52}
1using System;
2using System.IO;
3using System.Net;
4using System.Text;
5using System.Web;
6
7namespace com.baidu.ai
8{
9 public class VehicleAttr
10 {
11 // 车辆分析—车辆属性识别
12 public static string vehicle_attr()
13 {
14 string token = "[调用鉴权接口获取的token]";
15 string host = "https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr?access_token=" + token;
16 Encoding encoding = Encoding.Default;
17 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
18 request.Method = "post";
19 request.KeepAlive = true;
20 // 图片的base64编码
21 string base64 = getFileBase64("[本地图片文件]");
22 String str = "image=" + HttpUtility.UrlEncode(base64);
23 byte[] buffer = encoding.GetBytes(str);
24 request.ContentLength = buffer.Length;
25 request.GetRequestStream().Write(buffer, 0, buffer.Length);
26 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
27 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
28 string result = reader.ReadToEnd();
29 Console.WriteLine("车辆分析—车辆属性识别:");
30 Console.WriteLine(result);
31 return result;
32 }
33
34 public static String getFileBase64(String fileName) {
35 FileStream filestream = new FileStream(fileName, FileMode.Open);
36 byte[] arr = new byte[filestream.Length];
37 filestream.Read(arr, 0, (int)filestream.Length);
38 string baser64 = Convert.ToBase64String(arr);
39 filestream.Close();
40 return baser64;
41 }
42 }
43}
返回说明
返回参数 以type选定top_holder为例:
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
vehicle_info | 是 | object数组 | 每个框的具体信息 |
vehicle_num | 是 | int | 检测到的车辆框数目 |
+location | 是 | object | 检测到的车辆框位置 |
++left | 是 | int | 检测框左坐标 |
++top | 是 | int | 检测框顶坐标 |
++width | 是 | int | 检测框宽度 |
++height | 是 | int | 检测框高度 |
++cls | 是 | int | 检测主体类型,返回int类型的id,id对应内容如下:1:小汽车(car);2:卡车(truck);3:巴士(bus);4:二轮车-主要为摩托车,含自行车(motorbike);5:三轮车(tricycle);6:车牌(carplate);-1:图片中未检测到车辆(无检测结果) |
++score | 是 | float | 车辆置信度 |
+attributes | 是 | object数组 | 车辆属性内容(这里仅列举其中一个进行说明,全部24个属性见属性字段说明) |
++top_holder | 否 | object | 车顶架 |
++++name | 否 | string | 无车顶架、有车顶架 |
++++score | 否 | float | 对应概率分数 |
返回示例
未检测到任何车辆:
Plain Text
1{
2 "log_id": 1522102386508316465,
3 "vehicle_info": [
4 {
5 "attributes": {},
6 "location": {
7 "score": 0,
8 "top": 0,
9 "left": 0,
10 "width": 0,
11 "cls": -1,
12 "height": 0
13 }
14 }
15 ],
16 "vehicle_num": 0
17}
检测到2辆小汽车:
Plain Text
1 {
2 "log_id": 1522103149454741267,
3 "vehicle_info": [
4 {
5 "attributes": {
6 "vehicle_color": {
7 "score": 0.9207834601402283,
8 "name": "车身颜色白色"
9 },
10 "slag_full_loaded": {
11 "score": 0.9757698774337769,
12 "name": "渣土车未满载"
13 },
14 "safety_belt_copilot": {
15 "score": 0.9999989867210388,
16 "name": "副驾驶系安全带"
17 },
18 "vehicle_front_item_placeitems": {
19 "score": 0.7926750183105469,
20 "name": "有车前摆放物"
21 },
22 "vehicle_inspection": {
23 "score": 0.5232551693916321,
24 "name": "有年检标"
25 },
26 "skylight": {
27 "score": 0.7827605605125427,
28 "name": "无天窗"
29 },
30 "dangerous_vehicle": {
31 "score": 0.9992908239364624,
32 "name": "非危险品车"
33 },
34 "vehicle_shielding": {
35 "score": 0.9654262661933899,
36 "name": "无遮挡"
37 },
38 "sunvisor_copilot": {
39 "score": 0.9938165545463562,
40 "name": "副驾驶遮阳板未放下"
41 },
42 "safety_belt_pilot": {
43 "score": 0.5320025682449341,
44 "name": "驾驶员未系安全带"
45 },
46 "window_rain_eyebrow": {
47 "score": 0.9973089694976807,
48 "name": "无车窗雨眉"
49 },
50 "sunvisor_pilot": {
51 "score": 0.9889488220214844,
52 "name": "驾驶员遮阳板未放下"
53 },
54 "plate_stained": {
55 "score": 0.9904413223266602,
56 "name": "车牌无污损"
57 },
58 "slag_vehicle": {
59 "score": 0.9885181784629822,
60 "name": "非渣土车"
61 },
62 "vehicle_front_item_pendant": {
63 "score": 0.9655022621154785,
64 "name": "无后视镜挂件"
65 },
66 "has_plate": {
67 "score": 0.7274292707443237,
68 "name": "有车牌"
69 },
70 "vehicle_class": {
71 "score": 0.8929595947265625,
72 "name": "四轮车"
73 },
74 "special_vehicle": {
75 "score": 0.8439596891403198,
76 "name": "普通车"
77 },
78 "plate_cover": {
79 "score": 0.9869874715805054,
80 "name": "车牌未遮挡"
81 },
82 "top_holder": {
83 "score": 0.9910229444503784,
84 "name": "无车顶架"
85 },
86 "slag_vehicle_cover": {
87 "score": 0.5242087244987488,
88 "name": "渣土车未盖板"
89 },
90 "slag_refit": {
91 "score": 0.9636361598968506,
92 "name": "渣土车未改装"
93 },
94 "has_copilot": {
95 "score": 0.9043570756912231,
96 "name": "副驾驶无人"
97 },
98 "direction": {
99 "score": 0.8187354803085327,
100 "name": "车辆正向行驶"
101 }
102 },
103 "location": {
104 "score": 0.9702600240707397,
105 "top": 170,
106 "left": 419,
107 "width": 80,
108 "cls": 1,
109 "height": 86
110 }
111 },
112 {
113 "attributes": {
114 "vehicle_color": {
115 "score": 0.9032244086265564,
116 "name": "车身颜色黄色"
117 },
118 "slag_full_loaded": {
119 "score": 0.9681426882743835,
120 "name": "渣土车未满载"
121 },
122 "safety_belt_copilot": {
123 "score": 0.9999989867210388,
124 "name": "副驾驶系安全带"
125 },
126 "vehicle_front_item_placeitems": {
127 "score": 0.7316561937332153,
128 "name": "有车前摆放物"
129 },
130 "vehicle_inspection": {
131 "score": 0.6960608959197998,
132 "name": "有年检标"
133 },
134 "skylight": {
135 "score": 0.9296075105667114,
136 "name": "无天窗"
137 },
138 "dangerous_vehicle": {
139 "score": 0.9980440139770508,
140 "name": "非危险品车"
141 },
142 "vehicle_shielding": {
143 "score": 0.9729943871498108,
144 "name": "无遮挡"
145 },
146 "sunvisor_copilot": {
147 "score": 0.9857694506645203,
148 "name": "副驾驶遮阳板未放下"
149 },
150 "safety_belt_pilot": {
151 "score": 0.6393722891807556,
152 "name": "驾驶员未系安全带"
153 },
154 "window_rain_eyebrow": {
155 "score": 0.9806464910507202,
156 "name": "无车窗雨眉"
157 },
158 "sunvisor_pilot": {
159 "score": 0.7367949485778809,
160 "name": "驾驶员遮阳板未放下"
161 },
162 "plate_stained": {
163 "score": 0.9940692186355591,
164 "name": "车牌无污损"
165 },
166 "slag_vehicle": {
167 "score": 0.986552894115448,
168 "name": "非渣土车"
169 },
170 "vehicle_front_item_pendant": {
171 "score": 0.9854007363319397,
172 "name": "无后视镜挂件"
173 },
174 "has_plate": {
175 "score": 0.9633370637893677,
176 "name": "有车牌"
177 },
178 "vehicle_class": {
179 "score": 0.9078437685966492,
180 "name": "四轮车"
181 },
182 "special_vehicle": {
183 "score": 0.8234432339668274,
184 "name": "校车"
185 },
186 "plate_cover": {
187 "score": 0.9921958446502686,
188 "name": "车牌未遮挡"
189 },
190 "top_holder": {
191 "score": 0.98869788646698,
192 "name": "无车顶架"
193 },
194 "slag_vehicle_cover": {
195 "score": 0.7752302885055542,
196 "name": "渣土车未盖板"
197 },
198 "slag_refit": {
199 "score": 0.9444331526756287,
200 "name": "渣土车未改装"
201 },
202 "has_copilot": {
203 "score": 0.9544827342033386,
204 "name": "副驾驶无人"
205 },
206 "direction": {
207 "score": 0.8230369091033936,
208 "name": "车辆正向行驶"
209 }
210 },
211 "location": {
212 "score": 0.9173700213432312,
213 "top": 136,
214 "left": 281,
215 "width": 125,
216 "cls": 1,
217 "height": 102
218 }
219 }
220 ],
221 "vehicle_num": 2
222}