截图 文字识别

Posted gdwz922

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了截图 文字识别相关的知识,希望对你有一定的参考价值。

主:

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)

  

以上是关于截图 文字识别的主要内容,如果未能解决你的问题,请参考以下文章

Python实战,截图识别文字,过万使用量版本?

Python实战,截图识别文字,过万使用量版本?

用百度ocr+微信截图实现文字识别

OCR文字识别软件拆分对页和校正文本方向的教程

利用云识别过简单验证码实例(源代码分享)

为啥qq提取文字识别失败