python|女友(啥,识别个文字还要付费(我立马用Python实现了一款免费版文字识别工具!))

导语 有一天和女朋友聊天,翻着手机上的软件,看电影、看编程网站, 她说到:“这么多 APP,怎么就没一个做文字识别很方便的呢?
python|女友(啥,识别个文字还要付费(我立马用Python实现了一款免费版文字识别工具!))
文章图片

我经常读书读到一段话想把它摘抄下来,可是这些软件不是打开进入文字识别步骤很复杂,就是限制识别次数,要么就是限制编辑,很多识别软件还都是付费的,好烦“。
然后程序员小哥说:“要不我给你做一个免费版本文字识别小程序?” 话不多话,对象第一,开干~~~
?python|女友(啥,识别个文字还要付费(我立马用Python实现了一款免费版文字识别工具!))
文章图片
python|女友(啥,识别个文字还要付费(我立马用Python实现了一款免费版文字识别工具!))
文章图片
??
正文 程序原理简介:
python利用百度文字识别功能,实现对上传的图片进行扫描,获取图片的文字信息。
环境安装部分:PyQt5界面化程序。
版本:Python3,如下模块以及一些自带的模块。

# 引入第三方模块

import requests, base64 from PIL import Image

选择图片类型为.jpg,.png。
# 选择图片执行方法 def openfile(self): # 启动选择文件对话空,查找jpg以及png图片 self.download_path = QFileDialog.getOpenFileName(self, "选择要识别的图片", os.getcwd(), "Image Files(*.jpg *.png)")

进行图片识别:
图片识别(API) '''
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage" # 二进制方式打开图片文件 f = open(self.download_path[0], 'rb') img = base64.b64encode(f.read())params = {"image": img} # access_token = '[调用鉴权接口获取的token]' request_url = request_url + "?access_token=" + baiduToken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=https://www.it610.com/article/params, headers=headers) if response: # print(response.json()) return response.json()

效果图:

附完整项目代码:
from PyQt5.QtWidgets import * from PyQt5.QtGui import *# 引入自定义模块 import dc # 引入内置模块 import sys import os # 引入第三方模块 import requests, base64 from PIL import Imageclass parentWindow(QWidget, dc.Ui_Form): # 初始化方法 def __init__(self): # 找到父类 首页面 super(parentWindow, self).__init__() # 初始化页面方法 self.setupUi(self) # 点击选择图片 self.selectImg.clicked.connect(self.openfile) # 点击查看图片 self.viewImg.clicked.connect(self.viewbtn)# 选择图片执行方法 def openfile(self): # 启动选择文件对话空,查找jpg以及png图片 self.download_path = QFileDialog.getOpenFileName(self, "选择要识别的图片", os.getcwd(), "Image Files(*.jpg *.png)") # 判断是否选择图片 if not self.download_path[0].strip(): QMessageBox.information(self, '提示信息', '没有选择名片图片') pass else: # pixmap解析图片 pixmap = QPixmap(self.download_path[0]) # 设置图片 self.imgLabel.setPixmap(pixmap) # 让图片自适应label大小 self.imgLabel.setScaledContents(True) try: # 识别名片图片返回识别结果 content = self.recgImg() except: QMessageBox.information(self, '提示信息', '识别错误请重新选择图片')# 识别图片的数据赋值 words_result = content['words_result'] # print(words_result) text = '' for item in words_result: for v in item.values(): text = text + '\n' + v self.discernText.setText(text)# 识别名片图片 def recgImg(self): # 获取baiduToken apikey = '你的apikey' seckey = '你的seckey' tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + apikey + '&client_secret=' + seckey res = requests.get(url=tokenUrl, headers={'content-type': 'application/json; charset=UTF-8'}).json() baiduToken = res['access_token']''' 图片识别(API) ''' request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage" # 二进制方式打开图片文件 f = open(self.download_path[0], 'rb') img = base64.b64encode(f.read())params = {"image": img} # access_token = '[调用鉴权接口获取的token]' request_url = request_url + "?access_token=" + baiduToken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=https://www.it610.com/article/params, headers=headers) if response: # print(response.json()) return response.json()# 点击查看图片显示大图功能 def viewbtn(self): if self.download_path: # 使用电脑中的看图工具打开图片 img = Image.open(self.download_path[0]) # 显示图片 img.show() else: QMessageBox.information(self,'提示信息', '先选择名片图片')if __name__ == '__main__': # 每一个PyQt5应用都必须创建一个应用对象 app = QApplication(sys.argv) # 初始化页面 window = parentWindow() # 显示首页 window.show() sys.exit(app.exec_())

python|女友(啥,识别个文字还要付费(我立马用Python实现了一款免费版文字识别工具!))
文章图片
总结 好啦!文字识别系统就完成了,拿去!不谢~
【python|女友(啥,识别个文字还要付费(我立马用Python实现了一款免费版文字识别工具!))】记得三连就好~源码基地:#959755565#

    推荐阅读