-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvm1.py
157 lines (120 loc) · 4.53 KB
/
cvm1.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
155
156
157
#!/usr/bin/env python
from PySide.QtCore import *
from PySide.QtGui import *
import cv2
import sys
CASCADE_FILE = 'haarcascade_frontalface_default.xml'
class MainWindow(QWidget):
frame_changed = Signal(object)
def __init__(self):
QWidget.__init__(self)
self.video_width = 320
self.video_height = 240
self.face_rect = None
self.face_detector = FaceDetector()
self.frame_changed.connect(self.face_detector.detect)
self.face_detector.on_detect.connect(self.update_face_rect)
self.fps_counter = FpsCounter()
self.fps_counter.on_count.connect(self.update_status)
self.frame_changed.connect(self.fps_counter.count_frame)
self.setup_ui()
self.face_detector.start()
self.fps_counter.start()
def closeEvent(self, event):
self.face_detector.terminate()
self.fps_counter.terminate()
event.accept()
def setup_ui(self):
self.image_label = QLabel()
self.setFixedWidth(self.video_width)
self.setFixedHeight(self.video_height)
self.blank = QPixmap(self.video_width, self.video_height)
self.blank.fill(QColor(0,0,0))
self.image_label.setPixmap(self.blank)
self.status_label = QLabel("Initializing camera...")
self.detect_button = QPushButton("Detect Face")
self.detect_button.setCheckable(True)
self.detect_button.toggled.connect(self.face_detector.activate)
self.quit_button = QPushButton("Quit")
self.quit_button.clicked.connect(self.close)
buttons_layout = QHBoxLayout()
buttons_layout.addWidget(self.status_label)
buttons_layout.addStretch()
buttons_layout.addWidget(self.detect_button)
buttons_layout.addWidget(self.quit_button)
main_layout = QVBoxLayout()
main_layout.addWidget(self.image_label)
main_layout.addLayout(buttons_layout)
self.setWindowTitle("Face Detection")
self.setLayout(main_layout)
self.show()
self.timer = QTimer()
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.setup_camera)
self.timer.start(10)
def setup_camera(self):
self.capture = cv2.VideoCapture(0)
self.capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, self.video_width)
self.capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, self.video_height)
self.timer = QTimer()
self.timer.timeout.connect(self.display_camera_stream)
self.timer.start(50)
def display_camera_stream(self):
val, frame = self.capture.read()
frame = cv2.flip(frame, 1)
self.frame_changed.emit(frame)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if self.face_rect is not None:
x, y, w, h = self.face_rect
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888)
self.image_label.setPixmap(QPixmap.fromImage(image))
@Slot(object)
def update_face_rect(self, rect):
self.face_rect = rect
@Slot(str)
def update_status(self, msg):
self.status_label.setText(msg)
class FaceDetector(QThread):
on_detect = Signal(object)
def __init__(self):
QThread.__init__(self)
self.stopped = True
self.processing = False
self.face_cascade = cv2.CascadeClassifier(CASCADE_FILE)
@Slot()
def activate(self, val):
self.stopped = not val
self.on_detect.emit(None)
@Slot(object)
def detect(self, frame):
if not self.stopped and not self.processing:
self.processing = True
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, 1.1, 2)
self.on_detect.emit(faces[0] if len(faces) else None)
self.processing = False
class FpsCounter(QThread):
on_count = Signal(str)
def __init__(self):
QThread.__init__(self)
self.num_frames = 0
self.mutex = QMutex()
self.timer = QTimer()
self.timer.timeout.connect(self.timeout)
self.timer.start(1000)
@Slot()
def count_frame(self):
self.mutex.lock()
self.num_frames += 1
self.mutex.unlock()
@Slot()
def timeout(self):
self.mutex.lock()
self.on_count.emit("Camera: %d fps" % self.num_frames)
self.num_frames = 0
self.mutex.unlock()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
app.exec_()