Boundary Boxes in Video¶

Import Libraries¶

In [1]:
import cv2 as cv
import numpy as np
from IPython.display import display, clear_output
import PIL.Image
import time

Function To Draw Boundary Box Around Faces in Video¶

In [2]:
def detectFaces(video_file):
    cap = cv.VideoCapture(video_file)

    face_cascade = cv.CascadeClassifier(cv.data.haarcascades + 'haarcascade_frontalface_default.xml')

    while True:
        ret, frame = cap.read()
        gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=8, minSize=(30, 30))
        for (x, y, w, h) in faces:
            cv.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        frame_rgb = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
        img = PIL.Image.fromarray(frame_rgb)
        display(img)   
        clear_output(wait=True)
        
    cap.release()

Function Call¶

In [ ]:
detectFaces('JimMoving.mp4')
No description has been provided for this image
In [ ]: