100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python实现OCR-手写体识别

python实现OCR-手写体识别

时间:2024-06-07 12:01:43

相关推荐

python实现OCR-手写体识别

python实现OCR-手写体识别

日常生活中很多重要的文献在存在于图片中,需要手动输入到word,excel,这样麻烦又费时。为了能提高效率,我们可以使用腾讯提供的API来满足我们的要求,直接通过图片提取图片上的文字内容。

接口描述

接口请求域名:https://recognition./ocr/handwriting

本接口(handwriting)用于手写体识别。根据用户上传的图像,返回识别出的字段信息。

本接口支持 HTTPS 协议,如果您现在使用的是 HTTP 协议,为了保障您的数据安全,请切换至 HTTPS。

请求头时所需要提交的参数Header

必选参数:

Host (recognition.) 文字识别时请求的域名

content-type (application/json 或multipart/form->(1. 使用 application/json 格式,参数为 url 或 image,其值为图片链接或图片 base64 编码;2. 使用 multipart/form-data 格式,参数为 image,其值为图片的二进制内容。)

authorization(鉴权签名)

签名所需的信息

生成签名所需信息必须使用主账号的,包括 App ID、Secret ID 和 Secret Key

App ID、Secret ID 和 Secret Key z注册腾讯云即可得到

拼接签名串

拼接多次有效签名串: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

# -*- coding: UTF-8 -*-import tkinter as tkimport requestsimport hmacimport hashlibimport base64import timeimport randomimport refrom 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() + 2592000onceExpired = 0currenttime = 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./ocr/handwriting" # api接口headers = {"Authorization": sign,"Host": "recognition.","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) # 传入文件filesresponseinfo = response.contentdata = 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() + 2592000onceExpired = 0currenttime = 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./ocr/handwriting" # api接口headers = {"Host": "recognition.","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) # 传入文件filesresponseinfo = response.contentdata = 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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。