原文链接: https://www.52pojie.cn/thread-989385-1-1.html
1.安装python包
pip install pillow
pip install pytesser3
pip install pytesseract
pip install wheel
pip install baidu-aip
pip install keyboard
2.安装Tesseract-OCR
3.源码
from aip import AipOcr
import keyboard
import os
from PIL import ImageGrab
from time import sleep
def get_reuslt(img_name):
a=input('是否添加可信度?(建议字多不加) (y/n):')
if a == 'y':
APP_ID = 'xxx'
API_KEY = 'xxx'
SECRET_KEY = 'xxx'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
with open(img_name, 'rb') as fp:
image = fp.read()
""" 如果有可选参数 """
options = {}
options["detect_direction"] = "true"
options["probability"] = "true"
""" 带参数调用通用文字识别(高精度版) """
client = client.basicAccurate(image, options)
print(type(client))
for i in client['words_result']:
reuslt = i['words']
Credibility = i['probability']['average']
wyc=reuslt+" 平均可信度为:"+str((Credibility) * 100) + '%'
print(wyc)
with open(img_name+'.txt','a',encoding='utf-8') as f :
f.write(wyc+'\n')
print("文本已经保存本地")
else:
APP_ID = 'xxxxx'
API_KEY = 'xxxx'
SECRET_KEY = 'xxxxx'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
with open(img_name, 'rb') as fp:
image=fp.read()
""" 如果有可选参数 """
options = {}
options["detect_direction"] = "true"
options["probability"] = "false"
""" 带参数调用通用文字识别(高精度版) """
client=client.basicAccurate(image, options)
for i in client['words_result']:
reuslt=i['words']
print(reuslt+'\t')
with open(img_name+'.txt','a',encoding='utf-8') as f :
f.write(reuslt+'\n')
print("文本已经保存本地" )
def jietu():
while 1:
keyboard.wait('f1','')
keyboard.wait('ctrl+c')
sleep(0.2)
image = ImageGrab.grabclipboard()
# 从剪贴版获取图片
image.save('截图.jpg')
for filename in os.listdir(r"./"):
if (filename.endswith('.jpg')) or (filename.endswith('.png')) or (filename.endswith('.bmp')):
get_reuslt(filename)
print('请继续截图....')
def main():
for filename in os.listdir(r"./"):
if (filename.endswith('.jpg')) or (filename.endswith('.png')) or (filename.endswith('.bmp')) :
get_reuslt(filename)
input("结束请按Enter键")
if __name__ == '__main__':
print('********'*2+'请开始你的表演!!!!'+'********'*2+'\n')
print('截屏识别填1,图片识别填2:')
pd=input('')
if pd=='2':
print('***************请将图片放置本目录下***************')
a=input("我已将图片放好? (y/n):")
if a=='y':
main()
else:
pass
else:
print('只支持快捷键F1截屏,需要按 Ctrl+c 将图片存到剪贴板...')
print('请开始截图.......')
jietu()
4.报错解决:pytesseract.pytesseract.TesseractNotFoundError
问题解决:Python\Python36-32\Lib\site-packages\pytesseract 路径下的pytesseract.py文件中,注意反斜杠!!!!
tesseract_cmd = ‘D:/Program Files/Tesseract-OCR/tesseract.exe