您的位置:首页 > 大数据 > 人工智能

百度AI攻略:车牌识别

2020-02-04 06:44 357 查看

1.功能描述:

支持对中国大陆机动车车牌的识别,包括地域编号和车牌号

2.平台接入

具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:
http://ai.baidu.com/forum/topic/show/943327

3.调用攻略(Python3)及评测

3.1首先认证授权:

在开始调用任何API之前需要先进行认证授权,具体的说明请参考:

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

具体Python3代码如下:

# -*- 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()
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#/OCR-API/5116ac95

说明的比较清晰,这里就不重复了。

大家需要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate
图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式

Python3调用代码如下:

[code]#画出车牌识别结果
def draw_plate(draw,plate):
i=0
for point in plate['vertexes_location']:
if i==0:
start_x=point['x']
start_y=point['y']
origin_x=point['x']
origin_y=point['y']
#draw.text((start_x,start_y), plate['number'] ,plate['color'])
else:
draw.line((start_x, start_y, point['x'], point['y']), 'red')
start_x=point['x']
start_y=point['y']
i=i+1
draw.line((start_x, start_y, origin_x, origin_y), 'red')
return draw

def draw_plates(originfilename,plates,resultfilename,multi_detect):
from PIL import Image, ImageDraw

image_origin = Image.open(originfilename)
draw =ImageDraw.Draw(image_origin)
if multi_detect=='false':
#print (plate)
draw_plate(draw,plates)
else:
for plate in plates:
#print (plate)
draw_plate(draw,plate)

#draw.line((0,0) +Image1.size, fill=128)
#image_origin.show()
image_origin.save(resultfilename, "JPEG")

#车牌号
#filename:图片名(本地存储包括路径),
#multi_detect是否检测多张车牌,默认为false,当置为true的时候可以对一张图片内的多张车牌进行识别
def license_plate(filename,resultfilename,multi_detect='false'):
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate"

# 二进制方式打开图片文件
f = open(filename, 'rb')
img = base64.b64encode(f.read())

params = dict()
params['image'] = img
params['multi_detect'] = multi_detect
params = urllib.parse.urlencode(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)

words_result=data['words_result']
if multi_detect=='false':
print ('颜色',':',words_result['color'])
print ('车牌号',':',words_result['number'])
else:
for item in words_result:
print ('颜色',':',item['color'])
print ('车牌号',':',item['number'])
print ('-------------')
print(item['vertexes_location'])

draw_plates(filename,words_result,resultfilename,multi_detect)
license_plate('../img/plate3.jpg','../img/plate3_draw.jpg','false')


4.功能评测:
选用不同的数据对效果进行测试,具体效果如下(以下例子均来自网上):

蓝牌:

处理时长:2.52秒
颜色 : blue
车牌号 : 陕K75555

绿牌:

处理时长:1.44秒
颜色 : green
车牌号 : 冀FF01717

多张:

处理时长:0.94秒
颜色 : blue
车牌号 : 京NB2012
-------------
[{'y': 106, 'x': 26}, {'y': 135, 'x': 352}, {'y': 244, 'x': 345}, {'y': 216, 'x': 20}]
颜色 : blue
车牌号 : 京NB2013
-------------
[{'y': 367, 'x': 20}, {'y': 367, 'x': 371}, {'y': 473, 'x': 370}, {'y': 473, 'x': 20}]

5.测试结论和建议

测试下来,整体识别效果不错。对于车牌号有较强的识别能力,效果很好,速度也很快。可以广泛的应用于:
停车场闸机识别:在停车场的闸机上使用车牌识别,自动识别车牌号码实现无卡、无人的停车场管理,方便快捷
道路违章检测:在交通道路上的摄像头中加入车牌识别结合违章判断,对违章的车辆号码进行自动识别,实现自动化的违章审计

等领域,对于提高工作效率会有很大的帮助。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
才能我浪费 发布了76 篇原创文章 · 获赞 0 · 访问量 1297 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: