借用百度智能云完成<驾驶行为识别>视频检测【python】

简介: 借用百度智能云完成<驾驶行为识别>视频检测【python】

一.API Key和Secret Key准备工作


1.1在百度智能云平台上注册账号,


1.2进入百度智能云管理中心:


1.3.汇集信息


驾驶行为分析
API Key:    【官网获取的AK】     
Secret Key: 【官网获取的SK】
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'



二.检测图像中的驾驶行为


2.1根据官网给的样例,更改自己的ak和sk,如下所示

def detect_image(frame):
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior"
    image = cv2.imencode('.jpg', frame)[1]
    img = str(base64.b64encode(image))[2:-1]
    params = {"image": img}
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
    response_host = requests.get(host)
    access_token = []
    useriddatas = json.loads(response_host.text)
    access_token.append(str(useriddatas["access_token"]))
    access = (''.join(access_token))
    access_token = access
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    data = response.json()
    datas = str(data)
    if len(datas) > 200: #防止检测过快无法触及并发,这里我是使用的免费的次数
        find_behavior(data)
    else:
        pass


2.2从response.json()中解析行为:


def find_behavior(data):
    '''
    smoke //吸烟,
    cellphone //打手机 ,
    not_buckling_up // 未系安全带,
    not_facing_front // 视角未看前方,
    yawning // 打哈欠,
    eyes_closed // 闭眼,
    head_lowered // 低头
    '''
    # print(data)
    data_smoke = data['person_info'][0]['attributes']['smoke']['score']
    data_cellphone = data['person_info'][0]['attributes']['cellphone']['score']
    data_not_buckling_up = data['person_info'][0]['attributes']['not_buckling_up']['score']
    data_not_facing_front = data['person_info'][0]['attributes']['not_facing_front']['score']
    data_yawning = data['person_info'][0]['attributes']['yawning']['score']
    data_eyes_closed = data['person_info'][0]['attributes']['eyes_closed']['score']
    data_head_lowered = data['person_info'][0]['attributes']['head_lowered']['score']
    All_behavior = [data_smoke,
                    data_cellphone,
                    data_not_buckling_up,
                    data_not_facing_front,
                    data_yawning,
                    data_eyes_closed,
                    data_head_lowered]
    All_behavior = np.array(All_behavior)  # 转格式
    Danger_behavior = np.sum(All_behavior >= 0.4)  # 存在危险驾驶行为(该行为概率大于0.4)的个数
    if Danger_behavior > 0:
        """
        from pygame import mixer     #控制音频流模块
        mixer.init()                 #初始化混音器模块
        mixer.music.load('./存在危险驾驶行为.mp3')  #载入待播放音乐文件 假定为2秒
        mixer.music.play()           #开始播放音乐流
        sleep(2)  #休眠2秒供播放使用
        mixer.music.stop()  # 停止播放
        """
        print("存在危险驾驶行为")
    else:
        print("安全驾驶中")



2.3.将图像检测怼到视频中

