|
@@ -0,0 +1,84 @@
|
|
|
|
+from PyQt5.QtWidgets import QDialog
|
|
|
|
+from PyQt5.QtGui import QImage, QPixmap
|
|
|
|
+import sys
|
|
|
|
+from .DigitUI import Ui_Digit
|
|
|
|
+from .DigitDev import DigitDev
|
|
|
|
+from .DigitAI import DigitRecognizier
|
|
|
|
+import cv2
|
|
|
|
+import numpy as np
|
|
|
|
+
|
|
|
|
+class DigitForm(QDialog):
|
|
|
|
+ def __init__(self):
|
|
|
|
+ super(DigitForm, self).__init__()
|
|
|
|
+ # 加载UI(先设计好)
|
|
|
|
+ # 创建对象
|
|
|
|
+ self.ui = Ui_Digit()
|
|
|
|
+ # 使用setupUi绑定对话框(父窗体)
|
|
|
|
+ self.ui.setupUi(self)
|
|
|
|
+ # AI识别对象
|
|
|
|
+ self.reco = DigitRecognizier()
|
|
|
|
+ # 创建视频对象
|
|
|
|
+ self.dev = DigitDev()
|
|
|
|
+ self.dev.signal_video.connect(self.show_video)
|
|
|
|
+ self.dev.start()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ # 覆盖QDialog原来两个我们不需要的默认功能
|
|
|
|
+ def keyPressEvent(self, e):
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ def closeEvent(self, e):
|
|
|
|
+ # 完成一些需要的释放工作
|
|
|
|
+ self.dev.close()
|
|
|
|
+ sys.exit(0)
|
|
|
|
+
|
|
|
|
+ # UI中的两个槽函数
|
|
|
|
+ def capture_image(self):
|
|
|
|
+ # 抓取图像
|
|
|
|
+ self.capture_data = self.buffer_data
|
|
|
|
+ self.capture_shape = self.buffer_shape
|
|
|
|
+ # 显示抓取的图像
|
|
|
|
+ # byte -> QImage
|
|
|
|
+ h, w, ch = self.capture_shape
|
|
|
|
+ image = QImage(self.capture_data, w, h, w*ch, QImage.Format_BGR888)
|
|
|
|
+ # QImage -> QPixmap
|
|
|
|
+ pixmap = QPixmap.fromImage(image)
|
|
|
|
+ # QPixmap -> QLabel
|
|
|
|
+ self.ui.lbl_image.setPixmap(pixmap)
|
|
|
|
+ self.ui.lbl_image.setScaledContents(True)
|
|
|
|
+
|
|
|
|
+ def digit_recognize(self):
|
|
|
|
+ # 已知
|
|
|
|
+ # self.capture_data
|
|
|
|
+ # self.capture_shape
|
|
|
|
+ # 准备:训练好的手写字符模型:models.lenet:LeNet-5
|
|
|
|
+ # 实现与模型一致的 神经网络结构(层数,层类型,每层参数一致)
|
|
|
|
+ # 利用神经网络结构,识别图片
|
|
|
|
+ image = np.ndarray(
|
|
|
|
+ shape=self.capture_shape, # 构建图像矩阵的形状
|
|
|
|
+ dtype=np.uint8,
|
|
|
|
+ buffer=self.buffer_data
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ result = self.reco.recognize(image)
|
|
|
|
+ self.ui.lbl_top1.setText(F"<font size=20 color=red><b><strong>{result[0][0]}</strong><b></font>")
|
|
|
|
+ self.ui.lbl_prob1.setText(F"{result[0][1]:3.2f}")
|
|
|
|
+ if len(result) == 2:
|
|
|
|
+ self.ui.lbl_top2.setText(F"{result[1][0]}")
|
|
|
|
+ self.ui.lbl_prob2.setText(F"{result[1][1]:3.2f}")
|
|
|
|
+ else:
|
|
|
|
+ self.ui.lbl_top2.setText("--")
|
|
|
|
+ self.ui.lbl_prob2.setText("--")
|
|
|
|
+
|
|
|
|
+ def show_video(self, h, w, ch, data):
|
|
|
|
+ self.buffer_data = data
|
|
|
|
+ self.buffer_shape = (h, w, ch)
|
|
|
|
+ # byte -> QImage
|
|
|
|
+ image = QImage(data, w, h, w*ch, QImage.Format_RGB888)
|
|
|
|
+ # QImage -> QPixmap
|
|
|
|
+ pixmap = QPixmap.fromImage(image)
|
|
|
|
+ # QPixmap -> QLabel
|
|
|
|
+ self.ui.lbl_video.setPixmap(pixmap)
|
|
|
|
+ self.ui.lbl_video.setScaledContents(True)
|