资讯 社区 文档
技术能力
语音技术
文字识别
人脸与人体
图像技术
语言与知识
视频技术

车辆属性识别

该接口已停止更新,为避免影响您的业务使用,建议尽快安排业务迁移

接口描述

传入单帧图像,检测图片中所有车辆,返回每辆车的类型和坐标位置,可识别小汽车、卡车、巴士、三轮车、两轮车、车牌,并针对车辆识别24种外观属性,包括:是否有车窗雨眉、是否有车顶架、副驾驶是否有人、车身颜色、特种车类型、渣土车满载等

当前主要适用于普通监控场景,如道路、停车场等,无人机高空拍摄的图片,因车辆目标较小,识别效果可能欠佳,后续会扩展支持超高空拍摄场景,如有该场景的需求,请通过QQ群或提交工单,详细沟通测试情况。

普通监控场景示例图片:
车辆属性识别.jpg

在线调试

您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。

请求说明

请求示例

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信息。

提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。

curl -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'
<?php
/**
 * 发起http post请求(REST API), 并获取REST请求的结果
 * @param string $url
 * @param string $param
 * @return - http response body if succeeds, else false.
 */
function request_post($url = '', $param = '')
{
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);

    return $data;
}

$token = '[调用鉴权接口获取的token]';
$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr?access_token=' . $token;
$img = file_get_contents('[本地文件路径]');
$img = base64_encode($img);
$bodys = array(
    'image' => $img
);
$res = request_post($url, $bodys);

var_dump($res);
package com.baidu.ai.aip;

import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;

import java.net.URLEncoder;

/**
* 车辆分析—车辆属性识别
*/
public class VehicleAttr {

    /**
    * 重要提示代码中所需工具类
    * FileUtil,Base64Util,HttpUtil,GsonUtils请从
    * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
    * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
    * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
    * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
    * 下载
    */
    public static String vehicle_attr() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr";
        try {
            // 本地文件路径
            String filePath = "[本地文件路径]";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
            String accessToken = "[调用鉴权接口获取的token]";

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        VehicleAttr.vehicle_attr();
    }
}
# encoding:utf-8

import requests
import base64

'''
车辆分析—车辆属性识别
'''

request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())

params = {"image":img}
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())
#include <iostream>
#include <curl/curl.h>

// libcurl库下载链接:https://curl.haxx.se/download.html
// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
const static std::string request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr";
static std::string vehicle_attr_result;
/**
 * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
 * @param 参数定义见libcurl文档
 * @return 返回值定义见libcurl文档
 */
static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
    // 获取到的body存放在ptr中,先将其转换为string格式
    vehicle_attr_result = std::string((char *) ptr, size * nmemb);
    return size * nmemb;
}
/**
 * 车辆分析—车辆属性识别
 * @return 调用成功返回0,发生错误返回其他错误码
 */
int vehicle_attr(std::string &json_result, const std::string &access_token) {
    std::string url = request_url + "?access_token=" + access_token;
    CURL *curl = NULL;
    CURLcode result_code;
    int is_success;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.data());
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_httppost *post = NULL;
        curl_httppost *last = NULL;
        curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);

        curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
        result_code = curl_easy_perform(curl);
        if (result_code != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(result_code));
            is_success = 1;
            return is_success;
        }
        json_result = vehicle_attr_result;
        curl_easy_cleanup(curl);
        is_success = 0;
    } else {
        fprintf(stderr, "curl_easy_init() failed.");
        is_success = 1;
    }
    return is_success;
}
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace com.baidu.ai
{
    public class VehicleAttr
    {
        // 车辆分析—车辆属性识别
        public static string vehicle_attr()
        {
            string token = "[调用鉴权接口获取的token]";
            string host = "https://aip.baidubce.com/rest/2.0/image-classify/v2/vehicle_attr?access_token=" + token;
            Encoding encoding = Encoding.Default;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
            request.Method = "post";
            request.KeepAlive = true;
            // 图片的base64编码
            string base64 = getFileBase64("[本地图片文件]");
            String str = "image=" + HttpUtility.UrlEncode(base64);
            byte[] buffer = encoding.GetBytes(str);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
            string result = reader.ReadToEnd();
            Console.WriteLine("车辆分析—车辆属性识别:");
            Console.WriteLine(result);
            return result;
        }

        public static String getFileBase64(String fileName) {
            FileStream filestream = new FileStream(fileName, FileMode.Open);
            byte[] arr = new byte[filestream.Length];
            filestream.Read(arr, 0, (int)filestream.Length);
            string baser64 = Convert.ToBase64String(arr);
            filestream.Close();
            return baser64;
        }
    }
}

返回说明

返回参数 以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 对应概率分数

返回示例

未检测到任何车辆:

{
  "log_id": 1522102386508316465,
  "vehicle_info": [
    {
      "attributes": {},
      "location": {
        "score": 0,
        "top": 0,
        "left": 0,
        "width": 0,
        "cls": -1,
        "height": 0
      }
    }
  ],
  "vehicle_num": 0
}

