python+OpenCV3 实现人脸检测 实时读取视频流

python代码

import cv2 as cv
import numpy as np


def face_detect_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    face_detector = cv.CascadeClassifier("haarcascade_frontalface_alt.xml")
    faces = face_detector.detectMultiScale(gray, 1.1, 2)
    for x, y, w, h in faces:
        cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
    cv.imshow("result", image)


print("--------- Python OpenCV Tutorial ---------")
capture = cv.VideoCapture(0)
cv.namedWindow("result", cv.WINDOW_AUTOSIZE)
while (True):
    ret, frame = capture.read()
    frame = cv.flip(frame, 1)
    face_detect_demo(frame)
    c = cv.waitKey(10)
    if c == 27:  # ESC
        break
cv.waitKey(0)

cv.destroyAllWindows()

XML文件:https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_alt.xml

发布了203 篇原创文章 · 获赞 66 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/IT_flying625/article/details/104697727