生活随笔
收集整理的這篇文章主要介紹了
快速上手百度大脑驾驶行为分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者: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-8import base64import urllibimport urllib2request_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_tokenrequest = 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_secretrequest = 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_tokentic = 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 contentelse:return ''image_path='F:\paddle\car\ss0.png'
get_license_plate(image_path)
總結
以上是生活随笔為你收集整理的快速上手百度大脑驾驶行为分析的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。