zoukankan      html  css  js  c++  java
  • 截图 文字识别

    主:

    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)
    

      

  • 相关阅读:
    100 道 Linux 常见面试题
    借助Redis锁,完美解决高并发秒杀问题
    'cnpm'安装install
    Git常用命令及方法大全
    idea controller service impl mapper xml切换跳转快捷键
    idea创建springboot项目用阿里云镜像
    mybatis.type-aliases-package的作用和用法
    MyBatis Generator
    https://antdv.com/components/layout-cn/
    https://mvnrepository.com/search?q=mysql-connector-java //maven
  • 原文地址:https://www.cnblogs.com/gdwz922/p/9249269.html
Copyright © 2011-2022 走看看