def main(): bino_cam = BinoCamera(0, True) bino_cam.set_width_height((2560, 720)) face_detector = FaceDetector() # The width of frame is the half of camera width. distance_predictor = DistancePredictor(90, bino_cam.get_width_height()[0] / 2, bino_cam.get_width_height()[1], 60) distance = 0 distance_threshold = 2000.0 #mm monitor_flag = True while True: if not bino_cam.capture(): break Drawer.draw_lines(bino_cam.get_lframe()) Drawer.draw_lines(bino_cam.get_rframe()) left_face = face_detector.detect_face(bino_cam.get_lframe()) right_face = face_detector.detect_face(bino_cam.get_rframe()) if left_face: Drawer.dotted_rectangle(bino_cam.get_lframe(), (left_face.get_x(), left_face.get_y()), (left_face.get_x() + left_face.get_width(), left_face.get_y() + left_face.get_height()), (0, 0, 255), 2) if right_face: Drawer.dotted_rectangle(bino_cam.get_rframe(), (right_face.get_x(), right_face.get_y()), (right_face.get_x() + right_face.get_width(), right_face.get_y() + right_face.get_height()), (0, 0, 255), 2) if left_face and right_face: distance = distance_predictor.predict_distance((left_face.get_x(), left_face.get_y()), (right_face.get_x(), right_face.get_y())) Drawer.text(bino_cam.get_lframe(), "Distance(m): %.2fm" % (distance / 1000), (20, 40), 1.0, (0, 0, 255), 2) print(distance / 1000) if 0 < distance < distance_threshold and monitor_flag: Controller.set_top_window("Unity 2019.1.8f1 - mainScene.unity - Learn unity editor script - PC, Mac & Linux Standalone <DX11>") monitor_flag = False cv2.imshow("I am a good stuff v1.0 @ FPS: %d" % bino_cam.get_fps(), np.hstack((bino_cam.get_lframe(), bino_cam.get_rframe()))) if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows()
# Test function if __name__ == '__main__': # Test Camera camera = MonoCamera(0) camera.set_width_height((1280, 720)) face_detector = FaceDetector() while True: if not camera.capture(): break frame = camera.get_frame() Drawer.draw_lines(frame) face = face_detector.detect_face(frame) if face: x, y, w, h = face.get_rect() Drawer.dotted_rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.imshow("test Face Recognize", frame) if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows() # # Test Img # img = cv2.imread("assets/timg2.jpg") # face_detector = FaceDetector() # face = face_detector.detect_face(img) # if face: # x, y, w, h = face.get_rect() # cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1) # cv2.imshow("test Face Recognize", img) # cv2.waitKey(0)