名片识别信息分类python_python体验名片识别OCR
我想使用名片識(shí)別OCR,主要研究了騰訊云的智能映像和騰訊Youtu.
解析后的漢字直接是unicode,例如u'\ u90e8 \ u95e8 \ u526f \ u603b \ u7ecf \ u7406',根據(jù)騰訊的文檔,代碼經(jīng)驗(yàn):
首先,介紹一下.
import time
import random
import hmac, hashlib
import binascii
import base64
import requests
import os
import json
然后根據(jù)文檔生成簽名(簽名權(quán)),此步驟不是很順利. 在需要替換的位置中,自己查找文件名片識(shí)別服務(wù),例如appid,secret_id,secret_key. 至于水桶,我不明白它的用途.
appid = 'your app id' #自己填
secret_id = 'your secret id' #自己填
secret_key = 'your secret key' #自己填
bucket= 'seem no use' #自己填
# 還有個(gè)https的,我沒試
url = 'http://recognition.image.myqcloud.com/ocr/businesscard'
now = int(time.time())
rdm = random.randint(0,9999999999)
expired = now + 2592000 #一個(gè)月
plain_text = 'a='+appid+'&b='+bucket+'&k='+secret_id+'&e='+str(expired)+'&t='+str(now)+'&r='+str(rdm)+'&u=0&f='
bin = hmac.new(secret_key.encode(), plain_text.encode(), hashlib.sha1)
s = bin.hexdigest()
s = binascii.unhexlify(s)
s = s + plain_text.encode('ascii')
signature = base64.b64encode(s).rstrip() # 生成簽名
print(signature)
接下來,有兩種情況. 如果要識(shí)別的圖像是URL,則相對(duì)簡單. 設(shè)置標(biāo)題和數(shù)據(jù)以發(fā)送請(qǐng)求. 圖片網(wǎng)址以Utop為例. 只需打印出結(jié)果即可.
headers = {
'Authorization': signature,
'content-type': 'application/json',
}
data = {
'appid': appid,
'url_list': ['http://yoututest-1251966477.cossh.myqcloud.com/mingpian.jpg']
}
r = requests.post(url, headers=headers, data = json.dumps(data))
ret = r.json()
print(ret)
因?yàn)槲也皇煜?#39;content-type'的請(qǐng)求: 'multipart / form-data',所以研究了很長時(shí)間名片識(shí)別服務(wù),后來發(fā)現(xiàn)它實(shí)際上很簡單.
headers = {
'Authorization': signature,
# 不用設(shè)置content-type!!!
}
files = {
'appid': appid,
# 可以多張圖片,但是必須image開頭,路徑自己設(shè)置好
'image[0]': ( 'image[0].jpeg', open(os.path.abspath('mingpian0.jpeg'), 'rb'), 'image/jpeg', {}),
# 'image[1]': ( 'image[1].jpeg', open(os.path.abspath('mingpian1.jpeg'), 'rb'), 'image/jpeg', {}),
}
r = requests.post(url, headers=headers, files=files)
ret = r.json()
print(ret)
您是否發(fā)現(xiàn)該水桶根本沒有用過,沒有從我的賬戶中扣除任何錢?也許是因?yàn)轵v訊云提前付款并在下個(gè)月扣除了這筆錢嗎?等一下.
Youtube的演示做得很好,吸引了我,但是關(guān)于識(shí)別結(jié)果,有幾點(diǎn)需要注意: 首先,返回的結(jié)果很多,可能是一些原始數(shù)據(jù). 第二,我沒有得到漢字的結(jié)果編碼. 它以\ x開頭. 在這里,我仍然會(huì)體驗(yàn)到它,參考文檔,它與上面的騰訊云沒有太大區(qū)別.
順便說一下,Youtu演示具有python代碼. 我也從這里的簡化中借用了. 每個(gè)人都知道,如果我不再次敲它,我會(huì)認(rèn)為那不是真的.
首先介紹一堆.
# import 跟上面一樣吧
簽名部分也相似,但是存儲(chǔ)桶已經(jīng)成為申請(qǐng)Youtu的qq號(hào),隨便寫這個(gè)字段似乎也可以.
appid = 'your app id' # 在優(yōu)圖申請(qǐng)應(yīng)用后就得到
secret_id = 'your secret id' # 在優(yōu)圖申請(qǐng)應(yīng)用后就得到
secret_key = 'your secret key' # 在優(yōu)圖申請(qǐng)應(yīng)用后就得到
userid= 'your qq number'
url = 'http://api.youtu.qq.com/youtu/ocrapi/bcocr'
now = int(time.time())
rdm = random.randint(0,9999999999)
expired = now + 2592000
plain_text = 'u=' + userid + '&a=' + appid + '&k=' + secret_id + '&e=' + str(expired) + '&t=' + str(now) + '&r=' + str(rdm) + '&f='
bin = hmac.new(secret_key.encode(), plain_text.encode(), hashlib.sha1)
s = bin.hexdigest()
s = binascii.unhexlify(s)
s = s + plain_text.encode('ascii')
signature = base64.b64encode(s).rstrip() # 生成簽名
print(signature)
還有兩種情況,使用url和image,但是Utop的image方法實(shí)際上被轉(zhuǎn)換為base64字符串,這相對(duì)簡單.
headers = {
'Authorization': signature,
'Content-Type': 'text/json',
}
data = {
'app_id': appid,
'session_id': '',
}
# 下面自行選擇字段
data['url'] = 'http://yoututest-1251966477.cossh.myqcloud.com/mingpian.jpg'
data["image"] = base64.b64encode(open(os.path.abspath('mingpian.jpeg'), 'rb').read()).rstrip().decode('utf-8')
r = requests.post(url, headers=headers, data = json.dumps(data))
ret = r.json()
print(ret)
我不會(huì)發(fā)布返回的結(jié)果.
本文來自電腦雜談,轉(zhuǎn)載請(qǐng)注明本文網(wǎng)址:
http://www.pc-fly.com/a/ruanjian/article-208056-1.html
總結(jié)
以上是生活随笔為你收集整理的名片识别信息分类python_python体验名片识别OCR的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab保存数据到excel_Exc
- 下一篇: 黄金分割小数点后100位小数的c语言编程