Skip to content

Commit

Permalink
Add libfacedetection for detecting faces
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijffels committed Mar 21, 2019
1 parent e429fc8 commit 6415e5b
Show file tree
Hide file tree
Showing 24 changed files with 2,905 additions and 5 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ These algorithms are put into different packages because of license differences.
| **image.CannyEdges** | Canny Edge Detector for Images | GPL-3 | [Details](image.CannyEdges) |
| **image.Otsu** | Otsu's Image Segmentation Method | MIT | [Details](image.Otsu) |
| **image.dlib** | Speeded up robust features (SURF) and histogram of oriented gradients (HOG) features | AGPL-3 | [Details](image.dlib) |
| **image.libfacedetection** | CNN for Face Detection | BSD-3 | [Details](image.libfacedetection) |
| **image.darknet** | Image classification using darknet with deep learning models AlexNet, Darknet, VGG-16, Extraction (GoogleNet) and Darknet19. As well object detection using the state-of-the art YOLO detection system | MIT | [Details](image.darknet) |
| **image.OpenPano** | Image Stitching | see file LICENSE | [Details](image.OpenPano) |
| **image.DenoiseNLMeans** | Non-local means denoising | see file LICENSE | [Details](image.DenoiseNLMeans) |
Expand All @@ -25,7 +26,16 @@ A presentation given at the useR-2017 conference is available in file [presentat

## Installation

Install all packages
- You can see if the binary packages for your operating system are on the BNOSAC drat repo at https://github.com/bnosac/drat
- If they are, you can just install them as follows, where you replace `thepackagename` with one of the packages you are interested in

```
install.packages("thepackagename", repos = "https://bnosac.github.io/drat")
```

### Development packages

Install the development version of packages as follows:

```
install.packages("remotes")
Expand All @@ -39,6 +49,7 @@ remotes::install_github("bnosac/image", subdir = "image.Otsu", build_vignettes =
remotes::install_github("bnosac/image", subdir = "image.dlib", build_vignettes = TRUE)
remotes::install_github("bnosac/image", subdir = "image.darknet", build_vignettes = TRUE)
remotes::install_github("bnosac/image", subdir = "image.DenoiseNLMeans", build_vignettes = TRUE)
remotes::install_github("bnosac/image", subdir = "image.libfacedetection", build_vignettes = TRUE)
remotes::install_github("bnosac/image", subdir = "image.OpenPano", build_vignettes = TRUE)
```

Expand All @@ -50,10 +61,9 @@ vignette("image_contour_detector", package = "image.ContourDetector")
vignette("image_canny_edges", package = "image.CannyEdges")
```

###
### CI builds

- Alternatively, you can see if the binary packages for your operating system are on the BNOSAC drat repo at https://github.com/bnosac/drat
- CI builds:
- CI builds are here
- [Travis](https://travis-ci.org/bnosac/image)
- [AppVeyor](https://ci.appveyor.com/project/jwijffels/image)

Expand Down
3 changes: 3 additions & 0 deletions image.libfacedetection/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^.*\.Rproj$
^\.Rproj\.user$
LICENSE.note
17 changes: 17 additions & 0 deletions image.libfacedetection/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Package: image.libfacedetection
Type: Package
Title: Convolutional Neural Network for Face Detection
Description: An open source library for face detection in images. Provides a pretrained convolutional neural network based on <https://github.com/ShiqiYu/libfacedetection> which can be used to detect faces which have size greater than 48x48 pixels.
Maintainer: Jan Wijffels <[email protected]>
Authors@R: c(
person("Jan", "Wijffels", role = c("aut", "cre", "cph"), email = "[email protected]", comment = "R wrapper"),
person("BNOSAC", role = "cph", comment = "R wrapper"),
person("Shiqi Yu", role = c("ctb", "cph"), comment = "libfacedetection C++ code"))
License: BSD_3_clause + file LICENSE
Version: 0.1
URL: https://github.com/bnosac/image
Imports: Rcpp (>= 0.12.8), graphics
LinkingTo: Rcpp
Suggests:
magick
RoxygenNote: 6.1.1
6 changes: 6 additions & 0 deletions image.libfacedetection/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
YEAR: 2015-2019
COPYRIGHT HOLDER: Shiqi Yu

YEAR: 2019
COPYRIGHT HOLDER: Jan Wijffels
ORGANIZATION: BNOSAC
35 changes: 35 additions & 0 deletions image.libfacedetection/LICENSE.note
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.


License Agreement For libfacedetection
(3-clause BSD License)

Copyright (c) 2015-2019, Shiqi Yu, all rights reserved.
[email protected]

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall copyright holders or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
8 changes: 8 additions & 0 deletions image.libfacedetection/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,libfacedetection)
export(image_detect_faces)
importFrom(Rcpp,evalCpp)
importFrom(graphics,rect)
importFrom(graphics,text)
useDynLib(image.libfacedetection)
7 changes: 7 additions & 0 deletions image.libfacedetection/R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

detect_faces <- function(x, width, height, step) {
.Call('_image_libfacedetection_detect_faces', PACKAGE = 'image.libfacedetection', x, width, height, step)
}

111 changes: 111 additions & 0 deletions image.libfacedetection/R/pkg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#' @importFrom Rcpp evalCpp
#' @importFrom graphics text rect
#' @useDynLib image.libfacedetection
NULL




#' @title Detect faces in images using the libfacedetection CNN
#' @description Detect faces in images using using a convolutional neural network available from \url{https://github.com/ShiqiYu/libfacedetection}.
#' The function can be used to detect faces of minimal size 48x48 pixels.
#' @param x an object of class magick-image with rgb colors. Or an rgb integer array with pixel values in the 0-255 range.
#' @return A list with elements nr and detections.\cr
#' Where nr indicates the number of faces found and the data frame detections
#' indicates the locations of these. This data.frame has columns x, y, width and height
#' as well as columns neighbours and angle. The values of x and y are the top left of the start of the box.
#' @export
#' @examples
#' library(magick)
#' path <- system.file(package="image.libfacedetection", "images", "handshake.jpg")
#' x <- image_read(path)
#' x
#' faces <- image_detect_faces(x)
#' faces
#' plot(faces, x, border = "red", lwd = 7, col = "white")
#'
#'
#' ##
#' ## You can also directly pass on the RGB array in BGR format
#' ## without the need of having magick
#' ##
#' tensor <- image_data(x, channels = "rgb")
#' tensor <- as.integer(tensor)
#' faces <- image_detect_faces(tensor)
#' str(faces)
#' plot(faces, x)
image_detect_faces <- function(x) {
if(inherits(x, "magick-image")){
if(!requireNamespace("magick", quietly = TRUE)){
stop("image_detect_faces requires the magick package, which you can install from cran with install.packages('magick')")
}
meta <- magick::image_info(x)
if(nrow(meta) != 1){
stop("image_detect_faces requires a magick-image containing 1 image only")
}
w <- meta$width
h <- meta$height

x <- magick::image_data(x, channels = "rgb")
x <- as.integer(x)
x <- aperm(x, c(3, 2, 1))
faces <- detect_faces(x, width = w, height = h, step = 1*w*3)
}else if(inherits(x, "array")){
w <- ncol(x)
h <- nrow(x)
stopifnot(length(dim(x)) == 3 && dim(x)[3] == 3)
x <- aperm(x, c(3, 2, 1))
faces <- detect_faces(x, width = w, height = h, step = 1*w*3)
}else{
stop("x is not an array nor a magick-image")
}
class(faces) <- "libfacedetection"
faces
}

#' @title Plot detected faces
#' @description Plot functionality for bounding boxes detected with \code{\link{image_detect_faces}}
#' @param x object of class \code{libfacedetection} as returned by \code{\link{image_detect_faces}}
#' @param image object of class \code{magick-image} which was used to construct \code{x}
#' @param border color of the border of the box. Defaults to red. Passed on to \code{\link[graphics]{rect}}
#' @param lwd line width of the border of the box. Defaults to 5. Passed on to \code{\link[graphics]{rect}}
#' @param only_box logical indicating to draw only the box and not the text on top of it. Defaults to FALSE.
#' @param col color of the text on the box. Defaults to red. Passed on to \code{\link[graphics]{text}}
#' @param cex character expension factor of the text on the box. Defaults to 2. Passed on to \code{\link[graphics]{text}}
#' @param ... other parameters passed on to \code{\link[graphics]{rect}}
#' @export
#' @examples
#' library(magick)
#' path <- system.file(package="image.libfacedetection", "images", "handshake.jpg")
#' x <- image_read(path)
#' x
#' faces <- image_detect_faces(x)
#' faces
#' plot(faces, x, border = "red", lwd = 7, col = "white")
#'
#' ## show one detected face
#' face <- head(faces$detections, 1)
#' image_crop(x, geometry_area(x = face$x, y = face$y,
#' width = face$width, height = face$height))
#' ## show all detected faces
#' boxcontent <- lapply(seq_len(faces$nr), FUN=function(i){
#' face <- faces$detections[i, ]
#' image_crop(x, geometry_area(x = face$x, y = face$y,
#' width = face$width, height = face$height))
#' })
#' boxcontent <- do.call(c, boxcontent)
#' boxcontent
plot.libfacedetection <- function(x, image, border = "red", lwd = 5, only_box = FALSE, col = "red", cex = 2, ...){
stopifnot(inherits(image, "magick-image") && nrow(magick::image_info(image)) == 1)
faces <- x$detections
magick::image_draw(image)
lapply(seq_along(faces$x), FUN = function(i){
face <- lapply(faces, FUN=function(x) x[i])
graphics::rect(xleft = face$x, xright = face$x + face$width,
ytop = face$y, ybottom = face$y + face$height, border = border, lwd = lwd, ...)
if(!only_box){
graphics::text(x = face$x, y = face$y, adj = 0.5, labels = face$neighbours, col = col, cex = cex)
}
})
invisible()
}
41 changes: 41 additions & 0 deletions image.libfacedetection/dev/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
library(image.libfacedetection)
library(magick)
x <- system.file(package = "image.libfacedetection", "images", "keliamoniz1.jpg")
x <- system.file(package = "image.libfacedetection", "images", "keliamoniz2.jpg")
x <- system.file(package = "image.libfacedetection", "images", "handshake.jpg")
x <- "C:/Users/Jan/Desktop/faces.jpg"
x <- "C:/Users/Jan/Desktop/Vanessa.png"
x <- "C:/Users/Jan/Desktop/dt.jpeg"
x <- "C:/Users/Jan/Desktop/bartholomeus_van_der_helst.jpg"
x <- "C:/Users/Jan/Desktop/130829-F-FR885-018.JPG"
x <- "C:/Users/Jan/Desktop/180719-F-JA727-0002.JPG"
x <- "C:/Users/Jan/Desktop/people.jpg"
x <- "C:/Users/Jan/Desktop/handjeschudden.jpg"
img <- image_read(x)
#img <- image_resize(img, "800x")
w <- image_info(img)$width
h <- image_info(img)$height
x <- image_data(img)
x <- as.integer(x)
#x <- aperm(x, c(2, 1, 3))
#x <- aperm(x, c(2, 3, 1))
x <- aperm(x, c(3, 2, 1))
x <- as.vector(x)
print(str(x))
#faces <- image.libfacedetection:::detect_faces(x, 640, 480, 1*640*3)
#faces <- image.libfacedetection:::detect_faces(x, w, h, 1*w*3)
faces <- image.libfacedetection:::detect_faces(x, w, h, 1*w*3)
idx <-
print(str(faces))


image_draw(img)
face <- lapply(seq_along(faces$x), FUN = function(i){
face <- lapply(faces, FUN=function(x) x[i])
#rect(xleft = face$x, xright = face$x + face$width,
# ytop = h-face$y, ybottom = h-face$y-face$height, border = "red")
rect(xleft = face$x, xright = face$x + face$width,
ytop = face$y, ybottom = face$y+face$height, border = "red", lwd = 5)
text(x = face$x, y = face$y, adj = 0.5, labels = face$neighbours, col = "white", cex = 2)

})
Binary file added image.libfacedetection/inst/images/Thumbs.db
Binary file not shown.
Binary file added image.libfacedetection/inst/images/handshake.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions image.libfacedetection/man/image_detect_faces.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions image.libfacedetection/man/plot.libfacedetection.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions image.libfacedetection/src/Makevars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CXX_STD = CXX11
PKG_LIBS = $(SHLIB_OPENMP_CFLAGS)
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
PKG_CPPFLAGS = -DSTRICT_R_HEADERS
Loading

0 comments on commit 6415e5b

Please sign in to comment.