python屏幕文字识别_python 图片文字识别 可截图识别

2022-09-01 11:06:39 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

[Python] 纯文本查看 复制代码import osfrom aip import AipOcr

import keyboard

from PIL import ImageGrab

from time import sleep

def get_reuslt(img_name):

a=input(‘是否添加可信度?(建议字多不加) (y/n):’)

if a == ‘y’:

APP_ID = ‘xxxxxx’

API_KEY = ‘xxxxxx’

SECRET_KEY = ‘xxxxxx’

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 = ‘16689194’

API_KEY = ‘jXW5sEC420DZidAwMFMGQGsI’

SECRET_KEY = ‘5LRvxbPip8ZIdkM31rNs7Zo6d5o2Wv8q’

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()

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/140682.html原文链接:https://javaforall.cn

0 人点赞