In [43]:
import cv2, matplotlib.pyplot as plt, numpy as np
%matplotlib inline
In [4]:
cascPath="haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
In [238]:
def randi():
    total=0
    for i1 in range(2):
        for j1 in range(4):
            for i2 in range(4):
                for j2 in range(4):
                    for k2 in range(4):
                        for i4 in range(4):
                            # Read the image
                            imagePath="gp2/"+repr(i1)+repr(j1)+repr(i2)+repr(j2)+repr(k2)+repr(i4)+".jpg"
                            image = cv2.imread(imagePath)
                            try:
                                gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                                # Detect faces in the image
                                faces = faceCascade.detectMultiScale(
                                    gray,
                                    scaleFactor=1.1,
                                    minNeighbors=4,
                                    minSize=(20, 20)
                                )
                                total+=len(faces)
                            except:pass
        print i1,j1,"total",total
randi()
In [238]:
def randi():
    total=0
    for i1 in range(2):
        for j1 in range(4):
            for i2 in range(4):
                for j2 in range(4):
                    for k2 in range(4):
                        for i3 in range(4):
                            for j3 in range(4):
                                for k3 in range(4):
                                    for i4 in range(4):
                                        # Read the image
                                        imagePath="gp/"+repr(i1)+repr(j1)+repr(i2)+repr(j2)+repr(k2)+\
                                        repr(i3)+repr(j3)+repr(k3)+repr(i4)+".jpg"
                                        image = cv2.imread(imagePath)
                                        try:
                                            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                                            # Detect faces in the image
                                            faces = faceCascade.detectMultiScale(
                                                gray,
                                                scaleFactor=1.1,
                                                minNeighbors=4,
                                                minSize=(20, 20)
                                            )
                                            total+=len(faces)
                                        except:pass
                print i1,j1,i2,j2,"total",total
randi()
In [237]:
imagePath="1.jpg"
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=6,
    minSize=(30, 30)
)
total+=len(faces)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
plt.imshow(image)
Out[237]:
<matplotlib.image.AxesImage at 0x2bd6ba20>