The steps for this recipe are as follows:
- Import the libraries and settings:
import cv2
from time import sleep
debugging = True
classifier = \
cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
video = cv2.VideoCapture(0)
- Initialize the camera:
while True:
if not video.isOpened():
print('Waiting for Camera.')
sleep(5)
pass
- Capture and transform the image:
ret, frame = video.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- Classify the image:
faces = classifier.detectMultiScale(gray,
minNeighbors=5,
minSize=(100, 100)
)
- Debug the images:
if debugging:
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
- Detect the face:
if len(faces...