Facial Recognition Using R

We make use of CNNs to detect human faces in an image.

It is recommended to use RStudio for the sake of maximum simplicity.

Setup

We assume you have R Studio already installed. If not, then follow this tutorial.

You need to install the following libraries in R:

LibraryFunction
magickIt provides a modern and simple toolkit for image processing in R
image.libfacedetectionIt is an open source library for face detection in images

How To Install Libraries in R?

New libraries can be installed using the Install button in the Packages section rightmost tab. The radio button of the required library needs to be made checked out in order to make it functional.

Refer to the video tutorial here for the code explanation.

Code

install.packages("magick")
install.packages("image.libfacedetection", repos = "https://bnosac.github.io/drat")
library(magick)
library(image.libfacedetection)
image <- image_read("https://www2.pictures.zimbio.com/gi/Lionel+Messi+FC+Barcelona+v+Real+Sociedad+i_S74LhUODhx.jpg")
faces <- image_detect_faces(image)
faces
plot(faces, image, border = "red", lwd = 7, col = "white")
#the above code up gives the coordinates and the dimensions of the faces 
allfaces <- Map(
    x      = faces$detections$x,
    y      = faces$detections$y,
    width  = faces$detections$width,
    height = faces$detections$height,
    f = function(x, y, width, height){
      image_crop(image, geometry_area(x = x, y = y, width = width, height = height))
    })
allfaces <- do.call(c, allfaces)
allfaces
#The above code maps these coordinates and dimensions to give us the faces

Your output shall be waiting for you in the viewer screen when you execute the whole code using Ctrl+Enter with all the libraries installed.

Credits: The R Blog