
Introduction
Code on Github: Download
This tutorial explains how to perform face detection with OpenCV using Haar Cascade Classifiers. This tutorial is made of two videos:
Summary
In short, this piece of code does all the magic:
import cv2
fileInput = "quot;./image.jpg"quot;
fileOutput = "quot;./output.jpg"quot;
GREEN = (99, 255, 55)
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "quot;haarcascade_frontalface_default.xml"quot;)
image = cv2.imread(fileInput)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faceRects = faceCascade.detectMultiScale(
gray,
scaleFactor=1.05,
minNeighbors=21,
minSize=(200,200)
)
for rect in faceRects:
cv2.rectangle(image, rect, GREEN, 20)
cv2.imwrite(fileOutput, image)