一個小需求---實現車牌識別。html
目前有兩個想法python
1. 調雲在線的接口或者使用SDK作開發(配置環境和編譯第三方庫很麻煩,固然使用python能夠避免這些問題)算法
2. 本身實現車牌識別算法(複雜)json
一開始準備使用百度雲文字識別C++ SDK來作,發現須要準備curl、jsoncpp和OpenCV,而且curl和jsoncpp須要本身編譯,很麻煩,因此換用了python來作,真的是順暢簡單。curl
python官網下載地址:https://www.python.org/downloads/release/python-374/ 建議直接下載安裝版installer(看對系統和位數)編碼
打開安裝包無腦安裝便可。安裝好以後,看一下是否安裝成功。url
cmdspa
python --version
參考https://cloud.baidu.com/doc/OCR/s/pjwvxzmtc文檔,安裝python SDK3d
查看pip版本(python環境自帶,可是要注意版本)code
pip --version
若是版本不合適,那麼自行升級pip
pip install -U pip
安裝baidu-aip
pip install baidu-aip
(安裝成功的樣子)
如今咱們的百度雲SDK就安裝好了,下來建立應用
登陸百度雲(沒帳號註冊一下)
建立應用
本身填一下
如今咱們就建立好了車牌識別的應用,點擊應用列表可查看。
這裏的APPID、API KEY、Secret Key要在代碼中使用。(注意不要泄漏)
python代碼實現
1 ''' 2 Statement 3 1. using the file 4 2. prepare a image path and call func "get_license_plate(filePath)" 5 3. you can get a json object 6 4. get the info from the pbject 7 example : 8 { 9 "log_id": 3583925545, 10 "words_result": { 11 "color": "blue", 12 "number": "蘇HS7766" 13 } 14 } 15 ''' 16 17 from aip import AipOcr 18 import json 19 20 """get img""" 21 def get_file_content(filePath): 22 with open(filePath, 'rb') as fp: 23 return fp.read() 24 25 """ get licsense plate """ 26 def get_license_plate(filePath): 27 """ APPID AK SK """ 28 APP_ID = '********' 29 API_KEY = '**************' 30 SECRET_KEY = '******************' 31 32 """ create client """ 33 client = AipOcr(APP_ID, API_KEY, SECRET_KEY) 34 35 image = get_file_content(filePath) 36 37 """ 調用車牌識別 """ 38 res = client.licensePlate(image) 39 return res 40 41 42 """ call example """ 43 str = 'C:\\Users\\***\\Desktop\\big.jpg' """ 照片絕對地址 """ 44 res = get_license_plate(str) 45 print('車牌號碼:' + res['words_result']['number']) 46 print('車牌顏色:' + res['words_result']['color'])
代碼分解
引入庫
from aip import AipOcr """百度雲SDK""" import json """json庫"""
建立客戶端
""" APPID AK SK 本身建立的應用中的數據""" APP_ID = '*******' API_KEY = '***************' SECRET_KEY = '******************' """ create client """ client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
調用接口獲得車牌識別結果
image = get_file_content(filePath) """ 調用車牌識別 """ res = client.licensePlate(image)
這裏的res是一個json對象/一個dict
例子
{ "log_id": 3583925545, "words_result": { "color": "blue", "number": "蘇HS7766" } }
可使用res['listname']['listname']形式獲取字典數據
print('車牌號碼:' + res['words_result']['number']) print('車牌顏色:' + res['words_result']['color'])
至此,咱們就實現了使用百度雲SDK,經過編寫python代碼調用接口的車牌識別需求。
【1】百度雲API文檔:https://cloud.baidu.com/doc/OCR/s/pjwvxzmtc/
【2】python pip安裝與使用:https://www.runoob.com/w3cnote/python-pip-install-usage.html
【3】python官網:https://www.python.org/downloads/release/python-374/
原文出處:https://www.cnblogs.com/yocichen/p/11621781.html