16
loading...
This website collects cookies to deliver better user experience
pip install opencv-python
import cv2
import matplotlib.pyplot as plt # for plotting the image
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('test.png')
# Detect faces
faces = face_cascade.detectMultiScale(image = img, scaleFactor = 1.1, minNeighbors = 5)
# Draw bounding box around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Showing number of faces detected in the image
print(len(faces),"faces detected!")
# Plotting the image with face detected
finalimg = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
plt.figure(figsize=(12,12))
plt.imshow(finalimg)
plt.axis("off")
plt.show()
Note: This tutorial is only meant for face detection in an image file and not a live camera feed or videos.