if __name__ == "__main__":
    capture = cv2.VideoCapture(0)
    fps = 0.0
    while (True):
        t1 = time.time()
        # 读取某一帧
        ref, frame = capture.read()
        # 格式转变,BGRtoRGB
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        # 转变成Image
        # 进行检测
        detect_image(frame)
        # time.sleep(0.2)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        # fps = int(round(capture.get(cv2.CAP_PROP_FPS)))
        fps = (fps + (1. / (time.time() - t1))) / 2
        frame = cv2.putText(frame, "fps= %.2f" % (fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.imshow("video", frame)
        c = cv2.waitKey(1) & 0xff
        if c == 27:
            capture.release()
            break
    capture.release()
    cv2.destroyAllWindows()



综上所述:完整代码demo:

# encoding:utf-8
import json
import time
import numpy as np
import requests
import base64
import cv2
'''
驾驶行为分析
API Key:    USIybtKpK5CB2B1o6RSyYYz1     
Secret Key: CFSlR51sTQnocifu8mmrn8sv7XNb5vwS
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
'''
def find_behavior(data):
    '''
    smoke //吸烟,
    cellphone //打手机 ,
    not_buckling_up // 未系安全带,
    not_facing_front // 视角未看前方,
    yawning // 打哈欠,
    eyes_closed // 闭眼,
    head_lowered // 低头
    '''
    # print(data)
    data_smoke = data['person_info'][0]['attributes']['smoke']['score']
    data_cellphone = data['person_info'][0]['attributes']['cellphone']['score']
    data_not_buckling_up = data['person_info'][0]['attributes']['not_buckling_up']['score']
    data_not_facing_front = data['person_info'][0]['attributes']['not_facing_front']['score']
    data_yawning = data['person_info'][0]['attributes']['yawning']['score']
    data_eyes_closed = data['person_info'][0]['attributes']['eyes_closed']['score']
    data_head_lowered = data['person_info'][0]['attributes']['head_lowered']['score']
    All_behavior = [data_smoke,
                    data_cellphone,
                    data_not_buckling_up,
                    data_not_facing_front,
                    data_yawning,
                    data_eyes_closed,
                    data_head_lowered]
    All_behavior = np.array(All_behavior)  # 转格式
    Danger_behavior = np.sum(All_behavior >= 0.4)  # 存在危险驾驶行为(该行为概率大于0.4)的个数
    if Danger_behavior > 0:
        """
        from pygame import mixer     #控制音频流模块
        mixer.init()                 #初始化混音器模块
        mixer.music.load('./存在危险驾驶行为.mp3')  #载入待播放音乐文件 假定为2秒
        mixer.music.play()           #开始播放音乐流
        sleep(2)  #休眠2秒供播放使用
        mixer.music.stop()  # 停止播放
        """
        print("存在危险驾驶行为")
    else:
        print("安全驾驶中")
def detect_image(frame):
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior"
    image = cv2.imencode('.jpg', frame)[1]
    img = str(base64.b64encode(image))[2:-1]
    params = {"image": img}
    host ='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
    response_host = requests.get(host)
    access_token = []
    useriddatas = json.loads(response_host.text)
    access_token.append(str(useriddatas["access_token"]))
    access = (''.join(access_token))
    access_token = access
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    data = response.json()
    datas = str(data)
    if len(datas) > 200:
        find_behavior(data)
    else:
        pass
if __name__ == "__main__":
    capture = cv2.VideoCapture(0)
    fps = 0.0
    while (True):
        t1 = time.time()
        # 读取某一帧
        ref, frame = capture.read()
        # 格式转变,BGRtoRGB
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        # 转变成Image
        # 进行检测
        detect_image(frame)
        # time.sleep(0.2)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        # fps = int(round(capture.get(cv2.CAP_PROP_FPS)))
        fps = (fps + (1. / (time.time() - t1))) / 2
        frame = cv2.putText(frame, "fps= %.2f" % (fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.imshow("video", frame)
        c = cv2.waitKey(1) & 0xff
        if c == 27:
            capture.release()
            break
    capture.release()
    cv2.destroyAllWindows()


相关文章
|
1月前
|
数据采集 Python
爬虫实战-Python爬取百度当天热搜内容
爬虫实战-Python爬取百度当天热搜内容
66 0
|
1月前
|
缓存 API 定位技术
使用Python调用百度地图API实现地址查询
使用Python调用百度地图API实现地址查询
98 0
|
2月前
|
缓存 监控 Python
在Python中,如何检测和处理内存泄漏?
【2月更文挑战第7天】【2月更文挑战第18篇】在Python中,如何检测和处理内存泄漏?
|
2月前
|
机器学习/深度学习 TensorFlow 算法框架/工具
Python 与机器学习:开启智能时代的大门
【2月更文挑战第6天】在当今数字化时代,Python作为一种高度灵活且功能强大的编程语言,与机器学习技术的结合为我们带来了前所未有的智能化解决方案。本文将介绍Python在机器学习领域的应用,并探讨其如何开启智能时代的大门。
|
2月前
|
监控 安全 自动驾驶
基于python的室内老人实时摔倒智能监测系统-跌倒检测系统(康复训练检测+代码)
基于python的室内老人实时摔倒智能监测系统-跌倒检测系统(康复训练检测+代码)
77 1
|
4天前
|
数据采集 存储 安全
python检测代理ip是否可用的方法
python检测代理ip是否可用的方法
|
27天前
|
数据采集 XML 程序员
揭秘YouTube视频世界:利用Python和Beautiful Soup的独特技术
本文介绍了如何使用Python和Beautiful Soup库抓取YouTube视频数据,包括标题、观看次数和点赞、踩的数量。通过亿牛云爬虫代理IP服务避免被网站屏蔽,提供代理服务器配置和请求头设置示例。代码可能需根据YouTube页面更新进行调整。
揭秘YouTube视频世界:利用Python和Beautiful Soup的独特技术
|
1月前
|
数据采集 JSON API
使用Python获取B站视频并在本地实现弹幕播放功能
使用Python获取B站视频并在本地实现弹幕播放功能
20 0
|
1月前
|
计算机视觉 Python
怎么使用Python轻松打造淘宝主图视频生成神器
怎么使用Python轻松打造淘宝主图视频生成神器
39 0
|
1月前
|
数据可视化 UED Python
用Python打造批量下载视频并能可视化下载进度的炫酷下载器
用Python打造批量下载视频并能可视化下载进度的炫酷下载器
32 0