From 9e7a2435af869de8d4d2fb8bd3c6fc2d46a42acc Mon Sep 17 00:00:00 2001 From: "UOLCORP\\lbruscato" Date: Fri, 8 Dec 2017 14:28:24 -0200 Subject: [PATCH] add option to select google vision feature "SAFE_SEARCH_DETECTION" --- R/googleVision-LIB.R | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/R/googleVision-LIB.R b/R/googleVision-LIB.R index 5971f6e..ce3a830 100644 --- a/R/googleVision-LIB.R +++ b/R/googleVision-LIB.R @@ -32,7 +32,7 @@ imageToText <- function(imagePath) { #' @description a utility to extract features from the API response #' #' @param pp an API response object -#' @param feature the name of the feature to return +#' @param feature the name of the feature to return #' @return a data frame #' extractResponse <- function(pp, feature){ @@ -51,6 +51,9 @@ extractResponse <- function(pp, feature){ if (feature == "LANDMARK_DETECTION") { return(pp$content$responses$landmarkAnnotations[[1]]) } + if (feature == "SAFE_SEARCH_DETECTION") { + return(pp$content$responses$safeSearchAnnotation) + } } @@ -63,7 +66,7 @@ extractResponse <- function(pp, feature){ #' @param numResults the number of results to return. #' @export #' @return a data frame with results -#' @examples +#' @examples #' f <- system.file("exampleImages", "brandlogos.png", package = "RoogleVision") #' getGoogleVisionResponse(imagePath = f, feature = "LOGO_DETECTION") #' @import googleAuthR @@ -73,7 +76,7 @@ getGoogleVisionResponse <- function(imagePath, feature = "LABEL_DETECTION", numR ################################# txt <- imageToText(imagePath) ### create Request, following the API Docs. - if (is.numeric(numResults)) { + if (is.numeric(numResults)) { body <- paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "',feature,'", "maxResults": ',numResults,'} ], } ],}') } else { body <- paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "',feature,'" } ], } ],}') @@ -82,7 +85,7 @@ getGoogleVisionResponse <- function(imagePath, feature = "LABEL_DETECTION", numR simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header = "POST") ## set the request! pp <- simpleCall(the_body = body) - + if (ncol(pp$content$responses) >0) { ## obtain results. res <- extractResponse(pp, feature)