DigitForm.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from PyQt5.QtGui import QImage, QPixmap
  2. from PyQt5.QtWidgets import *
  3. from DigitDev import DigitDev
  4. from DigitUI import Ui_Digit
  5. from reco2 import Lenet5Recognizier
  6. from PIL import Image
  7. import numpy as np
  8. import copy
  9. import sys
  10. import cv2
  11. class DigitForm(QDialog):
  12. def __init__(self):
  13. super(DigitForm, self).__init__()
  14. # 加载UI(先设计好)
  15. # 创建对象
  16. self.ui = Ui_Digit()
  17. # 使用setupUi绑定对话框(父窗体)
  18. self.ui.setupUi(self)
  19. # AI识别对象
  20. self.reco = Lenet5Recognizier
  21. # 创建视频对象
  22. self.dev = DigitDev()
  23. self.dev.signal_video.connect(self.show_video)
  24. self.dev.start()
  25. def img_cut(self, img_file):
  26. img = cv2.imread(img_file) # 加载图像
  27. img2 = copy.deepcopy(img) # 创建副本用于显示轮廓
  28. img3 = copy.deepcopy(img)
  29. img4 = copy.deepcopy(img)
  30. img5 = copy.deepcopy(img)
  31. img6 = copy.deepcopy(img)
  32. img7 = copy.deepcopy(img)
  33. img8 = copy.deepcopy(img)
  34. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 将图像转换为灰度图像
  35. ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV) # 阈值处理将灰度图像转换为二值图像(反转)
  36. contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 查找图像中的轮廓
  37. cv2.drawContours(img2, contours, -1, (0, 0, 255), 3) # 绘制初始轮廓
  38. area = map(cv2.contourArea, contours) # 计算每个轮廓的面积
  39. area_list = list(area)
  40. area_max = max(area_list) # 找到最大面积的轮廓
  41. post = area_list.index(area_max)
  42. area_list.remove(area_max) # 找到第二大面积的轮廓
  43. area_second_max = max(area_list)
  44. post_second = area_list.index(area_second_max)
  45. cv2.drawContours(img4, contours, post, (0, 0, 255), 3) # 绘制只显示最大轮廓的图像
  46. cimg = np.zeros_like(img)
  47. cimg[:, :, :] = 255
  48. cv2.drawContours(cimg, contours, post, color=(0, 0, 0), thickness=-1) # 创建只包含最大轮廓的图像
  49. final = cv2.bitwise_or(img5, cimg) # 将原始图像和最大轮廓图像进行逐像素的或操作
  50. cv2.imwrite('img1.jpg',final)
  51. cv2.drawContours(img6, contours, post_second, (0, 0, 255), 3) # 绘制只显示第二大轮廓的图像
  52. cimg = np.zeros_like(img)
  53. cimg[:, :, :] = 255
  54. cv2.drawContours(cimg, contours, post_second, color=(0, 0, 0), thickness=-1) # 创建只包含第二大轮廓的图像
  55. final = cv2.bitwise_or(img7, cimg) # 将原始图像和第二大轮廓图像进行逐像素的或操作
  56. cv2.imwrite('img2.jpg',final)
  57. cv2.destroyAllWindows()
  58. # 抓取图像按钮
  59. def capture_image(self):
  60. # 抓取图像
  61. self.capture_data = self.buffer_data
  62. self.capture_shape = self.buffer_shape
  63. # 显示抓取的图像
  64. # byte -> QImage
  65. h, w, ch = self.capture_shape
  66. image = QImage(self.capture_data, w, h, w*ch, QImage.Format_BGR888)
  67. # QImage -> QPixmap
  68. pixmap = QPixmap.fromImage(image)
  69. # QPixmap -> QLabel
  70. self.ui.lbl_image.setPixmap(pixmap)
  71. self.ui.lbl_image.setScaledContents(True)
  72. # 将QPixmap转化为png文件
  73. pixmap = QPixmap(pixmap)
  74. image = pixmap.toImage()
  75. width, height = image.width(), image.height()
  76. image.save('example.png')
  77. # 识别图像按钮
  78. def digit_recognize(self):
  79. self.img_cut('example.png')
  80. result1 = self.reco.recognizie("img1.jpg")
  81. result2 = self.reco.recognizie("img2.jpg")
  82. # self.ui.lbl_top1.setText('11')
  83. # self.ui.lbl_top1 = QLabel()
  84. self.ui.lbl_top1.setPixmap(QPixmap('img1.jpg'))
  85. self.ui.lbl_top1.setScaledContents(True) # 自适应QLabel大小
  86. self.ui.lbl_prob1.setText(F"{result1[0]}")
  87. # self.ui.lbl_top2.setText('22')
  88. # # self.ui.lbl_top2 = QLabel()
  89. self.ui.lbl_top2.setPixmap(QPixmap('img2.jpg'))
  90. self.ui.lbl_top2.setScaledContents(True)
  91. self.ui.lbl_prob2.setText(F"{result2[0]}")
  92. self.ui.lbl_result.setText(self.judgment('img1.jpg','img2.jpg'))
  93. # 进行判断
  94. def judgment(self,x,y):
  95. r1 = self.reco.recognizie(x)
  96. r2 = self.reco.recognizie(y)
  97. if r1[0] == r2[0]:
  98. return '平局!'
  99. elif r1[0] == '剪刀' and r2[0] == '布' or r1[0] == '布' and r2[0] == '石头' or r1[0] == '石头' and r2[0] == '剪刀':
  100. return '左方胜利!'
  101. elif r1[0] == '布' and r2[0] == '剪刀' or r1[0] == '剪刀' and r2[0] == '石头' or r1[0] == '石头' and r2[0] == '布':
  102. return '右方胜利!'
  103. # 抓取图像处理
  104. def show_video(self, h, w, ch, data):
  105. self.buffer_data = data
  106. self.buffer_shape = (h, w, ch)
  107. # byte -> QImage
  108. image = QImage(data, w, h, w*ch, QImage.Format_RGB888)
  109. # QImage -> QPixmap
  110. pixmap = QPixmap.fromImage(image)
  111. # QPixmap -> QLabel
  112. self.ui.lbl_video.setPixmap(pixmap)
  113. self.ui.lbl_video.setScaledContents(True)