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()
""" 如果有可選參數(shù) """
options = {}
options["detect_direction"] = "true"
options["probability"] = "true"
""" 帶參數(shù)調(diào)用通用文字識(shí)別(高精度版) """
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("文本已經(jīng)保存本地")
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()
""" 如果有可選參數(shù) """
options = {}
options["detect_direction"] = "true"
options["probability"] = "false"
""" 帶參數(shù)調(diào)用通用文字識(shí)別(高精度版) """
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("文本已經(jīng)保存本地" )
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('請(qǐng)繼續(xù)截圖....')
def main():
for filename in os.listdir(r"./"):
if (filename.endswith('.jpg')) or (filename.endswith('.png')) or (filename.endswith('.bmp')) :
get_reuslt(filename)
input("結(jié)束請(qǐng)按Enter鍵")
if __name__ == '__main__':
print('********'*2+'請(qǐng)開(kāi)始你的表演!!!!'+'********'*2+'\n')
print('截屏識(shí)別填1,圖片識(shí)別填2:')
pd=input('')
if pd=='2':
print('***************請(qǐng)將圖片放置本目錄下***************')
a=input("我已將圖片放好?? (y/n):")
if a=='y':
main()
else:
pass
else:
print('只支持快捷鍵F1截屏,需要按 Ctrl+c 將圖片存到剪貼板...')
print('請(qǐng)開(kāi)始截圖.......')
jietu()
4.報(bào)錯(cuò)解決:pytesseract.pytesseract.TesseractNotFoundError
問(wèn)題解決:Python\Python36-32\Lib\site-packages\pytesseract 路徑下的pytesseract.py文件中,注意反斜杠!!!!
tesseract_cmd = ‘D:/Program Files/Tesseract-OCR/tesseract.exe
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
