123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- from PyQt5.QtGui import QImage, QPixmap
- from PyQt5.QtWidgets import *
- from DigitDev import DigitDev
- from DigitUI import Ui_Digit
- from reco2 import Lenet5Recognizier
- from PIL import Image
- import numpy as np
- import copy
- import sys
- import cv2
- class DigitForm(QDialog):
- def __init__(self):
- super(DigitForm, self).__init__()
- # 加载UI(先设计好)
- # 创建对象
- self.ui = Ui_Digit()
- # 使用setupUi绑定对话框(父窗体)
- self.ui.setupUi(self)
- # AI识别对象
- self.reco = Lenet5Recognizier
- # 创建视频对象
- self.dev = DigitDev()
- self.dev.signal_video.connect(self.show_video)
- self.dev.start()
- def img_cut(self, img_file):
- img = cv2.imread(img_file) # 加载图像
- img2 = copy.deepcopy(img) # 创建副本用于显示轮廓
- img3 = copy.deepcopy(img)
- img4 = copy.deepcopy(img)
- img5 = copy.deepcopy(img)
- img6 = copy.deepcopy(img)
- img7 = copy.deepcopy(img)
- img8 = copy.deepcopy(img)
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 将图像转换为灰度图像
- ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV) # 阈值处理将灰度图像转换为二值图像(反转)
- contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 查找图像中的轮廓
- cv2.drawContours(img2, contours, -1, (0, 0, 255), 3) # 绘制初始轮廓
- area = map(cv2.contourArea, contours) # 计算每个轮廓的面积
- area_list = list(area)
- area_max = max(area_list) # 找到最大面积的轮廓
- post = area_list.index(area_max)
- area_list.remove(area_max) # 找到第二大面积的轮廓
- area_second_max = max(area_list)
- post_second = area_list.index(area_second_max)
- cv2.drawContours(img4, contours, post, (0, 0, 255), 3) # 绘制只显示最大轮廓的图像
- cimg = np.zeros_like(img)
- cimg[:, :, :] = 255
- cv2.drawContours(cimg, contours, post, color=(0, 0, 0), thickness=-1) # 创建只包含最大轮廓的图像
- final = cv2.bitwise_or(img5, cimg) # 将原始图像和最大轮廓图像进行逐像素的或操作
- cv2.imwrite('img1.jpg',final)
- cv2.drawContours(img6, contours, post_second, (0, 0, 255), 3) # 绘制只显示第二大轮廓的图像
- cimg = np.zeros_like(img)
- cimg[:, :, :] = 255
- cv2.drawContours(cimg, contours, post_second, color=(0, 0, 0), thickness=-1) # 创建只包含第二大轮廓的图像
- final = cv2.bitwise_or(img7, cimg) # 将原始图像和第二大轮廓图像进行逐像素的或操作
- cv2.imwrite('img2.jpg',final)
- cv2.destroyAllWindows()
- # 抓取图像按钮
- 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)
- # 将QPixmap转化为png文件
- pixmap = QPixmap(pixmap)
- image = pixmap.toImage()
- width, height = image.width(), image.height()
- image.save('example.png')
- # 识别图像按钮
- def digit_recognize(self):
- self.img_cut('example.png')
- result1 = self.reco.recognizie("img1.jpg")
- result2 = self.reco.recognizie("img2.jpg")
- # self.ui.lbl_top1.setText('11')
- # self.ui.lbl_top1 = QLabel()
- self.ui.lbl_top1.setPixmap(QPixmap('img1.jpg'))
- self.ui.lbl_top1.setScaledContents(True) # 自适应QLabel大小
- self.ui.lbl_prob1.setText(F"{result1[0]}")
- # self.ui.lbl_top2.setText('22')
- # # self.ui.lbl_top2 = QLabel()
- self.ui.lbl_top2.setPixmap(QPixmap('img2.jpg'))
- self.ui.lbl_top2.setScaledContents(True)
- self.ui.lbl_prob2.setText(F"{result2[0]}")
- self.ui.lbl_result.setText(self.judgment('img1.jpg','img2.jpg'))
- # 进行判断
- def judgment(self,x,y):
- r1 = self.reco.recognizie(x)
- r2 = self.reco.recognizie(y)
- if r1[0] == r2[0]:
- return '平局!'
- elif r1[0] == '剪刀' and r2[0] == '布' or r1[0] == '布' and r2[0] == '石头' or r1[0] == '石头' and r2[0] == '剪刀':
- return '左方胜利!'
- elif r1[0] == '布' and r2[0] == '剪刀' or r1[0] == '剪刀' and r2[0] == '石头' or r1[0] == '石头' and r2[0] == '布':
- return '右方胜利!'
- # 抓取图像处理
- 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)
|