检测到2辆小汽车:

   {
  "log_id": 1522103149454741267,
  "vehicle_info": [
    {
      "attributes": {
        "vehicle_color": {
          "score": 0.9207834601402283,
          "name": "车身颜色白色"
        },
        "slag_full_loaded": {
          "score": 0.9757698774337769,
          "name": "渣土车未满载"
        },
        "safety_belt_copilot": {
          "score": 0.9999989867210388,
          "name": "副驾驶系安全带"
        },
        "vehicle_front_item_placeitems": {
          "score": 0.7926750183105469,
          "name": "有车前摆放物"
        },
        "vehicle_inspection": {
          "score": 0.5232551693916321,
          "name": "有年检标"
        },
        "skylight": {
          "score": 0.7827605605125427,
          "name": "无天窗"
        },
        "dangerous_vehicle": {
          "score": 0.9992908239364624,
          "name": "非危险品车"
        },
        "vehicle_shielding": {
          "score": 0.9654262661933899,
          "name": "无遮挡"
        },
        "sunvisor_copilot": {
          "score": 0.9938165545463562,
          "name": "副驾驶遮阳板未放下"
        },
        "safety_belt_pilot": {
          "score": 0.5320025682449341,
          "name": "驾驶员未系安全带"
        },
        "window_rain_eyebrow": {
          "score": 0.9973089694976807,
          "name": "无车窗雨眉"
        },
        "sunvisor_pilot": {
          "score": 0.9889488220214844,
          "name": "驾驶员遮阳板未放下"
        },
        "plate_stained": {
          "score": 0.9904413223266602,
          "name": "车牌无污损"
        },
        "slag_vehicle": {
          "score": 0.9885181784629822,
          "name": "非渣土车"
        },
        "vehicle_front_item_pendant": {
          "score": 0.9655022621154785,
          "name": "无后视镜挂件"
        },
        "has_plate": {
          "score": 0.7274292707443237,
          "name": "有车牌"
        },
        "vehicle_class": {
          "score": 0.8929595947265625,
          "name": "四轮车"
        },
        "special_vehicle": {
          "score": 0.8439596891403198,
          "name": "普通车"
        },
        "plate_cover": {
          "score": 0.9869874715805054,
          "name": "车牌未遮挡"
        },
        "top_holder": {
          "score": 0.9910229444503784,
          "name": "无车顶架"
        },
        "slag_vehicle_cover": {
          "score": 0.5242087244987488,
          "name": "渣土车未盖板"
        },
        "slag_refit": {
          "score": 0.9636361598968506,
          "name": "渣土车未改装"
        },
        "has_copilot": {
          "score": 0.9043570756912231,
          "name": "副驾驶无人"
        },
        "direction": {
          "score": 0.8187354803085327,
          "name": "车辆正向行驶"
        }
      },
      "location": {
        "score": 0.9702600240707397,
        "top": 170,
        "left": 419,
        "width": 80,
        "cls": 1,
        "height": 86
      }
    },
    {
      "attributes": {
        "vehicle_color": {
          "score": 0.9032244086265564,
          "name": "车身颜色黄色"
        },
        "slag_full_loaded": {
          "score": 0.9681426882743835,
          "name": "渣土车未满载"
        },
        "safety_belt_copilot": {
          "score": 0.9999989867210388,
          "name": "副驾驶系安全带"
        },
        "vehicle_front_item_placeitems": {
          "score": 0.7316561937332153,
          "name": "有车前摆放物"
        },
        "vehicle_inspection": {
          "score": 0.6960608959197998,
          "name": "有年检标"
        },
        "skylight": {
          "score": 0.9296075105667114,
          "name": "无天窗"
        },
        "dangerous_vehicle": {
          "score": 0.9980440139770508,
          "name": "非危险品车"
        },
        "vehicle_shielding": {
          "score": 0.9729943871498108,
          "name": "无遮挡"
        },
        "sunvisor_copilot": {
          "score": 0.9857694506645203,
          "name": "副驾驶遮阳板未放下"
        },
        "safety_belt_pilot": {
          "score": 0.6393722891807556,
          "name": "驾驶员未系安全带"
        },
        "window_rain_eyebrow": {
          "score": 0.9806464910507202,
          "name": "无车窗雨眉"
        },
        "sunvisor_pilot": {
          "score": 0.7367949485778809,
          "name": "驾驶员遮阳板未放下"
        },
        "plate_stained": {
          "score": 0.9940692186355591,
          "name": "车牌无污损"
        },
        "slag_vehicle": {
          "score": 0.986552894115448,
          "name": "非渣土车"
        },
        "vehicle_front_item_pendant": {
          "score": 0.9854007363319397,
          "name": "无后视镜挂件"
        },
        "has_plate": {
          "score": 0.9633370637893677,
          "name": "有车牌"
        },
        "vehicle_class": {
          "score": 0.9078437685966492,
          "name": "四轮车"
        },
        "special_vehicle": {
          "score": 0.8234432339668274,
          "name": "校车"
        },
        "plate_cover": {
          "score": 0.9921958446502686,
          "name": "车牌未遮挡"
        },
        "top_holder": {
          "score": 0.98869788646698,
          "name": "无车顶架"
        },
        "slag_vehicle_cover": {
          "score": 0.7752302885055542,
          "name": "渣土车未盖板"
        },
        "slag_refit": {
          "score": 0.9444331526756287,
          "name": "渣土车未改装"
        },
        "has_copilot": {
          "score": 0.9544827342033386,
          "name": "副驾驶无人"
        },
        "direction": {
          "score": 0.8230369091033936,
          "name": "车辆正向行驶"
        }
      },
      "location": {
        "score": 0.9173700213432312,
        "top": 136,
        "left": 281,
        "width": 125,
        "cls": 1,
        "height": 102
      }
    }
  ],
  "vehicle_num": 2
}
上一篇
车辆外观损伤识别
下一篇
车辆检测