Python用dlib实现 视频人脸检测 (初代版)

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:admin@192.168.1.139: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()

 

上一篇:Object detect


下一篇:Jenkins使用Git Publisher报fatal: unable to auto-detect email address