Python文字识别

import keyboard

import time

from PILimport ImageGrab#pip install

from aipimport AipOcr#pip install baidu-aip

#www.ai.baidu.com 注册账号 获得以下参数

""" 你的 APPID AK SK """

APP_ID= 'xxxx'

API_KEY= 'xxxxxxxx'

SECRET_KEY= 'xxxxxxxxxxxx'

client= AipOcr(APP_ID, API_KEY, SECRET_KEY)

keyboard.wait(hotkey='ctrl+alt+a')  #截图快捷键

keyboard.wait(hotkey='ctrl+v')    #保存快捷键

time.sleep(0.1)

#保存图片

image= ImageGrab.grabclipboard()  #把剪切板当中的图片直接拿出来

image.save('screen.png')  #路径 默认当前路径下

#识别图片内容,调用一个接口  api (读取图片的二进制数据)

#https://ai.baidu.com

""" 读取图片

def get_file_content(filePath):

with open(filePath, 'rb') as fp:

return fp.read()

image = get_file_content('example.jpg')

"""

####  b  bytes  二进制类型

#打开一个文件,

with open('screen.png','rb') as f:

  new_image= f.read()

""" 调用通用文字识别(高精度版) """

text= client.basicAccurate(new_image)

# print ( text)

result= text['words_result']

for iin result:

  print(i['words'])

你可能感兴趣的:(Python文字识别)