Python用dlib實現 視訊人臉檢測 (初代版)
阿新 • • 發佈:2020-10-23
import sys import cv2 import dlib def _help(): print("Usage:") print(" python video_face_detect_dlib.py") print(" python video_face_detect_dlib.py <path of a video>") print("For example:") print(" python video_face_detect_dlib.py video/lee.mp4") print("If the path of a video is not provided, the camera will be used as the input.Press q to quit.") def _face_detect(color_image, detector): gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY) # img_row, img_col = gray_image.shape[:2] # scale = 0.3 # gray_image = cv2.resize(gray_image, (int(scale * img_col), int(scale * img_row))) faces = detector(gray_image, 1)for face in faces: left = face.left() top = face.top() right = face.right() bottom = face.bottom() cv2.rectangle(color_image, (left, top), (right, bottom), (0, 255, 0), 2) cv2.namedWindow("Image", cv2.WINDOW_NORMAL) #cv2.imshow("Image", color_image)cv2.imshow("Image", color_image) def face_detect(video_path=0): detector = dlib.get_frontal_face_detector() video = "http://admin:[email protected]:8081/" # 此處@後的ipv4 地址需要改為app提供的地址 0表示筆記本自帶攝像頭 cap = cv2.VideoCapture(video) while True: ret, img = cap.read() if img is None: break _face_detect(img, detector) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() if len(sys.argv) > 2 or "-h" in sys.argv or "--help" in sys.argv: _help() elif len(sys.argv) == 2: face_detect(sys.argv[1]) else: face_detect()