python图片ocr识别手写印刷体中英文字体
生活随笔
收集整理的這篇文章主要介紹了
python图片ocr识别手写印刷体中英文字体
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
腳本說明:
腳本需要修改 APPID 以及 API_KEY的值,請到訊飛api平臺(tái)獲取。
首先截圖,然后打開腳本直接運(yùn)行,該腳本自動(dòng)識(shí)別剪切板上內(nèi)容,腳本運(yùn)行結(jié)束后,直接crtl+v復(fù)制。
# -*- coding: utf-8 -*- import base64 import hashlib import time import keyboard as keyboard import pyperclip import requests #獲取剪切板內(nèi)容 from PIL import ImageGrab# OCR手寫文字識(shí)別接口地址 URL = "http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting" # 應(yīng)用APPID(必須為webapi類型應(yīng)用,并開通手寫文字識(shí)別服務(wù),參考帖子如何創(chuàng)建一個(gè)webapi應(yīng)用:http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481) APPID = "" # 接口密鑰(webapi類型應(yīng)用開通手寫文字識(shí)別后,控制臺(tái)--我的應(yīng)用---手寫文字識(shí)別---相應(yīng)服務(wù)的apikey) API_KEY = ""def getHeader():curTime = str(int(time.time()))param = "{\"language\":\""+language+"\",\"location\":\""+location+"\"}"paramBase64 = base64.b64encode(param.encode('utf-8'))m2 = hashlib.md5()str1 = API_KEY + curTime + str(paramBase64, 'utf-8')m2.update(str1.encode('utf-8'))checkSum = m2.hexdigest()# 組裝http請求頭header = {'X-CurTime': curTime,'X-Param': paramBase64,'X-Appid': APPID,'X-CheckSum': checkSum,'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',}return header#圖片轉(zhuǎn)換為base編碼 def getBody(filepath):with open(filepath, 'rb') as f:imgfile = f.read()data = {'image': str(base64.b64encode(imgfile), 'utf-8')}return data#英文判斷 def isEnglish(keyword):#return keyword.isalpha() #判斷純單詞 是否是英文return all(ord(c) < 128 for c in keyword)# 寫入到剪切板 def settext(aString):# importing the module# copies all the data the user has copiedpyperclip.copy(aString)# paste the copied data from clipboardpyperclip.paste()#保存剪切板數(shù)據(jù)圖片 def get_pic():# 按ctrl+后才執(zhí)行下面的語句#keyboard.wait(hotkey='ctrl+alt+a')# ctrl+c保存截圖至剪切板, ImageGrab從剪切板讀取圖片img1 = ImageGrab.grabclipboard()#print(type(img1))# 文件保存的名字img_path = str(int(time.time()))+"javachuan"+".png"img1.save(img_path)return img_path# 語種設(shè)置 language = "cn|en" # 是否返回文本位置信息 location = "true" r = requests.post(URL, headers=getHeader(), data=getBody(get_pic())) resp=r.json() result=""#每一行的數(shù)據(jù) content="" #每一行某個(gè)數(shù)據(jù) 某個(gè)文字/某個(gè)單詞 for i in range(0,len(resp["data"]["block"][0]["line"])):for j in range(0,len(resp["data"]["block"][0]["line"][i]["word"])):content=resp["data"]["block"][0]["line"][i]["word"][j]["content"]if isEnglish(content):result+=content+" " #英文單詞 需要 空格else:result+=contentresult+="\n" #換行#添加到剪切板 settext(result) print(result)我的ocr python腳本效果演示:
中英文可以有效識(shí)別,并且盡可能保持原字體格式,英文單詞之間有 空格,
原本是同一行,就是同一行。
qq的文字識(shí)別效果演示
是惡速度慢,并且識(shí)別后不好復(fù)制,不能保持原樣。
總結(jié)
以上是生活随笔為你收集整理的python图片ocr识别手写印刷体中英文字体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vim中替换字符串的方法有哪些
- 下一篇: 用Python代码自动生成文献的IEEE