-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqtapp.py
More file actions
124 lines (98 loc) · 3.52 KB
/
qtapp.py
File metadata and controls
124 lines (98 loc) · 3.52 KB
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
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QMainWindow, QApplication, QPushButton, QLabel, QVBoxLayout
import cv2
from mtcnn_model import mark_faces_mtcnn
class QtCapture(QWidget):
def __init__(self, *args):
super(QWidget, self).__init__()
self.fps = 24
self.cap = cv2.VideoCapture(*args)
self.video_frame = QLabel()
lay = QVBoxLayout()
lay.setContentsMargins(0,0,0,0)
lay.addWidget(self.video_frame)
self.setLayout(lay)
# ------ Modification ------ #
self.isCapturing = False
self.ith_frame = 1
# ------ Modification ------ #
def setFPS(self, fps):
self.fps = fps
def nextFrameSlot(self):
ret, frame = self.cap.read()
# ------ Modification ------ #
# Save images if isCapturing
if self.isCapturing:
cv2.imwrite('img_%05d.jpg'%self.ith_frame, frame)
self.ith_frame += 1
# ------ Modification ------ #
# My webcam yields frames in BGR format
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# face detection
frame = mark_faces_mtcnn(frame)
img = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)
pix = QtGui.QPixmap.fromImage(img)
self.video_frame.setPixmap(pix)
def start(self):
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.nextFrameSlot)
self.timer.start(1000./self.fps)
def stop(self):
self.timer.stop()
# ------ Modification ------ #
def capture(self):
if not self.isCapturing:
self.isCapturing = True
else:
self.isCapturing = False
# ------ Modification ------ #
def deleteLater(self):
self.cap.release()
super(QWidget, self).deleteLater()
class ControlWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.capture = None
self.start_button = QPushButton('Start')
self.start_button.clicked.connect(self.startCapture)
self.quit_button = QPushButton('End')
self.quit_button.clicked.connect(self.endCapture)
self.end_button = QPushButton('Stop')
# ------ Modification ------ #
self.capture_button = QPushButton('Capture')
self.capture_button.clicked.connect(self.saveCapture)
# ------ Modification ------ #
vbox = QVBoxLayout(self)
vbox.addWidget(self.start_button)
vbox.addWidget(self.end_button)
vbox.addWidget(self.quit_button)
# ------ Modification ------ #
vbox.addWidget(self.capture_button)
# ------ Modification ------ #
self.setLayout(vbox)
self.setWindowTitle('Control Panel')
self.setGeometry(100,100,200,200)
self.show()
def startCapture(self):
if not self.capture:
self.capture = QtCapture(0)
self.end_button.clicked.connect(self.capture.stop)
# self.capture.setFPS(1)
self.capture.setParent(self)
self.capture.setWindowFlags(QtCore.Qt.Tool)
self.capture.start()
self.capture.show()
def endCapture(self):
self.capture.deleteLater()
self.capture = None
# ------ Modification ------ #
def saveCapture(self):
if self.capture:
self.capture.capture()
# ------ Modification ------ #
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = ControlWindow()
sys.exit(app.exec_())