百度AI攻略:駕駛行爲檢測

1.功能描述:python

針對車載場景,識別駕駛員使用手機、抽菸、不繫安全帶、雙手離開方向盤等動做姿態,分析預警危險駕駛行爲,提高行車安全性json

2.平臺接入windows

具體接入方式比較簡單,能夠參考個人另外一個帖子,這裏就不重複了:安全

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

3.調用攻略(Python3)及評測測試

3.1首先認證受權:優化

在開始調用任何API以前須要先進行認證受權,具體的說明請參考:編碼

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

具體Python3代碼以下:3d

# -*- 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,能夠考慮增長對人的情緒,疲勞都等方面的識別及警告。

相關文章
相關標籤/搜索