Skip to content

Commit 89eecdf

Browse files
Sylwester DawidaSylwester Dawida
Sylwester Dawida
authored and
Sylwester Dawida
committed
camera interface added to gui
1 parent a7c9722 commit 89eecdf

File tree

4 files changed

+160
-91
lines changed

4 files changed

+160
-91
lines changed

python/.idea/workspace.xml

+2-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
3.58 KB
Binary file not shown.

python/gui.py

+42-6
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,24 @@
1010
from PyQt5 import QtCore, QtGui, QtWidgets
1111
from comunication import Commands
1212
from datetime import datetime
13-
14-
start_time = datetime.now()
13+
from main import GestureRecognition, Signals
14+
from PyQt5.QtCore import QObject, pyqtSignal
15+
from PyQt5.QtCore import *
16+
import time
1517
import sys
1618
from PyQt5.QtWidgets import QDialog, QApplication
1719

1820

19-
class Ui_RobotController(object):
21+
class Ui_RobotController(QObject):
22+
23+
def closeNeuralNetworkThread(self):
24+
self.commands.disconnect()
25+
self.recognition.stop()
26+
self.recognition.wait()
27+
2028
def setupUi(self, RobotController):
2129
RobotController.setObjectName("RobotController")
22-
RobotController.resize(1335, 799)
30+
RobotController.resize(1400, 800)
2331
self.centralwidget = QtWidgets.QWidget(RobotController)
2432
self.centralwidget.setObjectName("centralwidget")
2533
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
@@ -51,8 +59,16 @@ def setupUi(self, RobotController):
5159
font.setPointSize(13)
5260
self.label.setFont(font)
5361
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
62+
63+
self.fps = QtWidgets.QLabel(self.centralwidget)
64+
self.fps.setGeometry(QtCore.QRect(500, 50, 381, 61))
65+
font = QtGui.QFont()
66+
font.setPointSize(13)
67+
self.fps.setFont(font)
68+
self.fps.setLayoutDirection(QtCore.Qt.LeftToRight)
69+
5470
self.label.setObjectName("label")
55-
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
71+
self.graphicsView = QtWidgets.QLabel(self.centralwidget)
5672
self.graphicsView.setGeometry(QtCore.QRect(500, 60, 800, 600))
5773
self.graphicsView.setObjectName("graphicsView")
5874
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
@@ -88,14 +104,27 @@ def setupUi(self, RobotController):
88104

89105
self.pushButton_2.clicked.connect(self.send)
90106

107+
self.recognition = GestureRecognition(self.showFps)
108+
self.recognition.setTerminationEnabled(True)
109+
self.recognition.signals.result.connect(self.getRecognitionResult)
110+
111+
self.finished = self.recognition.signals.finished
112+
113+
self.recognition.start()
114+
self.print("next")
115+
91116
def retranslateUi(self, RobotController):
92117
_translate = QtCore.QCoreApplication.translate
93118
RobotController.setWindowTitle(_translate("RobotController", "Robot Controller"))
94119
self.pushButton.setText(_translate("RobotController", "Connect"))
95120
self.pushButton_2.setText(_translate("RobotController", "Send"))
96121
self.label.setText(_translate("RobotController", "Information"))
122+
self.fps.setText("Fps: 0")
97123
self.label_2.setText(_translate("RobotController", "Manual commands"))
98124

125+
def showFps(self, fps):
126+
self.fps.setText("Fps: {}".format(fps))
127+
99128
def connect(self):
100129
if self.connectionInProgress:
101130
return
@@ -131,13 +160,20 @@ def print(self, str, unlock=0):
131160
self.connectionInProgress = False
132161
self.pushButton.setText("Connect")
133162

163+
def getRecognitionResult(self, image, output):
164+
height, width, byteValue = image.shape
165+
image = QtGui.QImage(image, image.shape[1], image.shape[0], image.shape[1] * 3, QtGui.QImage.Format_RGB888)
166+
pix = QtGui.QPixmap(image)
167+
self.graphicsView.setPixmap(pix)
168+
134169

