OpenCV Face Detection Webcam

In this notebook, opencv face detection will be applied to webcam images.

To run all cells in this notebook a webcam and HDMI output monitor are required.


Step 1: Load the overlay

In [1]:
from pynq import Overlay

Step 2: Initialize Webcam and HDMI Out

In [2]:
# monitor configuration: 640*480 @ 60Hz
from import HDMI
hdmi_out = HDMI('out', video_mode=HDMI.VMODE_640x480)

In [3]:
# monitor (output) frame buffer size
frame_out_w = 1920
frame_out_h = 1080
# camera (input) configuration
frame_in_w = 640
frame_in_h = 480

In [4]:
# initialize camera from OpenCV
from import Frame
import cv2

videoIn = cv2.VideoCapture(0)
videoIn.set(cv2.CAP_PROP_FRAME_WIDTH, frame_in_w);
videoIn.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_in_h);

print("capture device is open: " + str(videoIn.isOpened()))

capture device is open: True

Step 3: Show input frame on HDMI output

In [5]:
# Capture webcam image
import numpy as np

ret, frame_vga =

# Display webcam image via HDMI Out
if (ret):
    frame_1080p = np.zeros((1080,1920,3)).astype(np.uint8)       
    frame_1080p[0:480,0:640,:] = frame_vga[0:480,0:640,:]
    print("Error while reading from camera")

Step 4: Now use matplotlib to show image inside notebook

In [6]:
# Output webcam image as JPEG
%matplotlib inline 
from matplotlib import pyplot as plt
import numpy as np

Step5: Apply the face detection to the input

In [7]:
import cv2

np_frame = frame_vga

face_cascade = cv2.CascadeClassifier(
eye_cascade = cv2.CascadeClassifier(

gray = cv2.cvtColor(np_frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = np_frame[y:y+h, x:x+w]

    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:

Step 6: Show results on HDMI output

In [8]:
# Output OpenCV results via HDMI
frame_1080p[0:480,0:640,:] = frame_vga[0:480,0:640,:]

Step 7: Now use matplotlib to show image inside notebook

In [9]:
# Output OpenCV results via matplotlib
%matplotlib inline 
from matplotlib import pyplot as plt
import numpy as np

Step 8: Release camera and HDMI

In [ ]:
del hdmi_out

In [ ]: