python模仿百度云桌面_利用百度云接口实现车牌识别!人称Python调包侠!
一個(gè)小需求---實(shí)現(xiàn)車牌識別。
目前有兩個(gè)想法
1. 調(diào)云在線的接口或者使用SDK做開發(fā)(配置環(huán)境和編譯第三方庫很麻煩,當(dāng)然使用python可以避免這些問題)
2. 自己實(shí)現(xiàn)車牌識別算法(復(fù)雜)
一開始準(zhǔn)備使用百度云文字識別C++ SDK來做,發(fā)現(xiàn)需要準(zhǔn)備curl、jsoncpp和OpenCV,并且curl和jsoncpp需要自己編譯,很麻煩,所以換用了python來做,真的是順暢簡單。
1. 安裝python環(huán)境(我用python3.7)
免費(fèi)資源共享,群內(nèi)解答技術(shù)難題交流群:626017123
打開安裝包無腦安裝即可。安裝好之后,看一下是否安裝成功。
cmd
python --version
2. 百度云SDK下載安裝及創(chuàng)建應(yīng)用
查看pip版本(python環(huán)境自帶,但是要注意版本)
pip --version
如果版本不合適,那么自行升級pip
pip install -U pip
安裝baidu-aip
pip install baidu-aip
(安裝成功的樣子)
現(xiàn)在我們的百度云SDK就安裝好了,下來 創(chuàng)建應(yīng)用
登錄百度云(沒賬號注冊一下)
創(chuàng)建應(yīng)用
自己填一下
現(xiàn)在我們就創(chuàng)建好了車牌識別的應(yīng)用,點(diǎn)擊應(yīng)用列表可查看。
這里的APPID、API KEY、Secret Key要在代碼中使用。 (注意不要泄漏)
3. 編碼調(diào)接口,實(shí)現(xiàn)需求
python代碼實(shí)現(xiàn)
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 """ 調(diào)用車牌識別 """
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庫"""
創(chuàng)建客戶端
""" APPID AK SK 自己創(chuàng)建的應(yīng)用中的數(shù)據(jù)"""
APP_ID = '*******'
API_KEY = '***************'
SECRET_KEY = '******************'
""" create client """
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
調(diào)用接口得到車牌識別結(jié)果
image = get_file_content(filePath)
""" 調(diào)用車牌識別 """
res = client.licensePlate(image)
這里的res是一個(gè)json對象/一個(gè)dict
例子
{
"log_id": 3583925545,
"words_result": {
"color": "blue",
"number": "蘇HS7766"
}
}
可以使用res['listname']['listname']形式獲取字典數(shù)據(jù)
print('車牌號碼:' + res['words_result']['number'])
print('車牌顏色:' + res['words_result']['color'])
至此,我們就實(shí)現(xiàn)了使用百度云SDK,通過編寫python代碼調(diào)用接口的車牌識別需求。
總結(jié)
以上是生活随笔為你收集整理的python模仿百度云桌面_利用百度云接口实现车牌识别!人称Python调包侠!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 字符串去重且相同字符最多出
- 下一篇: zabbix中mysql连不上的排错_z