人脸检测-摄像头实时(OpenCV)

# -*- coding: utf-8 -*-
# https://github.com/windandscreen/face_detection
import datetime
import cv2


def detection(img):
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # OpenCV人脸识别分类器
    classifier = cv2.CascadeClassifier("opencv_xml/haarcascade_frontalface_default.xml")
    color = (0, 255, 0)  # 定义绘制颜色

    # 调用识别人脸
    faceRects = classifier.detectMultiScale(grayImg, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))

    if len(faceRects):  # 大于0则检测到人脸
        for faceRect in faceRects:  # 单独框出每一张人脸
            x, y, w, h = faceRect
            # 框出人脸
            cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
    cv2.imshow("image", img)  # 显示图像


def main():
    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        detection(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

    return 0


if __name__ == '__main__':
    tic = datetime.datetime.now()
    main()
    toc = datetime.datetime.now()
    print(toc - tic)

你可能感兴趣的:(人脸检测)