第二期【百度大脑新品体验】车辆属性识别
才能我浪费99 发布于2019-05 浏览:1442 回复:2
1
收藏

1.功能描述:

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

2.平台接入

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

具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:
http://ai.baidu.com/forum/topic/show/943327

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

3.1首先认证授权:

在开始调用任何API之前需要先进行认证授权,具体的说明请参考:

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

具体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}}]}
测试下来,整体感觉处理的结果和速度都很不错。不过对于多辆汽车,以及俯视视角的时候效果相对差一些。

收藏
点赞
1
个赞
共2条回复 最后由用户已被禁言回复于2022-04
#3才能我浪费99回复于2019-05

就是汽车很多的时候漏的比较多

0
#2才能我浪费99回复于2019-05

感觉效果还可以

0
TOP
切换版块