Human Face Detection with R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Doing human face detection with computer vision is probably something you do once unless you work for police departments, you work in the surveillance industry or for the Chinese government. In order to reduce the time you lose on that small exercise, bnosac created a small R package (source code available at https://github.com/bnosac/image) which wraps the weights of a Single Shot Detector (SSD) Convolutional Neural Network which was trained with the Caffe Deep Learning kit. That network allows to detect human faces in images. An example is shown below (tested on Windows and Linux).
install.packages("magick") install.packages("image.libfacedetection", repos = "https://bnosac.github.io/drat") library(magick) library(image.libfacedetection) image <- image_read("http://bnosac.be/images/bnosac/blog/wikipedia-25930827182-kerry-michel.jpg") faces <- image_detect_faces(image) faces plot(faces, image, border = "red", lwd = 7, col = "white")
What you get out of this is for each face the x/y locations and the width and height of the face. If you want to extract only the faces, loop over the detected faces and get them from the image as shown below.
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
Hope this gains you some time when doing which seems like a t-test of computer vision. Want to learn more on computer vision, next time just follow our course on Computer Vision with R and Python: https://lstat.kuleuven.be/training/coursedescriptions/ComputervisionwithRandPython
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.