import cv2
import matplotlib.pyplot as plt
%matplotlib inline
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
image = cv2.imread('test image.jpg')
# convert to rgb
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
<matplotlib.image.AxesImage at 0x121354b80>
# resize the image
image = cv2.resize(image, (450, 600))
# convert to gray scale image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(gray, cmap='gray')
<matplotlib.image.AxesImage at 0x1253e7160>
faces = face_cascade.detectMultiScale(gray)
len(faces)
5
# diplay the faces in the image
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
plt.imshow(image)
<matplotlib.image.AxesImage at 0x125ca51c0>