forked from bnosac/image
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add libfacedetection for detecting faces
- Loading branch information
Showing
24 changed files
with
2,905 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
LICENSE.note |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.