-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
154 lines (140 loc) · 7.08 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsPixmapItem, QFileDialog
import os
import cv2
from Hand.model_hand_2.model import HandModel
import torch
import numpy as np
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(537, 592)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.imageShowGraphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.imageShowGraphicsView.setGeometry(QtCore.QRect(10, 10, 511, 511))
self.imageShowGraphicsView.setObjectName("imageShowGraphicsView")
self.resultTextBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.resultTextBrowser.setGeometry(QtCore.QRect(318, 10, 201, 41))
self.resultTextBrowser.setObjectName("resultTextBrowser")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(11, 530, 511, 30))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.cameraButton = QtWidgets.QPushButton(self.widget)
self.cameraButton.setObjectName("cameraButton")
self.horizontalLayout.addWidget(self.cameraButton)
self.selectImageButton = QtWidgets.QPushButton(self.widget)
self.selectImageButton.setObjectName("selectImageButton")
self.horizontalLayout.addWidget(self.selectImageButton)
self.startPredictButton = QtWidgets.QPushButton(self.widget)
self.startPredictButton.setObjectName("startPredictButton")
self.horizontalLayout.addWidget(self.startPredictButton)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "手势识别"))
self.resultTextBrowser.setHtml(_translate("MainWindow",
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">预测结果为:None</span></p></body></html>"))
self.cameraButton.setText(_translate("MainWindow", "相机捕捉图像"))
self.cameraButton.clicked.connect(self.camera)
self.selectImageButton.setText(_translate("MainWindow", "选择图像"))
self.selectImageButton.clicked.connect(self.selectImage)
self.startPredictButton.setText(_translate("MainWindow", "开始预测"))
self.startPredictButton.clicked.connect(self.startPredict)
self.imageMethod = 0
def camera(self):
print('调用摄像头')
self.imageMethod = 1
capture = cv2.VideoCapture(0)
# 摄像头读取,ret为是否成功打开摄像头,true,false。
ret, frame = capture.read()
# frame为视频的每一帧图像,,对其进行翻转转到人类视角
frame = cv2.flip(frame, 1)
self.show('./test.png', frame)
# cv2.imwrite('./test.png', frame)
# print('save')
pass
def selectImage(self):
self.imageMethod = 0
print('选择图像')
try:
filename = QFileDialog.getOpenFileNames(None, '选择图像', './', "All Files(*);;image Files(*.png)")
self.filepath = filename[0][0]
self.show(self.filepath)
print(self.filepath)
except:
pass
def show(self, image_filepath, image=None):
image_shape = [505, 505]
if os.path.exists(image_filepath):
image = cv2.imread(image_filepath)
image = cv2.resize(image, (image_shape[0], image_shape[1]))
self.image = image
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = QImage(image.data, image_shape[0], image_shape[1], image_shape[0] * 3, QImage.Format_RGB888)
image_pix = QPixmap.fromImage(image)
image_item = QGraphicsPixmapItem(image_pix) # 创建像素图元
image_scene = QGraphicsScene() # 创建场景
image_scene.addItem(image_item)
self.imageShowGraphicsView.setScene(image_scene)
else:
image = cv2.resize(image, (image_shape[0], image_shape[1]))
self.image = image
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = QImage(image.data, image_shape[0], image_shape[1], image_shape[0] * 3, QImage.Format_RGB888)
image_pix = QPixmap.fromImage(image)
image_item = QGraphicsPixmapItem(image_pix) # 创建像素图元
image_scene = QGraphicsScene() # 创建场景
image_scene.addItem(image_item)
self.imageShowGraphicsView.setScene(image_scene)
def startPredict(self):
print('开始预测')
label2id = {
'ok': 0,
'yes': 1,
'eight': 2,
'five': 3,
'four': 4,
'three': 5
}
id2label = {}
for label, id in label2id.items():
id2label[id] = label
model = HandModel(classes=6)
model.load_state_dict(torch.load('./savemodel.pth'))
self.image = cv2.resize(self.image, (100, 100))
image = np.array(self.image)
image = np.transpose(image, [-1, 0, 1])
image = np.expand_dims(image, axis=0)
image = image / 255
image = torch.from_numpy(image)
image = image.type(torch.float32)
if torch.cuda.is_available():
image = image.cuda()
model = model.cuda()
model.eval()
pred = model(image)
id = torch.argmax(pred, dim=-1).cpu().numpy()[0]
print(id2label[id])
self.resultTextBrowser.setText("预测结果是:{}".format(id2label[id]))
# self.resultTextBrowser.setFont()