I hope you reached this article since you are interested in learning what is openCV, Face detection, Face recognition. Yes, you are in the right place. When you wanted to learn and know, how to face detection is done, you would have come across a term called Machine Learning. Face detection is one of the ML things.
Here I gonna share my experience and learning about face detection and the tools we use at Spritle.
Face Detection and Recognition has become one common thing in our day to day life, we are using these things in daily used devices like phone, laptop, CCTV, even Facebook, and many other social media platforms also detect faces.
But you would be wondering how this is done, will it involve a lot of research and learnings to get this working? but actually No, Let me stop boring you and jump directly on how it can be done.
As a developer, I can do face detection in less than 20 lines of code. Before I start, I want clarity about the following things, You have already heard about these things.
Face Detection – It sounds like, Where is my face?
Face Recognition – It sounds like, Who is that?
Facial Expression – It sounds like, How to express the person?
Object Detection – It sounds like, What is that?
What is ML?
It is the science and engineering of making an intelligent machine. Especially in intelligent computer programs. It is related to the similar task of using computers to understand human intelligence.
Example: I can easily find out who is he/she and what is a pen, phone, laptop, etc, the same things the machine can do.
What Is Face Detection?
Face detection is a computer technology being used in a variety of applications that identifies human faces in digital images. Face detection also refers to the psychological process by which humans locate and attend to faces in a visual scene.
Detecting Faces (Viola Jones Algorithm)
In 2002 Paul Viola Michael Jones came up with a book called โRapid object detection using a boosted cascade of simple featuresโ.
There are a few problems with face detection
- We don’t know the face size, it could be big or small
- Different ethnic group
- Low-resolution images
So all of this adds up to quite a difficult problem, and yet it’s not a problem. we don’t have to worry about it anymore, because we can do it because of these guys.
I don’t want to bore you talking about this algorithm, but I recommend you to read about it, here are the links.
Detecting Faces (Viola Jones Algorithm)
Computer Vision – Haar-Features
Computer vision tools and libraries
- OpenCV – Most popular library, multi-platform, and easy to use. It covers all the necessary techniques and algorithms to perform several image and video processing tasks, works well with C++ and Python.
- TensorFlow – Googleโs open-source framework for deep learning, has some great tools to perform image processing/classification โ it is something similar to API graph tensor. Moreover, Python API can be used to perform face and expression detection. Tensorflow also allows performing computer vision of tremendous magnitudes.
- AForge.NET – Extremely simple to use open-source C# framework applied in the computer vision, artificial intelligence industries.
- SimpleCV – a Python framework to deliver a more human-readable programming interface when building. It provides access to a wide range of computer vision tools (i.e OpenCV, Pygame, etc.), can be also used when quick prototyping.
- GPUImage – The framework built on OpenGL ES 2.0 that allows applying GPU-accelerated effects and filters to live motion video, images, and movies.
- SciPy – An open-source Python library used for scientific computing and technical computing.
- BoofCV – Open-source Java library for real-time robotics and computer vision applications released under an Apache 2.0 license for both academic and commercial use.
I have explored more about OpenCV, so let me give you more insights into that, later I will talk about other libraries.
My experience with OpenCV
If you want to learn to detect face quick, I will recommend using OpenCV. Because you can easily set up in your machine and execute your program within milliseconds. Also, you can easily debug your scripts.
Here I have used Python3. Yes, using OpenCV with python. It is quite an interesting thing. Don’t think python is going to be complex. It is very similar to other dynamic programming languages.
This is the demo_live_face_detection.py simple script for detecting the face. You can try yourself.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
# Load the cascade | |
#https://github.com/karthick965938/Face-detection/blob/master/haarcascades/haarcascade_frontalface_default.xml | |
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml') | |
# To capture video from webcam. | |
cap = cv2.VideoCapture(0) | |
# To use a video file as input | |
while True: | |
# Read the frame | |
_, img = cap.read() | |
# Convert to grayscale | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
# Detect the faces | |
faces = face_cascade.detectMultiScale(gray, 1.1, 4) | |
# Draw the rectangle around each face | |
for (x, y, w, h) in faces: | |
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) | |
# Display | |
cv2.imshow('img', img) | |
# Stop if escape key is pressed | |
k = cv2.waitKey(30) & 0xff | |
if k==27: | |
break | |
# Release the VideoCapture object | |
cap.release() |
Execute this program with the following command.
$ python3 demo_live_face_detection.py
In my next blog, I will talk more about Face Recognition, You can find my repo here in Github. I keep this repo updated.
I am building a simple idea where the Laptop/Computer camera will take multiple images of a person on a defined interval and it will be stored in the database and at EOD analyze the image’s facial expression with the help of facial expression script and create a mood-based report of the user.
If you are interested to contribute, please join me. Also, I would like to hear about your experience and learning as comments.
Amazing. Thanks, Karthick. How can we use this in a mobile app?
Hi Egghead,
We can use Google’s mobile vision API. It would help you. Please check here https://codelabs.developers.google.com/codelabs/face-detection/index.html?index=..%2F..index#4
Oh, wow, face detection in 20 minutes. Thanks, Karthick, but the tutorial says it’s not a face recognition API. Do you perhaps have another mobile-based sample resource for face recognition?
Oh, I thought you asking about face detection. Sorry ๐
I just research about Mobile-based source code for Face Detection. Please check the following links. Any help let me know ๐
https://github.com/Lauszus/FaceRecognitionApp
https://medium.com/devnibbles/facial-recognition-with-android-1-4-5e043c264edc
https://www.journaldev.com/15629/android-face-detection