1. 程式人生 > >OpenCV找出視訊檔案中的人臉

OpenCV找出視訊檔案中的人臉

程式碼位置:22-GetFaceFromMovie.py

import cv2
import os

cameraCapture = cv2.VideoCapture('./res/test.mp4')

path = os.getcwd() + '/XML/haarcascade_frontalface_alt_tree.xml'
detector = cv2.CascadeClassifier(path)

cv2.namedWindow('Test camera')
success, frame = cameraCapture.read()
while success:
    if cv2.waitKey(1) == 27:
        break
    success, frame = cameraCapture.read()
    rects = detector.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=2, minSize=(100, 100),
                                      flags=cv2.CASCADE_SCALE_IMAGE)
    if len(rects) > 0:
        for (x, y, w, h) in rects:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 1)
        cv2.imshow('Test camera', frame)

cv2.destroyAllWindows()
cameraCapture.release()
  • 這裡是結合前面所講的從攝像頭讀資料的那個Demo來的。
  • 從視訊檔案中讀出貞資料,其實就是一張圖片,然後對這張圖片做人臉檢測。
  • minSize=(100, 100) 這引數可以修改。明敏度不同。
    在這裡插入圖片描述