百度AI攻略:驾驶行为识别

1.功能描述:

针对车载场景,识别驾驶员使用手机、抽烟、不系安全带、双手离开方向盘等动作姿态,分析预警危险驾驶行为,提升行车安全性

2.平台接入

具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:
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()
    #print (token_content)
    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#/Body-API/2387dd4f

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

大家需要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior
图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M。图片的base64编码是不包含图片头的,如(data:image/jpg;base64,),支持图片格式:jpg、bmp、png,最短边至少50px,最长边最大4096px

Python3调用代码如下:

#画出驾驶行为识别结果
def driver_result(originfilename,persons,resultfilename,fontsize,fontcolor):
    from PIL import Image, ImageDraw,ImageFont

    image_origin = Image.open(originfilename)
    draw =ImageDraw.Draw(image_origin)
    setFont = ImageFont.truetype('C:/windows/fonts/simfang.ttf', fontsize)
    for person in persons:
        warning=''
        result=''
        attributes=person['attributes']
        #使用手机
        score = attributes['cellphone']['score']
        threshold = attributes['cellphone']['threshold']
        if score>threshold:
            warning=warning+'使用手机 '
        result=result+('使用手机: {:.5f} \n'.format(score))
        #抽烟
        score = attributes['smoke']['score']
        threshold = attributes['smoke']['threshold']
        if score>threshold:
            warning=warning+'抽烟 '
        result=result+( '抽烟: {:.5f} \n'.format(score))
        #未系安全带
        score = attributes['not_buckling_up']['score']
        threshold = attributes['not_buckling_up']['threshold']
        if score>threshold:
            warning=warning+'未系安全带 '
        result=result+( '未系安全带: {:.5f} \n'.format(score))
        #双手离开方向盘
        score = attributes['both_hands_leaving_wheel']['score']
        threshold = attributes['both_hands_leaving_wheel']['threshold']
        if score>threshold:
            warning=warning+'双手离开方向盘 '
        result=result+( '双手离开方向盘: {:.5f} \n'.format(score))
        #视角未看前方
        score = attributes['not_facing_front']['score']
        threshold = attributes['not_facing_front']['threshold']
        if score>threshold:
            warning=warning+'视角未看前方 '
        result=result+( '视角未看前方: {:.5f} \n'.format(score))
        
        gesture = person['location'] 
        draw.rectangle((gesture['left'],gesture['top'],gesture['left']+gesture['width'],gesture['top']+gesture['height']),outline = "red")
        
        if warning=='':
            warning='无'
        result=result+ '需要警告内容:'+warning
        print(result)
        draw.text((gesture['left'],gesture['top']), result,font=setFont,fill=fontcolor)
        
    image_origin.save(resultfilename, "JPEG")
      
#驾驶行为识别
#filename:原图片名(本地存储包括路径)
def driver_behavior(filename,resultfilename,fontsize,color):
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior"
    print(filename)
    # 二进制方式打开图片文件
    f = open(filename, 'rb')
    img = base64.b64encode(f.read())
    
    params = dict()
    params['image'] = img
    params = urllib.parse.urlencode(params).encode("utf-8")
    #params = json.dumps(params).encode('utf-8')
    
    access_token = get_token()
    begin = time.perf_counter()
    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()
    end = time.perf_counter()

    print('处理时长:'+'%.2f'%(end-begin)+'秒')
    if content:
        #print(content)
        content=content.decode('utf-8')
        #print(content)
        data = json.loads(content)
        print('人数:',data['person_num'])
        #print(data)
        result=data['person_info']
        
        driver_result(filename,result,resultfilename,fontsize,color)

driver_behavior('../img/driver8.jpg','../img/driver8_result.jpg',20,'blue')


4.功能评测:
选用不同的数据对效果进行测试,具体效果如下(以下例子均来自网上):

先测试正常的驾驶行为:

处理时长:0.64秒
人数: 1
使用手机: 0.00006
抽烟: 0.00007
未系安全带: 0.00207
双手离开方向盘: 0.00002
视角未看前方: 0.00255
需要警告内容:无

处理时长:0.58秒
人数: 1
使用手机: 0.00019
抽烟: 0.00015
未系安全带: 0.04759
双手离开方向盘: 0.00264
视角未看前方: 0.00234
需要警告内容:无

然后是各种危险驾驶行为:

处理时长:0.68秒
人数: 1
使用手机: 0.92399
抽烟: 0.00590
未系安全带: 0.00678
双手离开方向盘: 0.00087
视角未看前方: 0.21933
需要警告内容:使用手机

处理时长:0.47秒
人数: 1
使用手机: 0.99724
抽烟: 0.01933
未系安全带: 0.05710
双手离开方向盘: 0.01490
视角未看前方: 0.20637
需要警告内容:使用手机

处理时长:0.31秒
人数: 1
使用手机: 0.00339
抽烟: 0.45544
未系安全带: 0.33805
双手离开方向盘: 0.09973
视角未看前方: 0.24955
需要警告内容:抽烟

处理时长:1.23秒
人数: 1
使用手机: 0.01052
抽烟: 0.00131
未系安全带: 0.96246
双手离开方向盘: 0.99859
视角未看前方: 0.91556
需要警告内容:未系安全带 双手离开方向盘 视角未看前方

 

5.测试结论和建议

测试下来,整体识别效果不错。对于危险驾驶行为有很强的识别能力,效果很好,速度也很快。可以对实时监控车内情况,识别驾驶员抽烟、使用手机、未系安全带等危险行为,及时预警,降低事故发生率,保障人身财产安全。

建议:
1,现在识别场景限制比较多,可以针对这方面进行优化,增加功能的应用场景。
2,可以考虑增加对人的情绪,疲劳都等方面的识别及警告。

  • 4
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值