135170
if __name__ == '__main__':
136171
import sys
137172

173+
ui = Ui_RobotController()
138174
app = QtWidgets.QApplication(sys.argv)
175+
app.aboutToQuit.connect(ui.closeNeuralNetworkThread)
139176
window = QtWidgets.QMainWindow()
140-
ui = Ui_RobotController()
141177
ui.setupUi(window)
142178
window.show()
143179
sys.exit(app.exec_())

python/main.py

+116-77
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,124 @@
1+
from _testbuffer import ndarray
2+
13
import cv2
24
from neuralNetwork import Net2, Net
35
import openCvTranforms.opencv_transforms.transforms as tf
46
import torchvision.transforms as transforms
5-
from timeit import default_timer as timer
67
import torch
7-
import numpy as np
8+
from PyQt5.QtCore import *
9+
from time import time
810

911
# functions to show an image
12+
class Signals(QObject):
13+
'''
14+
Defines the signals available from a running worker thread.
15+
16+
Supported signals are:
17+
18+
finished
19+
No data
20+
21+
error
22+
`tuple` (exctype, value, traceback.format_exc() )
23+
24+
result
25+
`object` data returned from processing, anything
26+
27+
'''
28+
finished = pyqtSignal()
29+
error = pyqtSignal(tuple)
30+
result = pyqtSignal(object, torch.Tensor)
31+
32+
33+
class GestureRecognition(QThread):
34+
"""main class with neural network"""
35+
36+
def __init__(self, print):
37+
super(GestureRecognition, self).__init__()
38+
self.width = 80
39+
self.heigh = 60
40+
self.colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
41+
self.labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17',
42+
'18', '19',
43+
'20', '21', '22', '23', '24', '25', '26']
44+
self.maxLen = 200
45+
self.step = 20
46+
self.offset = 5
47+
self.printFps = print
48+
self.thicc = 10
49+
self.device = cv2.VideoCapture(0, cv2.CAP_DSHOW)
50+
self.net = Net(self.width, self.heigh)
51+
self.transform = transforms.Compose(
52+
[tf.Grayscale(),
53+
tf.Resize((self.heigh, self.width)),
54+
tf.ToTensor(),
55+
tf.Normalize((0.5,), (0.5,))
56+
])
57+
self.signals = Signals()
58+
self.running = True
59+
self.signals.finished.connect(self.stop)
60+
61+
def stop(self):
62+
self.running = False
63+
64+
def drawStatistics(self, image, stats, chosen):
65+
for i, data in enumerate(stats.view(-1)):
66+
imm = 10
67+
cv2.rectangle(image, (self.offset, i * self.step),
68+
(self.offset + (data * self.maxLen), (i * self.step) + self.thicc), self.colors[i % 3], -1)
69+
cv2.putText(image, self.labels[i], (self.offset, i * self.step + 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
70+
fontScale=0.5,
71+
color=(255, 255, 255), thickness=1, lineType=-1)
72+
nnm = 20
73+
74+
def imshow(self, img):
75+
img = img.view(self.heigh, self.width, 1).numpy() / 2 + 0.5 # unnormalize
76+
print(img.shape)
77+
# npimg = img.numpy()
78+
# cv2.imshow("preview", img)
79+
80+
def run(self):
81+
scale = 2.5
82+
# width = vc.get(3) # 640
83+
# heigh = vc.get(4) # 480
84+
# fps = vc.get(5) # 60
85+
86+
# new settings
87+
88+
self.device.set(5, 25) # set fps
89+
#print(self.device.get(16))
90+
#self.device.set(16, 1) #set to rgb
91+
92+
#print("width: " + str(self.device.get(3)) + " heigh: " + str(self.device.get(4)))
93+
94+
self.net = torch.load("./savedMode2.pth")
95+
self.net.eval()
1096

11-
width = 80
12-
heigh = 60
13-
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
14-
labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
15-
'20', '21', '22', '23', '24', '25', '26'];
16-
maxLen = 200
17-
step = 20
18-
offset = 5
19-
thicc = 10
20-
21-
22-
def drawStatistics(image, stats, chosen):
23-
for i, data in enumerate(stats.view(-1)):
24-
imm = 10
25-
cv2.rectangle(image, (offset, i * step), (offset + (data * maxLen), (i * step) + thicc), colors[i % 3], -1)
26-
cv2.putText(image, labels[i], (offset, i * step + 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5,
27-
color=(255, 255, 255), thickness=1, lineType=-1)
28-
nnm = 20
29-
30-
31-
def imshow(img):
32-
img = img.view(heigh, width, 1).numpy() / 2 + 0.5 # unnormalize
33-
print(img.shape)
34-
# npimg = img.numpy()
35-
cv2.imshow("preview", img)
36-
37-
38-
cv2.namedWindow("preview")
39-
vc = cv2.VideoCapture(0, cv2.CAP_DSHOW)
40-
scale = 2.5
41-
# width = vc.get(3) # 640
42-
# heigh = vc.get(4) # 480
43-
# fps = vc.get(5) # 60
44-
45-
# new settings
46-
47-
vc.set(5, 24) # set fps
48-
49-
print("width: " + str(vc.get(3)) + " heigh: " + str(vc.get(4)))
50-
net = Net(width, heigh)
51-
net = torch.load("./savedMode2.pth")
52-
net.eval()
53-
54-
transform = transforms.Compose(
55-
[tf.Grayscale(),
56-
tf.Scale((heigh, width)),
57-
tf.ToTensor(),
58-
tf.Normalize((0.5,), (0.5,))
59-
])
60-
61-
if vc.isOpened(): # try to get the first frame
62-
rval, frame = vc.read()
63-
print(frame.shape)
64-
else:
65-
rval = False
66-
out = torch.zeros(27)
67-
while rval:
68-
start = timer()
69-
rval, frame = vc.read()
70-
# frame = cv2.imread(r'./kinect_leap_dataset/acquisitions/P1/G1/0_rgb.png')
71-
# print(frame.shape)
72-
frameWithStats = frame.copy()
73-
drawStatistics(frameWithStats, out, 1)
74-
cv2.imshow("preview", frameWithStats)
75-
frame = transform(frame).view(-1, 1, heigh, width)
76-
print(frame.shape)
77-
# imshow(frame)
78-
out = net(frame)
79-
print(out.shape)
80-
end = timer()
81-
print(end - start)
82-
key = cv2.waitKey(1)
83-
if key == 27: # exit on ESC
84-
break # todo change to kayboard real time check
85-
cv2.destroyWindow("preview")
97+
if self.device.isOpened(): # try to get the first frame
98+
rval, frame = self.device.read()
99+
# print(frame.shape)
100+
else:
101+
rval = False
102+
out = torch.zeros(27)
103+
start = time()
104+
fps = 0
105+
while rval and self.running:
106+
if time() - start < 1:
107+
fps += 1
108+
else:
109+
start = time()
110+
self.printFps(fps)
111+
fps = 0
112+
rval, frame = self.device.read()
113+
# frame = cv2.imread(r'./kinect_leap_dataset/acquisitions/P1/G1/0_rgb.png')
114+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
115+
frameWithStats = frame.copy()
116+
#print(frameWithStats.shape)
117+
self.drawStatistics(frameWithStats, out, 1)
118+
# cv2.imshow("preview", frameWithStats)
119+
frame = self.transform(frame).view(-1, 1, self.heigh, self.width)
120+
#print(frame.shape)
121+
#imshow(frame)
122+
out = self.net(frame)
123+
self.signals.result.emit(frameWithStats, out)
124+
#print(out.shape)

0 commit comments

Comments
 (0)