| 123 | self.deleteLater() |
| 124 | |
| 125 | def onCapture(self): |
| 126 | _, frame = self.cap.read() |
| 127 | |
| 128 | minisize = ( |
| 129 | int(frame.shape[1] / DOWNSCALE), int(frame.shape[0] / DOWNSCALE)) |
| 130 | tmpframe = cv2.resize(frame, minisize) |
| 131 | tmpframe = cv2.cvtColor(tmpframe, cv2.COLOR_BGR2GRAY) # 做灰度处理 |
| 132 | tmpframe = cv2.equalizeHist(tmpframe) |
| 133 | |
| 134 | # minNeighbors表示每一个目标至少要被检测到5次 |
| 135 | faces = self.cascade.detectMultiScale(tmpframe, minNeighbors=5) |
| 136 | del tmpframe |
| 137 | if len(faces) < 1: # 没有检测到脸 |
| 138 | frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| 139 | img = QImage( |
| 140 | frame.data, frame.shape[1], frame.shape[0], frame.shape[1] * 3, QImage.Format_RGB888) |
| 141 | del frame |
| 142 | return self.setPixmap(QPixmap.fromImage(img)) |
| 143 | # 特征点检测描绘 |
| 144 | for x, y, w, h in faces: |
| 145 | x, y, w, h = x * DOWNSCALE, y * DOWNSCALE, w * DOWNSCALE, h * DOWNSCALE |
| 146 | # 画脸矩形 |
| 147 | cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0)) |
| 148 | # 截取的人脸部分 |
| 149 | tmpframe = frame[y:y + h, x:x + w] |
| 150 | # 进行特征点描绘 |
| 151 | rects = self.detector(tmpframe, 1) |
| 152 | if len(rects) > 0: |
| 153 | landmarks = numpy.matrix( |
| 154 | [[p.x, p.y] for p in self.predictor(tmpframe, rects[0]).parts()]) |
| 155 | for _, point in enumerate(landmarks): |
| 156 | pos = (point[0, 0] + x, point[0, 1] + y) |
| 157 | # 在原来画面上画点 |
| 158 | cv2.circle(frame, pos, 3, color=(0, 255, 0)) |
| 159 | # 转成Qt能显示的 |
| 160 | frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| 161 | img = QImage( |
| 162 | frame.data, frame.shape[1], frame.shape[0], frame.shape[1] * 3, QImage.Format_RGB888) |
| 163 | del frame |
| 164 | self.setPixmap(QPixmap.fromImage(img)) |
| 165 | |
| 166 | |
| 167 | if __name__ == "__main__": |