ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2020-10-23 20:02:29  阅读:32  来源: 互联网

标签:detect img Python image cv2 face video 初代版 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()

 

标签:detect,img,Python,image,cv2,face,video,初代版,dlib
来源: https://www.cnblogs.com/aotumandaren/p/13865906.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有