python视频车流量计数_【使用攻略】【图像识别】车流量统计(动态版)

Ai技术的发展,催生了智慧城市,智慧交通是其中很重要的一环。百度AI车流量统计(动态版)能够检测图片中所有机动车辆,返回每辆车的类型和坐标位置,以及运动轨迹。应用于智慧交通领域,可以很方便地监控城市道路交通情况,实现交通拥堵预警。利用大数据挖掘交通流量规律,可以对车流进行合理疏导。另外,对一些限行城市,还可以通过车牌识别、车型识别等技术,对违规车辆进行监控。

一.平台接入

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

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

二.接口API分析

1.接口API:

https://ai.baidu.com/docs#/ImageClassify-API/0d270ea1

(1)接口描述

根据传入的连续视频图片序列,进行车辆检测和追踪,返回每个车辆的坐标位置、车辆类型(包括小汽车、卡车、巴士、摩托车、三轮车5大类)。在原图中指定区域,根据车辆轨迹判断驶入/驶出区域的行为,统计各类车辆的区域进出车流量,可返回含统计值和跟踪框的渲染图。

注:邀测的接口,不能直接在控制台调用,可通过提交工单申请开通测试权限。

(2)请求说明

需要用到的信息有:

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

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

(3)返回示例

检测到2辆小汽车、1辆卡车,3条轨迹,1辆卡车离开区域:

{

"vehicle_num":

{

"car":2,

"truck":1,

...

"tricycle":0

},

"vehicle_info":

[

{

"ID":3

"location":

{

"left": 100,

"top": 200,

"width": 200,

"height": 400,

}

"type": "car"

},

{

"ID": 5

"location":

{

"left": 400,

"top": 200,

"width": 200,

"height": 400,

}

"type": "car"

},

{

"ID": 6

"location":

{

"left": 600,

"top": 200,

"width": 300,

"height": 400,

}

"type": "truck"

}

],

“vehicle_count”:

{

"car":

{

"in":0,

"out":0

},

"truck":

{

"in":0

"out":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

三.识别结果

识别结果方面:选取一段渲染后的输出图片,结果只能用“牛X”来形容。车辆识别结果比较准确,车辆分类基本正确。对远处车辆识别程度不高,当然这里可能受图片质量是否清晰等因素影响。

处理速度方面:每张图片处理时间在1-2s,相对于视频来说,略有卡顿。

四.源码共享

# -*- coding: utf-8 -*-

#!/usr/bin/env python

import os

import requests

import base64

import json

from pprint import pprint

import time

import io

from io import BytesIO

import cv2

import numpy as np

from PIL import Image

import glob

#client_id 为官网获取的AK, client_secret 为官网获取的SK

api_key = '77ibV7Kvyo8bMh3FovLzxc6D'

secret_key = 'EdKVNt56ce1FUIdB0oCZYHwO3uPKSiEz'

class Traffic_flowRecognizer(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/traffic_flow' + '?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):

###对视频进行抽帧后,连续读取图片

WSI_MASK_PATH = 'E:/cheliu/chouzhen/'#存放图片的文件夹路径

paths = glob.glob(os.path.join(WSI_MASK_PATH, '*.jpg'))

paths.sort()

data_list = []

c = 1

for path in paths:

f = open(path, 'rb')

img_str = base64.b64encode(f.read())

data_list.append(img_str)

params = {'area':'1,269,400,269,400,180,1,180','case_id':1214,'case_init':'false','image':data_list,'show':'true'}

tic = time.clock()

rp_json = self.get_result(params)

toc = time.clock()

print('单次处理时长: '+'%.2f' %(toc - tic) +' s')

img_b64encode = rp_json['image']

img_b64decode = base64.b64decode(img_b64encode) # base64解码

#显示检测结果图片

image = io.BytesIO(img_b64decode)

img = Image.open(image)

img.show()

#存储检测结果图片

file=open('E:/cheliu/out/'+str(c)+'.jpg','wb')

file.write(img_b64decode)

file.close()

c = c + 1

if __name__ == '__main__':

recognizer = Traffic_flowRecognizer(api_key, secret_key)

recognizer.detect()

五.意见建议

1.目前API接口支持传入图片流,视频预处理比较复杂,后续是否有直接传入视频流的接口,这样的话对用户会更加友好。

2.对输入图片质量还要有一定要求,由于拍摄场景的原因,距离远、夜晚光线不好等造成图片模糊,均会影响识别效果,所以调用接口需要对抓拍图片进行筛选,建议明确图片质量要求。

3.渲染后输出的图片,车辆信息文本输出格式建议优化,如前面图示,输出文本仅打印出一部分,而且分的太开,颜色也不明显。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值