一個小需求---實現車牌識別。
目前有兩個想法
- 調雲在線的接口或者使用SDK做開發(配置環境和編譯第三方庫很麻煩,當然使用python可以避免這些問題)
- 自己實現車牌識別算法(複雜)
一開始準備使用百度雲文字識別C++ SDK來做,發現需要準備curl、jsoncpp和OpenCV,並且curl和jsoncpp需要自己編譯,很麻煩,所以換用了python來做,真的是順暢簡單。
- 安裝python環境(我用python3.7)
python官網下載地址:https://www.python.org/downloads/release/python-374/ 建議直接下載安裝版installer(看對系統和位數)
打開安裝包無腦安裝即可。安裝好之後,看一下是否安裝成功。
cmd
python --version
- 百度雲SDK下載安裝及創建應用
參考https://cloud.baidu.com/doc/OCR/s/pjwvxzmtc文檔,安裝python SDK
查看pip版本(python環境自帶,但是要注意版本)
pip --version
如果版本不合適,那麼自行升級pip
pip install -U pip
安裝baidu-aip
pip install baidu-aip
現在我們的百度雲SDK就安裝好了,下來創建應用
創建應用
自己填一下
現在我們就創建好了車牌識別的應用,點擊應用列表可查看。
這裡的APPID、API KEY、Secret Key要在代碼中使用。(注意不要洩漏)
- 編碼調接口,實現需求
python代碼實現
'''
Statement
1. using the file
2. prepare a image path and call func "get_license_plate(filePath)"
3. you can get a json object
4. get the info from the pbject
example :
{
"log_id": 3583925545,
"words_result": {
"color": "blue",
"number": "蘇HS7766"
}
}
'''
from aip import AipOcr
import json
"""get img"""
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
""" get licsense plate """
def get_license_plate(filePath):
""" 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)
return res
""" call example """
str = 'C:\\\\Users\\\\***\\\\Desktop\\\\big.jpg' """ 照片絕對地址 """
res = get_license_plate(str)
print('車牌號碼:' + res['words_result']['number'])
print('車牌顏色:' + res['words_result']['color'])
至此,我們就實現了使用百度雲SDK,通過編寫python代碼調用接口的車牌識別需求。
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20191007142811664.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,sha一個小需求---實現車牌識別。
目前有兩個想法
- 調雲在線的接口或者使用SDK做開發(配置環境和編譯第三方庫很麻煩,當然使用python可以避免這些問題)
- 自己實現車牌識別算法(複雜)
一開始準備使用百度雲文字識別C++ SDK來做,發現需要準備curl、jsoncpp和OpenCV,並且curl和jsoncpp需要自己編譯,很麻煩,所以換用了python來做,真的是順暢簡單。
- 安裝python環境(我用python3.7)
python官網下載地址:https://www.python.org/downloads/release/python-374/ 建議直接下載安裝版installer(看對系統和位數)
打開安裝包無腦安裝即可。安裝好之後,看一下是否安裝成功。
cmd
python --version
- 百度雲SDK下載安裝及創建應用
參考https://cloud.baidu.com/doc/OCR/s/pjwvxzmtc文檔,安裝python SDK
查看pip版本(python環境自帶,但是要注意版本)
pip --version
如果版本不合適,那麼自行升級pip
pip install -U pip
安裝baidu-aip
pip install baidu-aip
現在我們的百度雲SDK就安裝好了,下來創建應用
創建應用
自己填一下
現在我們就創建好了車牌識別的應用,點擊應用列表可查看。
這裡的APPID、API KEY、Secret Key要在代碼中使用。(注意不要洩漏)
- 編碼調接口,實現需求
python代碼實現
'''
Statement
1. using the file
2. prepare a image path and call func "get_license_plate(filePath)"
3. you can get a json object
4. get the info from the pbject
example :
{
"log_id": 3583925545,
"words_result": {
"color": "blue",
"number": "蘇HS7766"
}
}
'''
from aip import AipOcr
import json
"""get img"""
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
""" get licsense plate """
def get_license_plate(filePath):
""" 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)
return res
""" call example """
str = 'C:\\\\Users\\\\***\\\\Desktop\\\\big.jpg' """ 照片絕對地址 """
res = get_license_plate(str)
print('車牌號碼:' + res['words_result']['number'])
print('車牌顏色:' + res['words_result']['color'])
至此,我們就實現了使用百度雲SDK,通過編寫python代碼調用接口的車牌識別需求。
dow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZlaTM0Nzc5NTc5MA==,size_16,color_FFFFFF,t_70)
閱讀更多 嗨學python 的文章