IMPORT PACKAGES
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
%matplotlib inline
print ("Packages loaded.")
Packages loaded.
FACE DETECTOR
cwd = os.getcwd()
clsf_path = cwd + "/data/haarcascade_frontalface_default.xml"
face_cascade = cv2.CascadeClassifier(clsf_path)
print ("face_cascade is %s" % (face_cascade))
face_cascade is <CascadeClassifier 0x7fe089352090>
LOAD IMAGE WITH FACES
imgpath = cwd + "/images/celebs.jpg"
img_bgr = cv2.imread(imgpath)
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
DETECT FACE
faces = face_cascade.detectMultiScale(img_gray)
print ("%d faces deteced. " % (len(faces)))
7 faces deteced.
PLOT DETECTED FACES
plt.figure(0)
plt.imshow(img_gray, cmap=plt.get_cmap("gray"))
ca = plt.gca()
for face in faces:
ca.add_patch(Rectangle((face[0], face[1]), face[2], face[3]
, fill=None, alpha=1, edgecolor='red'))
plt.title("Face detection with Viola-Jones")
plt.draw()
![png](basic_opencv2_files/basic_opencv2_10_0.png)
DETECT FACES IN THE IMAGES IN A FOLDER
path = cwd + "/../../img_dataset/celebs/Arnold_Schwarzenegger"
flist = os.listdir(path)
valid_exts = [".jpg",".gif",".png",".tga", ".jpeg"]
for f in flist:
if os.path.splitext(f)[1].lower() not in valid_exts:
continue
fullpath = os.path.join(path, f)
img_bgr = cv2.imread(fullpath)
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(img_gray)
plt.imshow(img_gray, cmap=plt.get_cmap("gray"))
ca = plt.gca()
for face in faces:
ca.add_patch(Rectangle((face[0], face[1]), face[2], face[3]
, fill=None, alpha=1, edgecolor='red'))
plt.title("Face detection with Viola-Jones")
plt.show()
![png](basic_opencv2_files/basic_opencv2_12_0.png)
![png](basic_opencv2_files/basic_opencv2_12_1.png)
![png](basic_opencv2_files/basic_opencv2_12_2.png)
![png](basic_opencv2_files/basic_opencv2_12_3.png)
![png](basic_opencv2_files/basic_opencv2_12_4.png)
![png](basic_opencv2_files/basic_opencv2_12_5.png)
![png](basic_opencv2_files/basic_opencv2_12_6.png)
![png](basic_opencv2_files/basic_opencv2_12_7.png)
![png](basic_opencv2_files/basic_opencv2_12_8.png)
![png](basic_opencv2_files/basic_opencv2_12_9.png)
![png](basic_opencv2_files/basic_opencv2_12_10.png)
![png](basic_opencv2_files/basic_opencv2_12_11.png)
![png](basic_opencv2_files/basic_opencv2_12_12.png)
![png](basic_opencv2_files/basic_opencv2_12_13.png)
![png](basic_opencv2_files/basic_opencv2_12_14.png)
![png](basic_opencv2_files/basic_opencv2_12_15.png)
![png](basic_opencv2_files/basic_opencv2_12_16.png)
![png](basic_opencv2_files/basic_opencv2_12_17.png)
![png](basic_opencv2_files/basic_opencv2_12_18.png)
![png](basic_opencv2_files/basic_opencv2_12_19.png)
![png](basic_opencv2_files/basic_opencv2_12_20.png)
![png](basic_opencv2_files/basic_opencv2_12_21.png)
![png](basic_opencv2_files/basic_opencv2_12_22.png)
![png](basic_opencv2_files/basic_opencv2_12_23.png)
![png](basic_opencv2_files/basic_opencv2_12_24.png)
![png](basic_opencv2_files/basic_opencv2_12_25.png)
![png](basic_opencv2_files/basic_opencv2_12_26.png)
![png](basic_opencv2_files/basic_opencv2_12_27.png)
![png](basic_opencv2_files/basic_opencv2_12_28.png)
![png](basic_opencv2_files/basic_opencv2_12_29.png)
![png](basic_opencv2_files/basic_opencv2_12_30.png)
![png](basic_opencv2_files/basic_opencv2_12_31.png)
![png](basic_opencv2_files/basic_opencv2_12_32.png)
![png](basic_opencv2_files/basic_opencv2_12_33.png)
![png](basic_opencv2_files/basic_opencv2_12_34.png)
![png](basic_opencv2_files/basic_opencv2_12_35.png)
![png](basic_opencv2_files/basic_opencv2_12_36.png)
![png](basic_opencv2_files/basic_opencv2_12_37.png)
![png](basic_opencv2_files/basic_opencv2_12_38.png)
![png](basic_opencv2_files/basic_opencv2_12_39.png)
![png](basic_opencv2_files/basic_opencv2_12_40.png)
![png](basic_opencv2_files/basic_opencv2_12_41.png)