Welcome to the ReadMe file for the Face Detection using Python project.
Index:
- Introduction
- FAQs
- Explanation of Code
- Sample Outputs
- Credits
1. Introduction
This project on Face Detection has been done with the implementation of Haar Cascade in Python scripts.
There are three scripts in this project.
'face_detect_static.py' is the script for Face Detection on Image files as supplied by the User.
'face_detect_video.py' is the script for Face Detection on Video files as supplied by the User.
'face_detect_webcam.py' is the script for Face Detection from the Webcam of the host machine the script is executed on.
---End of Section One---
2. FAQs
- What is Haar-like features ? What was used in this project ?
- How is the performance of the script ? Why did we use Gray-Scale ?
- What are the requirements for running the scripts ?
- At first, we import the libraries needed for our Script.
- CV2 is the acronym for OpenCV2 (Open Computer Vision release 2). It is used to detect objects (faces) in the images.
- "sys" module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available.
- "imutils" short for Image Utilities is a series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, displaying Matplotlib images, sorting contours, detecting edges, and much more easier with OpenCV and both Python 2.7 and Python 3.
- We load the Haar Cascade's default frontal face xml file into our program to detect frontal faces from Images, Videos or WebCams.
- We create the Haar Cascade with the previously defined path with the 'CascadeClassifier()' function.
- We read the image, video or webcam input in which we have to detect the faces. We can also take these as input since we have used 'sys' import into our script.
- We convert the image input into gray-scale for ease of the interpreter to work faster and then detect the faces. The "faceCascade.detectMultiScale"'s parameters 'gray' specifies the gray-scale image where the objects are detected, scaleFactor specifies how much the image size is reduced at each image scale, 'minNeighbours' specifies how many neighbors each candidate rectangle should have to retain it, 'minSize' specifies the minimum possible object size and objects smaller than that are ignored.
- We use the 'while' loop to ensure that we get to visualize the output unless we opt to quit by pressing any defined keys (q). We resize the frame and also convert to gray-scale to imporve the overall performance of the script. And finally we display the output to the screen by using the 'imshow()' function.
- We draw rectangles around the faces that are detected using the 'rectangle()' function.
- NOTE:In this project, the Haar Cascade used was the default one without any modifications. There are instances in the output where a non-face was detected as a face.
- The outputs obtained after executing the scripts have been given with pointed titles.
- Image Credits: Mr. Ujan Banerjee
- WebCam Credits: Self
- Video Credits and all Copyrights goes to the Youtube Channel of JayZTwoCents. Here is the link.
Haar-like features are digital image features used in object recognition. They owe their name to their intuitive similarity with Haar wavelets and were used in the first real-time face detector. Historically, working with only image intensities (i.e., the RGB pixel values at each and every pixel of image) made the task of feature calculation computationally expensive. A publication by Papageorgiou et al. discussed working with an alternate feature set based on Haar wavelets instead of the usual image intensities. Viola and Jones adapted the idea of using Haar wavelets and developed the so-called Haar-like features. A Haar-like feature considers adjacent rectangular regions at a specific location in a detection window, sums up the pixel intensities in each region and calculates the difference between these sums. This difference is then used to categorize subsections of an image.
For example, let us say we have an image database with human faces. It is a common observation that among all faces the region of the eyes is darker than the region of the cheeks. Therefore a common Haar feature for face detection is a set of two adjacent rectangles that lie above the eye and the cheek region. The position of these rectangles is defined relative to a detection window that acts like a bounding box to the target object (the face in this case).
~Wikipedia
A Haar Cascade is basically a classifier which is used to detect the object for which it has been trained for, from the source. The Haar Cascade is by superimposing the positive image over a set of negative images. The training is generally done on a server and on various stages. Better results are obtained by using high quality images and increasing the amount of stages for which the classifier is trained. One can also used predefined Haar Cascades which are available on GitHub.
Haar Cascade for frontal face (default) detection was used in this project. It was downloaded from Github. from this link. The OpenCV documentation for Face Detection using Haar Cascades is here.
The performance of the script is strictly based upon the Hardware of the system on which it is run and the quality of files provided. For the webcam, the performance dependency on hardware is not much of a factor but for videos it is, as FFmpeg is decoding the video frame by frame as the script is run.
We converted the input into Gray-Scale to simplify the job of the Haar Cascade algorithm. It would be able to work faster to determine edges and give out positive or negative results on images or frames.
The following Python packages are required to run the scripts.
a) imutils : A series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, displaying Matplotlib images, sorting contours, detecting edges, and much more easier with OpenCV and both Python 2.7 and Python 3.
b) opencv2 : OpenCV (Open Source Computer Vision Library: http://opencv.org) is an open-source BSD-licensed library that includes several hundreds of computer vision algorithms.
c) ffmepg : FFmpeg is a free software project, the product of which is a vast software suite of libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the FFmpeg program itself, designed for command-line-based processing of video and audio files, widely used for format transcoding, basic editing (trimming and concatenation), video scaling, video post-production effects, and standards compliance (SMPTE, ITU).
[Source: Internet]
---End of Section Two---
3. Explanation of Code
import cv2
import sys
import imutils
cascPath = 'haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread('img.jpg')
video_capture = cv2.VideoCapture(0)
video_capture = cv2.VideoCapture('vid.mp4')
For Images:
# Convert to Gray-Scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect Faces in the Image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(25, 25)
)
For WebCam feed and Videos:
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
# Resize the Frame to improve speed
frame = imutils.resize(frame, width=450)
# Convert to Gray-Scale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(25, 25)
)
# Display the resulting Frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the Capture
video_capture.release()
cv2.destroyAllWindows()
# Draw a rectangle around the Faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
---End of Section Three---
4. Sample Outputs
---End of Section Four---
5. Credits
---End of Section Five---