【使用攻略】【图像识别】安全带抓拍
wangwei8638 发布于2019-05 浏览:7309 回复:10
1
收藏
最后编辑于2022-04

开车如果不知道“海燕系统”,恐怕你要被罚款罚到腿软了。

以往在电影中出现的场景,已经在“海燕系统”中实现了。“海燕系统”不仅能完成智能辨认抓拍,还能对采集来的大数据进行二次分析,识别出车辆的车标、细分车型、车身颜色、年检标、车摆物、是否系安全带等一系列细节信息。利用百度提供的API接口,我们可以搭建自己的“海燕系统”,本文先从利用车辆属性识别API,识别司机是否系安全带说起。

一.平台接入

此步骤比较简单,不多阐述。可参照之前文档:

https://ai.baidu.com/forum/topic/show/943028

二.分析接口文档

1. 接口API

https://ai.baidu.com/docs#/ImageClassify-API/29196fe3

   (1)接口描述

检测图片中所有车辆,返回每辆车的类型和坐标位置,可识别小汽车、卡车、巴士、摩托车、三轮车、自行车6大类车辆,并针对小汽车识别11种外观属性,包括:是否有车窗雨眉、是否有车顶架、副驾驶是否有人、驾驶位是否系安全带、遮阳板是否放下、车辆朝向等。

 (2)请求说明

需要用到的信息有:

请求URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_attr

Header格式:Content-Type:application/x-www-form-urlencoded

请求参数:

附:type字段说明

(3)返回示例

{'log_id': 8296612782826163523,
 'vehicle_info': [{'attributes': 
{'copilot': {'score': 0.9637151956558228},
 'copilot_belt': {'score': 0.2371468544006348},
'copilot_visor': {'score': 0.01172924041748047},
'direction': {'name': '正前方',
'score': 0.7401106357574463},
'driver_belt': {'score': 0.7797645330429077},
'driver_visor': {'score': 0.001996934413909912},
'in_car_item': {'score': 0.9681501984596252},
'rearview_item': {'score': 0.9463440775871277},
 'roof_rack': {'score': 0.08109265565872192},
 'skylight': {'score': 0.9997358918190002},
 'vehicle_type': {'name': '小汽车',
'score': 0.9998549222946167},
'window_rain_eyebrow': {'score': 0.0002390742301940918}},
'location': {'height': 386,
                                'left': 316,
                                'top': 153,
                                'width': 381}}],
 'vehicle_num': 1}

2.获取access_token

#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()
if token_content:
token_info = json.loads(token_content.decode("utf-8"))
token_key = token_info['access_token']
return token_key


三.识别结果

识别结果:

车辆个数: 1
是否系安全带: 0.7797645330429077

花费时长: 1.76 s

识别结果方面:可以看出,识别结果还是不错。识别出了驾驶位系了安全带,副驾驶位有人且未系安全带。

处理速度方面:处理时间1.76s,可以接受。

四.源码共享

# -*- coding: utf-8 -*-
#!/usr/bin/env python

import os
import requests
import base64
import json
from pprint import pprint
import time
#client_id 为官网获取的AK, client_secret 为官网获取的SK
api_key = '****************'
secret_key = '******************'

class LandmarkRecognizer(object):
    def __init__(self, api_key, secret_key):
        self.access_token = self._get_access_token(api_key=api_key, secret_key=secret_key)
        self.API_URL = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_attr' + '?access_token=' \
                      + self.access_token
    #获取token
    @staticmethod
    def _get_access_token(api_key, secret_key):
        api = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \
            '&client_id={}&client_secret={}'.format(api_key, secret_key)
        rp = requests.post(api)
        if rp.ok:
            rp_json = rp.json()
#            print(rp_json['access_token'])
            return rp_json['access_token']
        else:
            print('=> Error in get access token!')
    def get_result(self, params):
        rp = requests.post(self.API_URL, data=params)
        if rp.ok:
#            print('=> Success! got result: ')
            rp_json = rp.json()
            pprint(rp_json)
            return rp_json
        else:
            print('=> Error! token invalid or network error!')
            print(rp.content)
            return None
    #识别车辆属性
    def detect(self, img_path):
        f = open(img_path, 'rb')
        strover = '识别结果:'
        img_str = base64.b64encode(f.read())
        params = {'image': img_str}
        tic = time.clock()
        rp_json = self.get_result(params)
        toc = time.clock()
#        pprint(rp_json)       
        vehicle_num = rp_json['vehicle_num']
        strover += '车辆个数:  {} \n '.format(vehicle_num)
        vehicle_info = rp_json['vehicle_info']
        strover += '是否系安全带:  {} \n '.format(vehicle_info[0]['attributes']['driver_belt']['score'])
        print(strover)
        print('花费时长: '+'%.2f'  %(toc - tic) +' s')

if __name__ == '__main__':
    recognizer = LandmarkRecognizer(api_key, secret_key)
    img = 'F:\paddle\s9.jpeg'
    recognizer.detect(img)

五.意见建议

       尝试了多张图片,总体识别准确率不高。由于拍摄场景的原因,距离远、车前玻璃反光、夜晚光线不好等造成图片模糊,均会影响识别效果,所以调用接口需要对抓拍图片进行筛选,建议API接口添加图片质量控制参数。

收藏
点赞
1
个赞
共10条回复 最后由用户已被禁言回复于2022-04
#11doubi渣渣回复于2021-01
#10 wangwei8638回复
细节都能看清

那感觉训练预测的时候都要切图了

0
#10wangwei8638回复于2021-01
#9 doubi渣渣回复
想必高清摄像头分辨率是非常大的……666

细节都能看清

0
#9doubi渣渣回复于2021-01
#8 wangwei8638回复
嗯呐,目标检测是基础

想必高清摄像头分辨率是非常大的……666

0
#8wangwei8638回复于2021-01
#7 doubi渣渣回复
这个场景,也许目标检测更合适?

嗯呐,目标检测是基础

0
#7doubi渣渣回复于2020-12

这个场景,也许目标检测更合适?

0
#6wangwei8638回复于2020-12
#5 深圳司灏刘先生回复
现在安全带检测都用什么网络 来做啊

只要有网络就可以啊,互联网或者移动互联网

0
#5深圳司灏刘先生回复于2020-12

现在安全带检测都用什么网络 来做啊

0
#4wangwei8638回复于2019-08

海燕系统真的牛

0
#3wangwei8638回复于2019-05

gif无法上传

0
#2wangwei8638回复于2019-05

未完待续

0
TOP
切换版块