python实现OCR-手写体识别
日常生活中很多重要的文献在存在于图片中,需要手动输入到word,excel,这样麻烦又费时。为了能提高效率,我们可以使用腾讯提供的API来满足我们的要求,直接通过图片提取图片上的文字内容。
接口描述
接口请求域名:https://recognition.image.myqcloud.com/ocr/handwriting
本接口(handwriting)用于手写体识别。根据用户上传的图像,返回识别出的字段信息。
本接口支持 HTTPS 协议,如果您现在使用的是 HTTP 协议,为了保障您的数据安全,请切换至 HTTPS。
请求头时所需要提交的参数Header
必选参数:
Host (recognition.image.myqcloud.com) 文字识别时请求的域名
content-type (application/json 或multipart/form-data) (1. 使用 application/json 格式,参数为 url 或 image,其值为图片链接或图片 base64 编码;2. 使用 multipart/form-data 格式,参数为 image,其值为图片的二进制内容。)authorization (鉴权签名)
签名所需的信息
生成签名所需信息必须使用主账号的,包括App ID、Secret ID 和 Secret Key
App ID、Secret ID 和 Secret Key 注册腾讯云即可得到所以此处不作提供
拼接签名串
拼接多次有效签名串:
a=[appid]&b=[bucket]&k=[SecretID]&e=[expiredTime]&t=[currentTime]&r=[rand]&f=[fileid]
拼接单次有效签名串:
a=[appid]&b=[bucket]&k=[SecretID]&e=[expiredTime]&t=[currentTime]&r=[rand]&f=[fileid]
用 HMAC-SHA1 算法对请求进行加密(SHA1算法加密后的输出必须是原始的二进制数据,否则签名失败)。
对 orignal 使用 HMAC-SHA1 算法进行签名,然后将 orignal 附加到签名结果的末尾,再进行 Base64 编码,得到最终的 sign。
生成签名的公式如下:
SignTmp = HMAC-SHA1(SecretKey, orignal)
Sign = Base64(SignTmp.orignal)
输入参数时需要的参数 appid
完整代码如下:
Python
# -*- coding: UTF-8 -*-
import tkinter as tk
Import requests
Import hmac
Import hashlib
Import base64
Import Time
Import random
Import re
from tkinter import filedialog
# 设置窗口
Window = tk.tk()
window.title('看图识字')
window.geometry('600x500+200+100')
# 设置输入窗口
e = tk.Entry(Width = 80)
e.pack()
def put_url():
phurl = e.get()
if phurl=="":
t.delete(0.0, 'end') # 清空文本框内容
t.insert('insert', '请输入待识别文字图片的url链接~~~~~~')
Else:
appid = "12599" # 不提供,自行申请
bucket = " "
secret_id = "AKIDJNAgxxmjW4N1Qg7I" # 不提供,自行申请
secret_key = "6z2weXdgeRyFHv6" # 不提供,自行申请
expiredtime = Time.Time() + 2592000
onceExpired = 0
currenttime = Time.Time()
# rdm = ''.join(random.choice("0123456789") for i in range(10)) #join拼接推倒式
rdm = random.randrange(0, 10000000000) # 直接生产指定区域随机数
UserId = "0"
fileid = "tencentyunSignTest"
# 拼接签名公式
info = "a=" + appid + "&b=" + bucket + "&k=" + secret_id + "&e=" + str(expiredtime) + "&t=" + str(
currenttime) + "&r=" + str(
rdm) + "&u=0&f="
signindex = hmac.new(bytes(secret_key, 'utf-8'), bytes(info, 'utf-8'), hashlib.sha1).digest() # HMAC-SHA1加密公式
# sign = base64.b64encode(signindex + bytes(info,'utf-8')) #base64转码公式,下同
sign = base64.b64encode(signindex + info.encode('utf-8'))
url = "https://recognition.image.myqcloud.com/ocr/handwriting" # api接口
headers = {"Authorization": sign,
"Host": "recognition.image.myqcloud.com",
"Content - Type": "application / json",
}
'''
是否必须
appid 是 String 接入项目的唯一标识,可在 账号信息 或 云 API 密钥 中查看。
image 否 Binary/String 图片文件 或 图片 base64。
url 否 String 图片 url 和 image 同时赋值时,则以 url 指定的图像作为输入。
'''
files = {
'appid': (None, appid),
'bucket': (None, bucket),
# 'image': ('1.jpg', open('C:\\Users\Administrator\Desktop\\手写体.jpg', 'rb'), 'image/jpeg'),
'url': phurl
}
response = requests.post(url, files=files, headers=headers) # 传入文件files
responseinfo = response.Content
data = responseinfo.decode('utf-8')
t.delete(0.0, 'end') # 清空文本框内容
r_index = r 'itemstring":"(.*?)"' # 所有文字均在此属性下
result = re.findall(r_index, data) # 在data里查找所有满足要去的属性
t.delete(0.0, 'end') # 清空文本框内容
for i in result: # 遍历出内容
Print (i)
t.insert('insert', i + '\n') # 参数insert表示在光标处插入字符串
# 在光标处插入字符串
def select_pho():
if e.get()==''or e.get()!='':
Root = tk.tk()
root.withdraw()
file_path = FileDialog.askopenfilename()
appid = "139" # 不提供,自行申请
bucket = " "
secret_id = "AKIDYzGHEpXROXheuOryMdq" # 不提供,自行申请
secret_key = "KVRAneIn9HE1a4Lfrl9Se" # 不提供,自行申请
expiredtime = Time.Time() + 2592000
onceExpired = 0
currenttime = Time.Time()
# rdm = ''.join(random.choice("0123456789") for i in range(10)) #join拼接推倒式
rdm = random.randrange(0, 10000000000) # 直接生产指定区域随机数
UserId = "0"
fileid = "tencentyunSignTest"
# 拼接签名公式
info = "a=" + appid + "&b=" + bucket + "&k=" + secret_id + "&e=" + str(expiredtime) + "&t=" + str(
currenttime) + "&r=" + str(
rdm) + "&u=0&f="
signindex = hmac.new(bytes(secret_key, 'utf-8'), bytes(info, 'utf-8'), hashlib.sha1).digest() # HMAC-SHA1加密公式
# sign = base64.b64encode(signindex + bytes(info,'utf-8')) #base64转码公式,下同
sign = base64.b64encode(signindex + info.encode('utf-8'))
url = "https://recognition.image.myqcloud.com/ocr/handwriting" # api接口
headers = {'Host': 'recognition.image.myqcloud.com',
"Authorization": sign,
}
'''
是否必须
appid 是 String 接入项目的唯一标识,可在 账号信息 或 云 API 密钥 中查看。
image 否 Binary/String 图片文件 或 图片 base64。
url 否 String 图片 url 和 image 同时赋值时,则以 url 指定的图像作为输入。
'''
files = {
'appid': (None, appid),
'bucket': (None, bucket),
'image': ('1.jpg',open(file_path,'rb'),'image/jpeg'),
}
response = requests.post(url, files=files, headers=headers) # 传入文件files
responseinfo = response.Content
data = responseinfo.decode('utf-8')
r_index = r 'itemstring":"(.*?)"' # 所有文字均在此属性下
result = re.findall(r_index, data) # 在data里查找所有满足要去的属性
t.delete(0.0, 'end') # 清空文本框内容
for i in result: # 遍历出内容
Print (i)
t.insert('insert', i+'\n') # 参数insert表示在光标处插入字符串
# 设置两个插入按钮
b1 = tk.Button(text='输入url', width=20, height=2, command=put_url)
b1.pack()
b2 = tk.Button(text='选择图片', width=20, height=2, command=select_pho)
b2.pack()
t = tk.Text(width=80, height=25) # 设置文本显示框
t.pack()
window.mainloop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: UTF-8 -*-
importtkinterastk
Importrequests
Importhmac
Importhashlib
Importbase64
ImportTime
Importrandom
Importre
fromtkinterimportfiledialog
# 设置窗口
Window=tk.tk()
window.title('看图识字')
window.geometry('600x500+200+100')
# 设置输入窗口
e=tk.Entry(Width=80)
e.pack()
defput_url():
phurl=e.get()
ifphurl=="":
t.delete(0.0,'end')# 清空文本框内容
t.insert('insert','请输入待识别文字图片的url链接~~~~~~')
Else:
appid="12599"# 不提供,自行申请
bucket=" "
secret_id="AKIDJNAgxxmjW4N1Qg7I"# 不提供,自行申请
secret_key="6z2weXdgeRyFHv6"# 不提供,自行申请
expiredtime=Time.Time()+2592000
onceExpired=0
currenttime=Time.Time()
# rdm = ''.join(random.choice("0123456789") for i in range(10)) #join拼接推倒式
rdm=random.randrange(0,10000000000)# 直接生产指定区域随机数
UserId="0"
fileid="tencentyunSignTest"
# 拼接签名公式
info="a="+appid+"&b="+bucket+"&k="+secret_id+"&e="+str(expiredtime)+"&t="+str(
currenttime)+"&r="+str(
rdm)+"&u=0&f="
signindex=hmac.new(bytes(secret_key,'utf-8'),bytes(info,'utf-8'),hashlib.sha1).digest()# HMAC-SHA1加密公式
# sign = base64.b64encode(signindex + bytes(info,'utf-8')) #base64转码公式,下同
sign=base64.b64encode(signindex+info.encode('utf-8'))
url="https://recognition.image.myqcloud.com/ocr/handwriting"# api接口
headers={"Authorization":sign,
"Host":"recognition.image.myqcloud.com",
"Content - Type":"application / json",
}
'''
是否必须
appid 是 String 接入项目的唯一标识,可在 账号信息 或 云 API 密钥 中查看。
image 否 Binary/String 图片文件 或 图片 base64。
url 否 String 图片 url 和 image 同时赋值时,则以 url 指定的图像作为输入。
'''
files={
'appid':(None,appid),
'bucket':(None,bucket),
# 'image': ('1.jpg', open('C:\\Users\Administrator\Desktop\\手写体.jpg', 'rb'), 'image/jpeg'),
'url':phurl
}
response=requests.post(url,files=files,headers=headers)# 传入文件files
responseinfo=response.Content
data=responseinfo.decode('utf-8')
t.delete(0.0,'end')# 清空文本框内容
r_index=r'itemstring":"(.*?)"'# 所有文字均在此属性下
result=re.findall(r_index,data)# 在data里查找所有满足要去的属性
t.delete(0.0,'end')# 清空文本框内容
foriinresult:# 遍历出内容
Print(i)
t.insert('insert',i+'\n')# 参数insert表示在光标处插入字符串
# 在光标处插入字符串
defselect_pho():
ife.get()==''ore.get()!='':
Root=tk.tk()
root.withdraw()
file_path=FileDialog.askopenfilename()
appid="139"# 不提供,自行申请
bucket=" "
secret_id="AKIDYzGHEpXROXheuOryMdq"# 不提供,自行申请
secret_key="KVRAneIn9HE1a4Lfrl9Se"# 不提供,自行申请
expiredtime=Time.Time()+2592000
onceExpired=0
currenttime=Time.Time()
# rdm = ''.join(random.choice("0123456789") for i in range(10)) #join拼接推倒式
rdm=random.randrange(0,10000000000)# 直接生产指定区域随机数
UserId="0"
fileid="tencentyunSignTest"
# 拼接签名公式
info="a="+appid+"&b="+bucket+"&k="+secret_id+"&e="+str(expiredtime)+"&t="+str(
currenttime)+"&r="+str(
rdm)+"&u=0&f="
signindex=hmac.new(bytes(secret_key,'utf-8'),bytes(info,'utf-8'),hashlib.sha1).digest()# HMAC-SHA1加密公式
# sign = base64.b64encode(signindex + bytes(info,'utf-8')) #base64转码公式,下同
sign=base64.b64encode(signindex+info.encode('utf-8'))
url="https://recognition.image.myqcloud.com/ocr/handwriting"# api接口
headers={'Host':'recognition.image.myqcloud.com',
"Authorization":sign,
}
'''
是否必须
appid 是 String 接入项目的唯一标识,可在 账号信息 或 云 API 密钥 中查看。
image 否 Binary/String 图片文件 或 图片 base64。
url 否 String 图片 url 和 image 同时赋值时,则以 url 指定的图像作为输入。
'''
files={
'appid':(None,appid),
'bucket':(None,bucket),
'image':('1.jpg',open(file_path,'rb'),'image/jpeg'),
}
response=requests.post(url,files=files,headers=headers)# 传入文件files
responseinfo=response.Content
data=responseinfo.decode('utf-8')
r_index=r'itemstring":"(.*?)"'# 所有文字均在此属性下
result=re.findall(r_index,data)# 在data里查找所有满足要去的属性
t.delete(0.0,'end')# 清空文本框内容
foriinresult:# 遍历出内容
Print(i)
t.insert('insert',i+'\n')# 参数insert表示在光标处插入字符串
# 设置两个插入按钮
b1=tk.Button(text='输入url',width=20,height=2,command=put_url)
b1.pack()
b2=tk.Button(text='选择图片',width=20,height=2,command=select_pho)
b2.pack()
t=tk.Text(width=80,height=25)# 设置文本显示框
t.pack()
window.mainloop()
源码下载:点击下载