1.功能描述:
支持對中國大陸機(jī)動車車牌的識別配椭,包括地域編號和車牌號
2.平臺接入
具體接入方式比較簡單而克,可以參考我的另一個帖子菩彬,這里就不重復(fù)了:
http://ai.baidu.com/forum/topic/show/943327
3.調(diào)用攻略(Python3)及評測
3.1首先認(rèn)證授權(quán):
在開始調(diào)用任何API之前需要先進(jìn)行認(rèn)證授權(quán),具體的說明請參考:
http://ai.baidu.com/docs#/Auth/top
具體Python3代碼如下:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib
import base64
import json
#client_id 為官網(wǎng)獲取的AK舷蒲, client_secret 為官網(wǎng)獲取的SK
client_id =【百度云應(yīng)用的AK】
client_secret =【百度云應(yīng)用的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車牌號識別分析接口調(diào)用:
詳細(xì)說明請參考:?https://ai.baidu.com/docs#/OCR-API/5116ac95
說明的比較清晰趟佃,這里就不重復(fù)了扇谣。
大家需要注意的是:
API訪問URL:https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate
圖像數(shù)據(jù),base64編碼后進(jìn)行urlencode闲昭,要求base64編碼和urlencode后大小不超過4M罐寨,最短邊至少15px,最長邊最大4096px,支持jpg/jpeg/png/bmp格式
Python3調(diào)用代碼如下:
#畫出車牌識別結(jié)果
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是否檢測多張車牌鸯绿,默認(rèn)為false,當(dāng)置為true的時(shí)候可以對一張圖片內(nèi)的多張車牌進(jìn)行識別
def license_plate(filename,resultfilename,multi_detect='false'):
? ? request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate"
? ? # 二進(jìn)制方式打開圖片文件
? ? 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('處理時(shí)長:'+'%.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.功能評測:
選用不同的數(shù)據(jù)對效果進(jìn)行測試,具體效果如下(以下例子均來自網(wǎng)上):
藍(lán)牌:
處理時(shí)長:2.52秒
顏色 : blue
車牌號 : 陜K75555
綠牌:
處理時(shí)長:1.44秒
顏色 : green
車牌號 : 冀FF01717
多張:
處理時(shí)長: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.測試結(jié)論和建議
測試下來瓶蝴,整體識別效果不錯毒返。對于車牌號有較強(qiáng)的識別能力,效果很好舷手,速度也很快拧簸。可以廣泛的應(yīng)用于:
停車場閘機(jī)識別:在停車場的閘機(jī)上使用車牌識別男窟,自動識別車牌號碼實(shí)現(xiàn)無卡盆赤、無人的停車場管理,方便快捷
道路違章檢測:在交通道路上的攝像頭中加入車牌識別結(jié)合違章判斷歉眷,對違章的車輛號碼進(jìn)行自動識別牺六,實(shí)現(xiàn)自動化的違章審計(jì)
等領(lǐng)域,對于提高工作效率會有很大的幫助汗捡。