基于PYQT5及百度aip的文字识别程序。

基于PYQT5及百度aip的文字识别程序。

在这里插入图片描述

#coding:utf-8
from PyQt5.Qt import *
import sys
from aip import AipOcr
class App(QWidget):
    def __init__(self):
        super().__init__()
        self.author='薛辉辉'
        self.setWindowTitle('文字识别程序 1.0   by:xuehuihui')
        # self.resize(500,500)
        self.setFixedSize(500,500)
        self.btn=QPushButton('打开',self)
        self.btn.move(20,20)
        self.btn.resize(80,30)
        self.btn.clicked.connect(self.openfiles)
        self.text1=QTextEdit(self)
        self.text1.move(20,50)
        self.text1.resize(460,430)
    def openfiles(self):
        self.text1.clear()
        self.a,b=QFileDialog.getOpenFileNames(self, "多文件选择","./","All Files (*.jpg *.gif *.png *.jpeg);;JPG Files (*.jpg);;PNG Files (*.png)")
        print(self.a,b)
        for i in self.a:
            self.baidu(i)
    def baidu(self,path):
        APP_ID = '自行填写'
        API_KEY = '自行填写'
        SECRET_KEY = '自行填写'
        magnification = 0.6
        client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

        def get_file_content(filePath):
            with open(filePath, 'rb') as fp:
                return fp.read()

        image = get_file_content(path)
        options = {}
        options["recognize_granularity"] = "big"
        options["language_type"] = "CHN_ENG"
        options["detect_direction"] = "true"
        options["detect_language"] = "true"
        options["vertexes_location"] = "true"
        options["probability"] = "false"
        a = client.general(image, options)
        word = '\n'
        loc = []
        word_len = a['words_result'][0]['finegrained_vertexes_location'][1]['x'] - \
                   a['words_result'][0]['finegrained_vertexes_location'][0]['x']
        for i in range(0, a['words_result_num']):
            if i == a['words_result_num'] - 1:
                word = word + a['words_result'][i]['words']
                break
            else:
                if a['words_result'][i]['location']['left'] > word_len * magnification + \
                        a['words_result'][i + 1]['location']['left'] and a['words_result'][i]['location']['width'] < \
                        a['words_result'][i + 1]['location']['width']:
                    word = word + '\n    ' + a['words_result'][i]['words']
                else:
                    word = word + a['words_result'][i]['words']
        print(word)
        self.text1.append(word)
if __name__ == '__main__':
    app=QApplication(sys.argv)
    window=App()
    window.show()
    sys.exit(app.exec_())
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值