关于python 使用腾讯云OCR 通用印刷体识别

时间:2023-12-04 18:03:08

腾讯的python SDK没有通用印刷体识别,所以参考了别人识别网上图片的方式:https://www.cnblogs.com/semishigure/p/7690789.html

但是咱们使用的基本都是识别本地图片,所以要采用image方式;

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import docx
import requests
import hmac
import hashlib
import base64
import time
import random
import re appid = "xxxxx"
bucket = "xxxxxx" #参考本文开头提供的链接
secret_id = "xxxxxxe" #参考官方文档
secret_key = "xxxxxxx" #同上
expired = time.time() + 2592000
onceExpired = 0
current = time.time()
rdm = ''.join(random.choice("0123456789") for i in range(10))
userid = "0"
fileid = "tencentyunSignTest" info = "a=" + appid + "&b=" + bucket + "&k=" + secret_id + "&e=" + str(expired) + "&t=" + str(current) + "&r=" + str(
rdm) + "&u=0&f=" signindex = hmac.new(secret_key, info, hashlib.sha1).digest() # HMAC-SHA1加密
sign = base64.b64encode(signindex + info) # base64转码 url = "http://recognition.image.myqcloud.com/ocr/general"
headers = {'Host': 'recognition.image.myqcloud.com',
"Authorization": sign,
}
files = {'appid': (None,appid),
'bucket': (None,bucket),
'image': ('1.jpg',open('C:\\ThsSoftware\\python2_x86_ths\\1.jpg','rb'),'image/jpeg')
} r = requests.post(url, files=files,headers=headers)
responseinfo = r.content
#创建内存中的word文档对象
#file=docx.Document()
r_index = r'itemstring":"(.*?)"' # 做一个正则匹配
result = re.findall(r_index, responseinfo)
for i in result:
#file.add_paragraph(i)
print i
#file.save("D:\\writeResult.docx")

  识别率挺高的!