截图 文字识别

主:

import keyboard     #  键盘控制
from PIL import ImageGrab # 获取剪切板的文件
import time
import sys

from baidu import BaiDuAPI          #   自己写的 百度文字识别,

#  取图
def show_p():
    if keyboard.wait('ctrl + alt + a') == None:  # 截图
        if keyboard.wait('ctrl') == None:         # 告知程序截图完了,
            time.sleep(0.01)
            im = ImageGrab.grabclipboard()  # 获取剪切板的文件
            im.save('1.png')             # 保存剪切板图片,


if __name__ =='__main__':
    d = BaiDuAPI('co.ini')
    for _ in range(sys.maxsize):
        show_p()

    text = d.shi_bie('1.png')
    print(text)

  

 

百度文字识别

import configparser        #  读写配置文件
from aip import AipOcr    # pip install baidu-aip  百度文字识别


class BaiDuAPI( object ):       #  父类
    '''用于文字识别'''
    def __init__(self,filePath):
        target = configparser.ConfigParser()

        #  将百度得到 KEY 写入 co.ini,
        target.read( filePath, encoding='utf-8-sig' )
        app_id = target.get('我的 KEY','APP_ID')
        api_key = target.get('我的 KEY','API_KEY')
        secret_key = target.get('我的 KEY','SECRET_KEY')
        self.client = AipOcr(app_id, api_key, secret_key)       # 百度提供

    @staticmethod       # 静态方法
    def get_a(filePath):
        '''用于读取图片'''
        with open( filePath,'rb' ) as f:
            return f.read()

    def shi_bie(self,filePath):
        '''将图片成文字'''
        image = self.get_a(filePath)
        texts = self.client.basicGeneral(image)      #  百度提供
        # print(texts)
        # texts = texts['words_result'][0]['words']
        te = ''
        for i in texts['words_result']:
            #        取到最后,如果没有 words 返回 空
            te = te + ''.join(i.get('words','') )
        # print(te)
        return te

if __name__ =='__main__':
    d = BaiDuAPI('co.ini')
    text = d.shi_bie('1.png')
    print(text)

 

 百度文字识别中用到的 KEY,用 co.ini导入

[我的 KEY]
APP_ID = 123
API_KEY = abc
SECRET_KEY = jkl
;client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

  

posted @ 2018-07-01 03:37  25班Ph201805201  阅读(286)  评论(0编辑  收藏  举报