bnosac / image

Computer Vision and Image Recognition algorithms for R users
274 stars 65 forks source link

image.libfacedetection fails with some images #13

Closed jmcastagnetto closed 5 years ago

jmcastagnetto commented 5 years ago

For example, in this picture of people in Lima, Peru, it doesn't detect a face: https://www.maxpixel.net/static/photo/1x/South-America-Lima-Peru-Road-Colorful-Color-1244329.jpg

Another example, this time from Wikimedia, it detects 3 faces: https://upload.wikimedia.org/wikipedia/commons/e/ec/Urarina_shaman_B_Dean.jpg

Using a smaller resolution from the second image, it doesn't detect any face (source: https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Urarina_shaman_B_Dean.jpg/406px-Urarina_shaman_B_Dean.jpg)

I've used exactly the same code as the article in http://www.bnosac.be/index.php/blog/89-human-face-detection-with-r

library(magick)
library(image.libfacedetection)

# using an image where it fails to detect a face
img_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Urarina_shaman_B_Dean.jpg/406px-Urarina_shaman_B_Dean.jpg"

image <- image_read(img_url)
faces <- image_detect_faces(image)
faces
plot(faces, image, border = "red", lwd = 7, col = "white")

I am showing below the results from printing the faces object:

> faces
$nr
[1] 0

$detections
[1] x          y          width      height     neighbours
[6] angle     
<0 rows> (or 0-length row.names)

attr(,"class")
[1] "libfacedetection"

Is it a problem with the image resolution perhaps? If so, then it should be noted in the documentation, and perhaps change the comment in https://github.com/bnosac/image/blob/master/image.libfacedetection/man/image_detect_faces.Rd which in line 20 indicates:

The function can be used to detect faces of minimal size 48x48 pixels.

Reproducibility information

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          5.2                         
year           2018                        
month          12                          
day            20                          
svn rev        75870                       
language       R                           
version.string R version 3.5.2 (2018-12-20)
nickname       Eggshell Igloo
> packageVersion("magick")
[1] ‘2.0’
> packageVersion("image.libfacedetection")
[1] ‘0.1’
jwijffels commented 5 years ago

Doc says: it can detect faces of dimension 48x48. Do the faces that you indicate have at least that size? Output also show a confidence called neighbours. You can use that according to your needs. FYI. Model was trained on data from http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace, caffe convolutional neural architecture is here: https://github.com/ShiqiYu/libfacedetection/tree/master/models/caffe

jmcastagnetto commented 5 years ago

I saw that, and If you look at the URL in the code, the face is bigger than that. Just so it is clear, I am including a manually cut rectangle of 94 x 79 pixels, containing the face.

face-rectangle-406px-Urarina_shaman_B_Dean

Tested with a photo with people with different ethnicities from Peru and it also failed. Might it be a training bias?

Will check the source materials for the model next weekend and report back if I get a hint of what's happening.

jwijffels commented 5 years ago

Knipsel this face is 45x62 - why don't you take larger faces.

jmcastagnetto commented 5 years ago

OK, you were correct. Sorry for that. High-res versions do work.