-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathyolo_detect.R
More file actions
60 lines (60 loc) · 2.53 KB
/
Copy pathyolo_detect.R
File metadata and controls
60 lines (60 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#' @title Object detection with YOLO v2 (You only look once)
#' @description Object detection with YOLO v2 (You only look once)
#' @references \url{https://pjreddie.com/publications}
#' @param file character string with the full path to the image file
#' @param object an object of class \code{darknet_model} as returned by \code{\link{image_darknet_model}}
#' @param threshold numeric, detection threshold
#' @param hier_threshold numeric, detection threshold
#' @return invisible(), a file called predictions.png is created in the working directory showing
#' the objects found
#' @export
#' @seealso \code{\link{image_darknet_model}}
#' @examples
#' ##
#' ## Define the model
#' ##
#' yolo_tiny_voc <- image_darknet_model(type = 'detect',
#' model = "tiny-yolo-voc.cfg",
#' weights = system.file(package="image.darknet", "models", "tiny-yolo-voc.weights"),
#' labels = system.file(package="image.darknet", "include", "darknet", "data", "voc.names"))
#' yolo_tiny_voc
#'
#' ##
#' ## Find objects inside the image
#' ##
#' f <- system.file("include", "darknet", "data", "dog.jpg", package="image.darknet")
#' x <- image_darknet_detect(file = f, object = yolo_tiny_voc)
#'
#' \dontrun{
#' ## For other models, see ?image_darknet_model
#'
#' ## trained on COCO
#' weights <- file.path(system.file(package="image.darknet", "models"), "yolo.weights")
#' download.file(url = "http://pjreddie.com/media/files/yolo.weights", destfile = weights)
#' yolo_coco <- image_darknet_model(type = 'detect',
#' model = "yolov3.cfg",
#' weights = system.file(package="image.darknet", "models", "yolov3.weights"),
#' labels = system.file(package="image.darknet", "include", "darknet", "data", "coco.names"))
#' yolo_coco
#'
#' f <- system.file("include", "darknet", "data", "dog.jpg", package="image.darknet")
#' x <- image_darknet_detect(file = f, object = yolo_coco)
#' }
image_darknet_detect <- function(file, object, threshold = 0.3, hier_threshold = 0.5) {
stopifnot(file.exists(file))
stopifnot(object$type == "detect")
if(any(basename(file) == file)){
stop("Give a full path to the file instead of a path inside the working directory")
}
threshold <- as.numeric(threshold)
hier_threshold <- as.numeric(hier_threshold)
result <- .Call("darknet_detect",
object$cfgfile,
object$weightfile,
file,
threshold, hier_threshold,
object$labels,
system.file(package = "image.darknet", "include", "darknet"),
PACKAGE = "image.darknet")
invisible()
}