作者:wangwei8638
针对车载场景,识别驾驶员使用手机、抽烟、不系安全带、双手离开方向盘等动作姿态,分析预警危险驾驶行为,提升行车安全性。
一.平台接入
此步骤比较简单,不多阐述。可参照之前文档:
https://ai.baidu.com/forum/topic/show/943162
二.分析接口文档
1.打开API文档页面,分析接口要求
https://ai.baidu.com/docs#/Body-API/2387dd4f
(1)接口描述
检测到驾驶员后,进一步识别行为属性,可识别使用手机、抽烟、不系安全带、双手离开方向盘、视角未朝前方5大类行为。
(2)请求说明
需要用到的信息有:
请求URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior
Header格式:Content-Type:application/x-www-form-urlencoded
Body中放置请求参数,参数详情如下:
(3)返回参数
返回示例
{
"person_num": 1,
"person_info": [{
"attributes": {
"cellphone": {
"threshold": 0.9,
"score": 0.500098466873169
},
"both_hands_leaving_wheel": {
"threshold": 0.9,
"score": 0.468360424041748
},
"not_facing_front": {
"threshold": 0.9,
"score": 0.08260071277618408
},
"not_buckling_up": {
"threshold": 0.9,
"score": 0.998087465763092
},
"smoke": {
"threshold": 0.9,
"score": 6.29425048828125e-05
}
},
"location": {
"width": 483,
"top": 5,
"height": 238,
"left": 8
}
}],
"log_id": 2320165720061799596
}
2.获取access_token
# encoding:utf-8
import base64
import urllib
import urllib2
request_url = " https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior "
# 二进制方式打开视频文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())
params = {"data": data }
params = urllib.urlencode(params)
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
request = urllib2.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
content = response.read()
if content:
print content
三.识别结果
- 正面
识别结果:
- 侧面
识别结果:
3.侧后方
识别结果:
结论:
识别结果方面:分别从驾驶员前方、侧方、后方几个角度拍摄的照片进行测试,识别结果比较准确。并且能够识别出同一人的多种违规行为。
四.源码共享
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib
import urllib.parse
import urllib.request
import base64
import json
import time
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id = '******************'
client_secret = '**********************'
#获取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.decode("utf-8"))
token_key = token_info['access_token']
return token_key
# 读取图片
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
#获取驾驶行为信息
def get_license_plate(path):
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior"
f = get_file_content(path)
access_token=get_token()
img = base64.b64encode(f)
params = {"image": img}
params = urllib.parse.urlencode(params).encode('utf-8')
request_url = request_url + "?access_token=" + access_token
tic = time.clock()
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()
toc = time.clock()
print('处理时长: '+'%.2f' %(toc - tic) +' s')
if content:
driver_behavior = json.loads(content.decode("utf-8"))
strover = '识别结果:\n '
result = driver_behavior['person_info'][0]['attributes']
#使用手机
score = result['cellphone']['score']
strover += '使用手机: {} \n '.format(score)
#抽烟
score = result['smoke']['score']
strover += '抽烟: {} \n '.format(score)
#未系安全带
score = result['not_buckling_up']['score']
strover += '未系安全带: {} \n '.format(score)
#双手离开方向盘
score = result['both_hands_leaving_wheel']['score']
strover += '双手离开方向盘: {} \n '.format(score)
#视角未看前方
score = result['not_facing_front']['score']
strover += '视角未看前方: {} \n '.format(score)
print (strover)
return content
else:
return ''
image_path='F:\paddle\car\ss0.png'
get_license_plate(image_path)