【Python小程序】必备软件系列之文字识别提取,前台再也不同担心纸质转电子文件啦~
生活随笔
收集整理的這篇文章主要介紹了
【Python小程序】必备软件系列之文字识别提取,前台再也不同担心纸质转电子文件啦~
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
🚀 作者 :“程序員梨子”
🚀 **文章簡介 **:本篇文章主要利用PyQt5做的一款界面化的文字識別小程序。
🚀 **文章源碼獲取 **: 為了感謝每一個關注我的小可愛💓每篇文章的項目源碼都是無償分
享滴💓👇👇👇👇
點這里藍色這行字體自取,需要什么源碼記得說標題名字哈!私信我也可!
🚀 歡迎小伙伴們 點贊👍、收藏?、留言💬
正文?
哇哇哇!這么多的文字我怎么能全部搬到文件里面呢?
?
事情是這樣子的,今天上班老板拿了一疊文件,上面的文字需要弄成電子文檔,前臺小姐姐欲哭無
淚,幸好公司程序員多,最不缺的就是代碼。so 有同樣困擾的小伙伴兒馬上拿這款文字識別小程
序用起來!再也不用擔心啦~
效果展示——
程序原理簡介:
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=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, '提示信息', '沒有選擇名片圖片')passelse:# 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' + vself.discernText.setText(text)# 識別名片圖片def recgImg(self):# 獲取baiduTokenapikey = '你的apikey'seckey = '你的seckey'tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + apikey + '&client_secret=' + seckeyres = 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=" + baiduTokenheaders = {'content-type': 'application/x-www-form-urlencoded'}response = requests.post(request_url, data=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翻屏软件】英文1秒翻,水准
- 下一篇: 【Pygame小游戏】《坦克大战》,那些