截图 文字识别

主:

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)

  

转载于:https://www.cnblogs.com/gdwz922/p/9249269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值