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}}]}
測試下來,總體感受處理的結果和速度都很不錯。不過對於多輛汽車,以及俯視視角的時候效果相對差一些。