Last active
June 12, 2022 18:53
-
-
Save adaltof/bb291ed979000ea3a8e9df44b6838a4e to your computer and use it in GitHub Desktop.
Function to add Haar feature-based cascade classifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import matplotlib.pyplot as plt | |
%matplotlib inline | |
# extract pre-trained face detector | |
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml') | |
def detect_human_face(img): | |
# convert BGR image to grayscale | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
# find faces in image | |
faces = face_cascade.detectMultiScale(gray) | |
# print number of faces detected in the image | |
print('Number of faces detected:', len(faces)) | |
# get bounding box for each detected face | |
for (x,y,w,h) in faces: | |
# add bounding box to color image | |
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) | |
# convert BGR image to RGB for plotting | |
cv_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
# display the image, along with bounding box | |
plt.imshow(cv_rgb) | |
plt.show() | |
img = cv2.imread(human_files[100]) | |
detect_human_face(img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment