前面有講在窗體上如何滾動(dòng)顯示大尺寸圖像,具體可以參看:84.——使用PyQt的滾動(dòng)顯示大尺寸圖像 。有網(wǎng)友留言能否做一個(gè)識(shí)別圖像中的文字 。
本文就在上一篇的程序基礎(chǔ)上增加一個(gè)圖像文字識(shí)別功能人工智能圖像識(shí)別技術(shù),并保存識(shí)別結(jié)果到文本文件中 。
界面設(shè)計(jì)
整個(gè)垂直布局,分上,中,下三個(gè)區(qū)域,上區(qū)域一個(gè)和+Label用來顯示圖像 。中間區(qū)域用文本框來顯示識(shí)別結(jié)果 。下面一個(gè)功能按鈕人工智能圖像識(shí)別技術(shù) , 一個(gè)識(shí)別、一個(gè)保存 。文本框高度設(shè)定為窗體的四分之一 。
UI
功能實(shí)現(xiàn)
功能:
1、默認(rèn)打開獲取當(dāng)前目錄中的所有圖像,點(diǎn)擊選擇文件夾項(xiàng),可以選擇任意目錄 。
2、單擊圖像文件名,可以顯示整幅圖像,點(diǎn)擊圖像,可以在圖像展示區(qū)按比例縮放整個(gè)圖像 。(窗口可以自適應(yīng)改變大?。?
3、對(duì)打開的當(dāng)前圖像進(jìn)行文字識(shí)別(這里使用的是 )
4、保存文字識(shí)別結(jié)果
代碼:
import sysimport osfrom PyQt5 import QtCore, QtGui,QtWidgetsfrom Ui_picocr import Ui_MainWindowimport paddleocrimport numpy as npclass MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):def __init__(self):super().__init__()self.setupUi(self)self.imgname=""self.npimg=np.array([])#設(shè)置圖片控件的雙擊事件self.lblimg.mousePressEvent=self.img_scale#圖像列表框按鍵事件self.lstimg.keyPressEvent=self.on_lstimg_keyPressEvent#文字識(shí)別按鈕點(diǎn)擊事件self.btnocr.clicked.connect(self.on_btnocr_clicked)#保存按鈕點(diǎn)擊事件self.btnsave.clicked.connect(self.on_btnsave_clicked)#設(shè)置圖片控件文本提示self.lblimg.setToolTip("雙擊自動(dòng)調(diào)整大小")#獲取當(dāng)前目錄curdir=os.path.abspath(os.curdir)#獲取當(dāng)前目錄下的所有圖片文件lstimg=["選擇文件夾"]for root,dir,file in os.walk(curdir):for f in file:if os.path.splitext(f)[1] in ['.jpg','.png','.bmp']:lstimg.append(os.path.join(root,f))#把圖片文件列表放到listview中slm=QtCore.QStringListModel()slm.setStringList(lstimg)self.lstimg.setModel(slm)#設(shè)置txtedit高度self.txtedit.setFixedHeight(self.height()//4)#圖片列表單擊事件def on_lstimg_clicked(self,index):#獲取當(dāng)前圖片文件名self.imgname=self.lstimg.model().stringList()[index.row()]if self.imgname=="選擇文件夾":#打開選擇文件夾對(duì)話框dirname=QtWidgets.QFileDialog.getExistingDirectory(self,'選擇文件夾')lstimg=["選擇文件夾"]for root,dir,file in os.walk(dirname):for f in file:if os.path.splitext(f)[1] in ['.jpg','.png','.bmp']:lstimg.append(os.path.join(root,f))#把圖片文件列表放到listview中slm=QtCore.QStringListModel()slm.setStringList(lstimg)self.lstimg.setModel(slm)else:#獲取圖像的寬高img=QtGui.QImage(self.imgname)h,w,c=img.height(),img.width(),img.format()#QImage圖像格式轉(zhuǎn)換為cv2圖像格式########### Format_RGB32 = 4,存入格式為B,G,R,A 對(duì)應(yīng) 0,1,2,3# RGB32圖像每個(gè)像素用32比特位表示,占4個(gè)字節(jié),# R,G,B分量分別用8個(gè)bit表示 , 存儲(chǔ)順序?yàn)锽,G,R , 最后8個(gè)字節(jié)保留img2bgr=img.convertToFormat(QtGui.QImage.Format_RGB32)ptr=img2bgr.bits()ptr.setsize(img2bgr.byteCount())self.npimg=np.array(ptr,dtype=np.uint8).reshape(h,w,4)#去掉alpha通道BGRself.npimg=self.npimg[:,:,:3]#設(shè)置scrollarea的滾動(dòng)范圍和自動(dòng)調(diào)整大小self.scrollAreaWidgetContents.setMinimumSize(w,h)#設(shè)置圖像控件的寬高self.lblimg.setFixedSize(w,h)#顯示圖片self.lblimg.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(self.imgname)))self.statusBar().showMessage(self.imgname)#圖像列表框按鍵事件def on_lstimg_keyPressEvent(self,event):if event.key()==QtCore.Qt.Key_Down or event.key()==QtCore.Qt.Key_Up:self.lstimg.setCurrentIndex(self.lstimg.currentIndex().sibling(self.lstimg.currentIndex().row()+1,0))self.on_lstimg_clicked(self.lstimg.currentIndex())#圖像控件雙擊事件def img_scale(self,event):#設(shè)置大小為滾動(dòng)區(qū)域的大小self.lblimg.setFixedSize(self.scroll.width(),self.scroll.height())#設(shè)置圖像大小自適應(yīng)控件大小self.lblimg.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(self.imgname)).scaled(self.lblimg.width(),self.lblimg.height(),QtCore.Qt.KeepAspectRatio))#ocr按鈕事件def on_btnocr_clicked(self):ocr=paddleocr.PaddleOCR(use_angle_cls=True,lang="ch")result=ocr.ocr(self.npimg)boxes=[]txts=[]scores=[]for line in result:boxes.append(line[0])txts.append(line[1][0])scores.append(line[1][1])self.txtedit.setText("n".join(txts))def on_btnsave_clicked(self):#保存文本,打開保存文本對(duì)話框filename=QtWidgets.QFileDialog.getSaveFileName(self,'保存文本','','Text Files(*.txt)')with open(filename[0],"w") as f:f.write(self.txtedit.toPlainText())self.statusBar().showMessage("保存成功")if __name__ == '__main__':app=QtWidgets.QApplication(sys.argv)window=MainWindow()window.setWindowTitle("picocr")window.show()sys.exit(app.exec_())
注意:
1、列表響應(yīng)箭頭按鍵事件
#圖像列表框按鍵事件 self.lstimg.keyPressEvent=self.on_lstimg_keyPressEvent
2、圖像格式轉(zhuǎn)為cv2圖像格式 。文字識(shí)別傳入的圖像參數(shù)img: img for ocr,,and list or。所以這里也可以直接使用 。
#QImage圖像格式轉(zhuǎn)換為cv2圖像格式########### Format_RGB32 = 4,存入格式為B,G,R,A 對(duì)應(yīng) 0,1,2,3# RGB32圖像每個(gè)像素用32比特位表示,占4個(gè)字節(jié) , # R,G,B分量分別用8個(gè)bit表示,存儲(chǔ)順序?yàn)锽 , G,R , 最后8個(gè)字節(jié)保留img2bgr=img.convertToFormat(QtGui.QImage.Format_RGB32)ptr=img2bgr.bits()ptr.setsize(img2bgr.byteCount())self.npimg=np.array(ptr,dtype=np.uint8).reshape(h,w,4)#去掉alpha通道BGRself.npimg=self.npimg[:,:,:3]
運(yùn)行效果
結(jié)果1
結(jié)果2
【85.人工智能——PyQt+圖像文字識(shí)別】本文到此結(jié)束,希望對(duì)大家有所幫助 。
- 人工智能電話機(jī)器人怎么樣?
- 人工智能時(shí)代,聽朋友說懂人工智能一定會(huì)python
- 人工智能大討論:2045年人工智能超越人類靠譜么?
- 未來人工智能的機(jī)器人會(huì)不會(huì)統(tǒng)治世界呢?你怎么看?
- 人工智能利用聯(lián)想能力,能一鍵把照片上的衣服去掉,怎么看?
- chatGTP人工智能
- 不可見問題-人工智能時(shí)代
- 炒股,一個(gè)人的認(rèn)知局限性,到底有多可怕?人工智能,只有A股的那些
- 從人工智能到遷移學(xué)習(xí)之簡(jiǎn)述
- 當(dāng)AI發(fā)展到強(qiáng)人工智能時(shí)代時(shí)
