Detecting Faces in Images, a bit of Computer Vision

This is a really short article, on an unfinished project, if you like face recognition, and want to continue it, DM me wherever.

Intro

Do you like surveillance ? Automation of espionnage ? Yeah, me neither, but tech, is tech, and if you don’t do it, others will, and they may care less than you about safety.
The goal of this mini-project is circling faces on an image, and a future part may be applying face recognition to find out who is on the image.

The Tech

I’m going to use a Jupyter Python notebook, importing the standard scientific python libraries, and cv2 for Computer Vision.
I’ll include code snippets, and, since it’s a very simple task, you may be able to replicate it yourselves.

Classifiers

For this, I’m not going to train my own model (for now), and simply use a Cascade Classifier.

1
2
front_cascade = cv2.CascadeClassifier("./cascades/haarcascade_frontalface_alt.xml")
profile_cascade = cv2.CascadeClassifier("./cascades/haarcascade_profileface.xml")

Images

We can import and use images directly with cv2

1
2
3
face_bgr = cv2.imread("./known_faces\\musk.jpg")
face_rgb = cv2.cvtColor(face_bgr, cv2.COLOR_BGR2RGB)
plt.imshow(face_rgb)

image elon musk's face

Our cascades only like grayscale images, so let’s convert our image

1
2
face_gray = cv2.cvtColor(face_bgr, cv2.COLOR_BGR2GRAY)
plt.imshow(face_gray, cmap='gray')

image elon musk's face

Detection

Now let’s write a script that detects every face, no matter its orientation:

Here we initialize everything we’ll need

1
2
3
4
detected_faces_coords_tmp = [0]
detected_faces_coords = []
detected_faces_rgb = []
face_gray_copy = np.copy(face_gray)

And then we create a loop, because sometimes the cascades miss a face, because another one is too obvious. So, we create a blackbox on top of the found faces, to try and see if we didn’t miss one in the background.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

while len(detected_faces_coords_tmp) > 0:

front_detected_faces_coords = front_cascade.detectMultiScale(face_gray_copy, scaleFactor=1.5, minNeighbors=5)
profile_detected_faces_coords = profile_cascade.detectMultiScale(face_gray_copy, scaleFactor=1.5, minNeighbors=5)

detected_faces_coords_tmp = front_detected_faces_coords if len(front_detected_faces_coords) > \
len(profile_detected_faces_coords) else profile_detected_faces_coords

detected_faces_coords.extend(detected_faces_coords_tmp)

detected_faces_rgb.extend([face_rgb[y:y+h, x:x+w]
for (x, y, w, h) in detected_faces_coords_tmp])

for x, y, w, h in detected_faces_coords:
face_gray_copy = blackbox_image(face_gray_copy, [y, x, y+h, x+w])

Results

For the people that are impressed by circles, I made this.

image elon musk's face

And here’s the usable data, we could feed into a Neural Network that would detect who is on the picture, or their gender, or edit their features, or whatever pleases your creative ideas !
image elon musk's face

Okay, that Elon Musk image is generous, of course, it is already almost centered. How about a low-quality, generic group of people ?

ppl faces

See, even low-quality faces are picked up, and transformed into usable images !

low res guy's face