脚本说明: 脚本需要修改 APPID 以及 API_KEY的值,请到讯飞api平台获取。
首先截图,然后打开脚本直接运行,该脚本自动识别剪切板上内容,脚本运行结束后,直接crtl+v复制。
import base64
import hashlib
import time
import keyboard as keyboard
import pyperclip
import requests
from PIL import ImageGrab
URL = "http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting"
APPID = ""
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()
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
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 all(ord(c) < 128 for c in keyword)
def settext(aString):
pyperclip.copy(aString)
pyperclip.paste()
def get_pic():
img1 = ImageGrab.grabclipboard()
img_path = str(int(time.time()))+"javachuan"+".png"
img1.save(img_path)
return img_path
language = "cn|en"
location = "true"
r = requests.post(URL, headers=getHeader(), data=getBody(get_pic()))
resp=r.json()
result=""
content=""
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+=content
result+="\n"
settext(result)
print(result)
我的ocr python脚本效果演示: 中英文可以有效识别,并且尽可能保持原字体格式,英文单词之间有 空格, 原本是同一行,就是同一行。
qq的文字识别效果演示 是恶速度慢,并且识别后不好复制,不能保持原样。
|