Michele di Nuzzo

Data Science and Machine Learning

Real Time Face Detection with OpenCV e R

Facial recognition (face detection) is an artificial intelligence technique used to verify a person’s identity from one or more images depicting him or her. As you might imagine, the fields of application are immense: from security (recognition of the person in front of the sensor) to image cataloging (many tools, such as Google Photos and Facebook, allow people to be recognized in photos). Some airlines use facial recognition to board passengers, and almost all cell phone models use facial recognition to unlock the device.

In our exercise we will see how to identify (in a generic way) a face in an image or video.You will be surprised how it is possible to achieve these results by very few lines of code in R and by taking advantage of the famous OpenCV library.

What is OpenCV

OpenCV is an Open Source Machine Learning and Computer Vision library, containing several thousand algorithms, which perform very well in terms of accuracy and speed. Written in C++, it has interfaces to the major languages used for machine learning, including R, where the opencv package (https://cran.r-project.org/web/packages/opencv/) takes care of just such a link to the library.

Face Detection with R

Among the various algorithms, we will examine those for face recognition (face detection) and real-time processing of images from the webcam. Face recognition is part of computer vision (or computer vision), the purpose of which is to recognize or detect human faces in any digital image and belongs to the broader field of Object Detection, where we try more generally to detect and classify specific objects (animals, cars, humans, etc.).

Let’s start by installing the package:

> install.packages("opencv")
> library(opencv)

Let us now try to perform a fairly easy recognition: that of our face! It is necessary, of course, that you have any webcam on your pc. The ocv_picture() function takes an image from the webcam, which we will save in the variable test1. With the ocv_write() function we save the image in the file test1.png. Stand in front of the webcam and run the code:

> # Face detection from webcam
> test1 <- ocv_picture()
> ocv_write(test1, 'test1.png')

We now use the ocv_face() function, which will take as input the image just taken and try to perform face recognition:

> test1_faces <- ocv_face(test1)
> ocv_write(test1_faces, 'test1_faces.png')

You may observe that in the file test1_faces.png your face will be surrounded by a circle. Recognizing a face in the photo of a face is actually not that complicated. Let’s try to increase the difficulty and download any photo with many faces from the Internet:

Let’s call it test2.jpg and place it in the workbook of R. Let’s run:

> # Face detection da immagine
> test2 <- ocv_read('test2.jpg')
> test2_faces <- ocv_face(test2)
> ocv_write(test2_faces, 'test2_faces.jpg')

“C:\\..\\test2_faces.jpg”

> test2_faces

<pointer: 0x0000021a6f3fc270>
attr(,”class”)
1 “opencv-image”

The result will be:

Real Time Face Detection

You will be able to see that almost all faces have been correctly identified. Identification can also be done live by analyzing the stream from the webcam in real time:

> # Live face detection
> ocv_video(ocv_face)

Or we can apply live filters:

> # Apply filtri
> ocv_video(ocv_edges)
> ocv_video(ocv_knn)
> ocv_video(ocv_facemask)
> ocv_video(ocv_mog2)
> ocv_video(ocv_stylize)
> ocv_video(ocv_sketch)

Image processing

Let’s try applying various filters to an image:

> # Filters
> test3 <- ocv_read('test3.jpg')


> ocv_sketch(test3, color = T)


> ocv_blur(test3, ksize = 15)


> ocv_markers(test3)


> ocv_stylize(test3)

To obtain, on the other hand, the coordinates of the mask:

> # coordinate della maschera
> facemask <- ocv_facemask(test3)
> attr(facemask, 'faces')
radius   x   y
1     82 418 200

Are you interested in Machine Learning?

Everything you need to know (and more) you can find in my book Data Science and Machine Learning, available in both print and digital versions. You can purchase it from here.

 

 

Leave a Reply