在论坛看到一个调用了百度的API的,弄了下失败的,就转成了弄个腾讯的,腾讯的一个月有1000次免费的额度,一般自己是够用的了。
要自己跑,要先pip install Pillow,pyperclip,tencentcloud-sdk-python
代码如下:
[Python] 纯文本查看 复制代码 # 代码参考了:https://blog.csdn.net/weixin_40248634/article/details/105938224,[url]https://www.52pojie.cn/forum.php?mod=viewthread&tid=1130029&extra=page%3D4%26filter%3Dtypeid%26typeid%3D29[/url]
import os
from PIL import Image, ImageGrab
SECRET_ID = "" # 这两个,换成你自己申请的腾讯的秘钥,地址https://console.cloud.tencent.com/cam/capi
SECRET_KEY = ""
##从剪贴板保存图像
def GetPhoto():
im = ImageGrab.grabclipboard()
if isinstance(im, Image.Image) == True:
if not os.path.exists(r"C:\TempPhoto"):
os.mkdir(r"C:\TempPhoto")
open(r"C:\TempPhoto\1.jpg", "w+")
im.save(r"C:\TempPhoto\1.jpg")
return 0
else:
return 1
if __name__ == '__main__':
print("程序开始运行,可以截图识别啦。。。。。。。")
while True:
i = GetPhoto()
if i == 0:
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
import base64
import pyperclip
try:
cred = credential.Credential(SECRET_ID, SECRET_KEY)
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-guangzhou", clientProfile)
image_path = r'C:\TempPhoto\1.jpg'
with open(image_path, 'rb') as f: # 以二进制读取本地图片
data = f.read()
encodestr = str(base64.b64encode(data), 'utf-8') # base64编码图片
# req = models.GeneralBasicOCRRequest() # 这个是是通用印刷体识别接口,
req = models.GeneralAccurateOCRRequest() # 这个是通用印刷体识别(高精度版),跟下面的需要保持一致
req.ImageBase64 = encodestr
# resp = client.GeneralBasicOCR(req) # GeneralBasicOCR是通用印刷体识别接口,
resp = client.GeneralAccurateOCR(req) # 通用印刷体识别(高精度版)GeneralAccurateOCR 跟上面的需要保持一致
with open(r"C:\TempPhoto\1.txt", mode='wt', encoding='utf-8') as w:
for text in resp.TextDetections:
w.write(text.DetectedText) # 将OCR识别的文字保存到文件
with open(r"C:\TempPhoto\1.txt", mode='rt', encoding='utf-8') as r:
Str = r.read()
pyperclip.copy(Str)
print("识别完成!识别结果已复制到剪贴板,可直接粘帖")
if os.path.exists(r"C:\TempPhoto\1.txt"):
os.remove(r"C:\TempPhoto\1.txt")
if os.path.exists(r"C:\TempPhoto\1.jpg"):
os.remove(r"C:\TempPhoto\1.jpg")
if os.path.exists(r"C:\TempPhoto"):
os.rmdir(r"C:\TempPhoto")
break
except TencentCloudSDKException as err:
print(f"识别失败,错误:{err}")
|