paint-brush
A Python Library for Face Detection and Extraction with OpenCV Using HOG/Neural Networkby@cleuton-sampaio
2,307 reads
2,307 reads

A Python Library for Face Detection and Extraction with OpenCV Using HOG/Neural Network

by Cleuton SampaioMarch 19th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A Python Library for Face Detection and Extraction with OpenCV Using HOG/Neural Network using HOG or Neural Network. The program detects the faces in the passed image, extracting each one in a new image. It transforms in monochrome, aligns the face (aligns the eyes horizontally) and cuts the person's face, in a square shape to the size you want. The parameters are:. img_h, img_w: Size of the "square" of each Face Chip to be generated; face_cascade_file: Model haar cascade for face detection - included in. data / haarcascades; eye.
featured image - A Python Library for Face Detection and Extraction with OpenCV Using HOG/Neural Network
Cleuton Sampaio HackerNoon profile picture

Many people, including me, use a combination of libraries to work on the images, such as: OpenCV itself, DlibPillow etc. But this is a very confusing and problematic process. Dlib installation, for example, can be extremely complex and frustrating.

This is a python library that uses OpenCV to detect, align and extract faces images for classification purposes, either using HOG or Neural Network.

OpenCV is more than capable of doing everything that is needed in an image processing pipeline, such as: detect facesalign faces and extract faces, also known as Face Chips.

Installation

Clone the repository! If you have Anaconda just create an environment with the attached file:

conda env create -f newHogEnv.yml

To test just run the program pythonfaces.py:

python pythonfaces.py |image.jpg|

It has two other arguments:

python pythonfaces.py |image.jpg| |face cascade model| |eye cascade model|

If you enter only the image file name, it will assume the files that are in the "./data/haarcascades" folder.

The program detects the faces in the passed image, extracting each one in a new image. It transforms in monochrome, aligns the face (aligns the eyes horizontally) and cuts the person's face, in a square shape to the size you want.

Use as a library

There are several useful functions in this module and you can import it like this:

import pythonfaces

detectFaces

This function receives an OpenCV image (it can be a video frame), detects the faces in it and returns a list with images. The first is the original marked image (where the faces and eyes are) and the others are the cropped images of each face found (Face Chip), properly aligned and scaled.

You can save the images with:

cv2.imwrite(|path of the file to be written|, |OpenCV image object|)

The parameters are:

  1. img: OpenCV image;
  2. img_h, img_w: Size of the "square" of each Face Chip to be generated;
  3. face_cascade_file: Model haar cascade for face detection - included in "/ data / haarcascades";
  4. eye_cascade_file: Haar cascade model for eye detection - included in "/ data / haarcascades";

These models came from the original OpenCV project on Github.

See the code for pythonfaces.py for other functions.