百度AI攻略:车辆属性识别

1.功能描述:python

检测图像中的各种车辆,并针对小汽车识别11种外观属性,包括:是否有车窗雨眉、是否有车顶架、副驾驶是否有人等,可用于交通安防场景的特定车辆检测追踪。json

2.平台接入app

车辆属性识别接入网址:http://ai.baidu.com/tech/vehicle/attr测试

具体接入方式比较简单,能够参考个人另外一个帖子,这里就不重复了:编码

http://ai.baidu.com/forum/topic/show/943327url

3.调用攻略(Python3)及评测rest

3.1首先认证受权:code

在开始调用任何API以前须要先进行认证受权,具体的说明请参考:orm

http://ai.baidu.com/docs#/Auth/topblog

具体Python3代码以下:

# -*- coding: utf-8 -*-

#!/usr/bin/env python

import urllib

import base64

import json

#client_id 为官网获取的AK, client_secret 为官网获取的SK

client_id =【百度云应用的AK】

client_secret =【百度云应用的SK】

#获取token

def get_token():

    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret

    request = urllib.request.Request(host)

    request.add_header('Content-Type', 'application/json; charset=UTF-8')

    response = urllib.request.urlopen(request)

    token_content = response.read()

    if token_content:

        token_info = json.loads(token_content)

        token_key = token_info['access_token']

    return token_key

3.2车辆属性识别分析接口调用:

详细说明请参考: https://ai.baidu.com/docs#/ImageClassify-API/29196fe3

说明的比较清晰,这里就不重复了。

你们须要注意的是:

API访问URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_attr

图像数据,Base64编码字符串,不超过4M。最短边至少10px,最长边最多4096px。支持图片格式:jpg,bmp,png。 注意:图片的base64编码是不包含图片头的,如(data:image/jpg;base64,)

Python3调用代码以下:

#车辆属性识别

#filename:图片名(本地存储包括路径)

def vehicle_attr(filename):

    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_attr"

 

    # 二进制方式打开图片文件

    f = open(filename, 'rb')

    img = base64.b64encode(f.read())

 

    params = dict()

    params['image'] = img

    params['show'] = 'true'

    params = urllib.parse.urlencode(params).encode("utf-8")

    #params = json.dumps(params).encode('utf-8')

 

    access_token = get_token()

    request_url = request_url + "?access_token=" + access_token

    request = urllib.request.Request(url=request_url, data=params)

    request.add_header('Content-Type', 'application/x-www-form-urlencoded')

    response = urllib.request.urlopen(request)

    content = response.read()

    if content:

        #print(content)

        content=content.decode('utf-8')

        print(content)

 

vehicle_attr('car1.jpg') 

4.功能评测及建议:

选用不一样的数据对效果进行测试,具体效果以下:

{"log_id": 2553167137379126576, "vehicle_num": 1, "vehicle_info": [{"attributes": {"direction": {"score": 0.5358242392539978, "name": "左前方"}, "copilot_belt": {"score": 0.2600597143173218}, "copilot_visor": {"score": 0.004816591739654541}, "rearview_item": {"score": 0.1061452031135559}, "driver_visor": {"score": 0.01984471082687378}, "in_car_item": {"score": 0.8143197894096375}, "skylight": {"score": 0.5828855633735657}, "copilot": {"score": 0.0791858434677124}, "window_rain_eyebrow": {"score": 0.005147635936737061}, "vehicle_type": {"score": 0.9827386736869812, "name": "小汽车"}, "roof_rack": {"score": 0.07000851631164551}, "driver_belt": {"score": 0.9432666301727295}}, "location": {"width": 2358, "top": 366, "left": 262, "height": 1242}}]}

测试下来,总体感受处理的结果和速度都很不错。不过对于多辆汽车,以及俯视视角的时候效果相对差一些。

相关文章
相关标签/搜索