From 9b75a3ac28e1c17118833d08bc0a6f9e9cc77833 Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Fri, 11 Jan 2019 12:58:09 -0800 Subject: [PATCH 1/4] support for download parameter --- R/googleVision-LIB.R | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/R/googleVision-LIB.R b/R/googleVision-LIB.R index 5971f6e..ad97636 100644 --- a/R/googleVision-LIB.R +++ b/R/googleVision-LIB.R @@ -16,11 +16,16 @@ #' @param imagePath provide path/url to image #' @return get the image back as encoded file #' -imageToText <- function(imagePath) { +imageToText <- function(imagePath, download) { if (stringr::str_count(imagePath, "http")>0) {### its a url! - content <- RCurl::getBinaryURL(imagePath) - txt <- RCurl::base64Encode(content, "txt") + if (identical(download, FALSE)) { + txt <- imagePath + } + else { + content <- RCurl::getBinaryURL(imagePath) + txt <- RCurl::base64Encode(content, "txt") + } } else { txt <- RCurl::base64Encode(readBin(imagePath, "raw", file.info(imagePath)[1, "size"]), "txt") } @@ -61,6 +66,7 @@ extractResponse <- function(pp, feature){ #' @param imagePath path or url to the image #' @param feature one out of: FACE_DETECTION, LANDMARK_DETECTION, LOGO_DETECTION, LABEL_DETECTION, TEXT_DETECTION #' @param numResults the number of results to return. +#' @param download should image urls be downloaded and sent as data to the API? #' @export #' @return a data frame with results #' @examples @@ -68,15 +74,26 @@ extractResponse <- function(pp, feature){ #' getGoogleVisionResponse(imagePath = f, feature = "LOGO_DETECTION") #' @import googleAuthR #' -getGoogleVisionResponse <- function(imagePath, feature = "LABEL_DETECTION", numResults = 5){ +getGoogleVisionResponse <- function(imagePath, + feature = "LABEL_DETECTION", + numResults = 5, + download = TRUE){ ################################# - txt <- imageToText(imagePath) + txt <- imageToText(imagePath, download = download) + + if (identical(download, FALSE)) { + imageContent <- paste0('"image": { "source": { "imageUri": "',txt,'" } }') + } + else { + imageContent <- paste0('"image": { "content": "',txt,'" }') + } + ### create Request, following the API Docs. - if (is.numeric(numResults)) { - body <- paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "',feature,'", "maxResults": ',numResults,'} ], } ],}') + if (is.numeric(numResults)) { + body <- paste0('{ "requests": [ { ', imageContent, ', "features": [ { "type": "',feature,'", "maxResults": ',numResults,'} ], } ],}') } else { - body <- paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "',feature,'" } ], } ],}') + body <- paste0('{ "requests": [ { ', imageContent, ', "features": [ { "type": "',feature,'" } ], } ],}') } simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header = "POST") From 6f941f47be9751422b175aa1817f6bc3d5e89bfc Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Fri, 11 Jan 2019 12:58:35 -0800 Subject: [PATCH 2/4] support for scoring batches --- R/googleVision-LIB.R | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/R/googleVision-LIB.R b/R/googleVision-LIB.R index ad97636..1efb7c3 100644 --- a/R/googleVision-LIB.R +++ b/R/googleVision-LIB.R @@ -37,7 +37,7 @@ imageToText <- function(imagePath, download) { #' @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){ @@ -69,7 +69,7 @@ extractResponse <- function(pp, feature){ #' @param download should image urls be downloaded and sent as data to the API? #' @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 @@ -80,22 +80,30 @@ getGoogleVisionResponse <- function(imagePath, download = TRUE){ ################################# - txt <- imageToText(imagePath, download = download) + requests <- c() - if (identical(download, FALSE)) { - imageContent <- paste0('"image": { "source": { "imageUri": "',txt,'" } }') - } - else { - imageContent <- paste0('"image": { "content": "',txt,'" }') - } + for (singlePath in imagePath) { + txt <- imageToText(singlePath, download = download) - ### create Request, following the API Docs. - if (is.numeric(numResults)) { - body <- paste0('{ "requests": [ { ', imageContent, ', "features": [ { "type": "',feature,'", "maxResults": ',numResults,'} ], } ],}') - } else { - body <- paste0('{ "requests": [ { ', imageContent, ', "features": [ { "type": "',feature,'" } ], } ],}') + if (identical(download, FALSE)) { + imageContent <- paste0('"image": { "source": { "imageUri": "',txt,'" } }') + } + else { + imageContent <- paste0('"image": { "content": "',txt,'" }') + } + + ### create Request, following the API Docs. + if (is.numeric(numResults)) { + request <- paste0('{ ', imageContent, ', "features": [ { "type": "',feature,'", "maxResults": ',numResults,'} ], }') + } else { + request <- paste0('{ ', imageContent, ', "features": [ { "type": "',feature,'" } ], }') + } + + requests <- c(requests, request) } + body <- paste0('{ "requests": [ ', paste0(request, collapse = ", ") , ' ],}') + simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header = "POST") ## set the request! pp <- simpleCall(the_body = body) From f84d6e13365239aebaf65933730fec5e3a969dbe Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Fri, 11 Jan 2019 13:14:24 -0800 Subject: [PATCH 3/4] fix batch request typo --- R/googleVision-LIB.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/googleVision-LIB.R b/R/googleVision-LIB.R index 1efb7c3..04a6031 100644 --- a/R/googleVision-LIB.R +++ b/R/googleVision-LIB.R @@ -102,7 +102,7 @@ getGoogleVisionResponse <- function(imagePath, requests <- c(requests, request) } - body <- paste0('{ "requests": [ ', paste0(request, collapse = ", ") , ' ],}') + body <- paste0('{ "requests": [ ', paste0(requests, collapse = ", ") , ' ],}') simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header = "POST") ## set the request! From d4caf593b104bc2b498a96a875f54e6959f88703 Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Fri, 11 Jan 2019 13:27:55 -0800 Subject: [PATCH 4/4] support parsing multiple responses --- R/googleVision-LIB.R | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/R/googleVision-LIB.R b/R/googleVision-LIB.R index 04a6031..912d94c 100644 --- a/R/googleVision-LIB.R +++ b/R/googleVision-LIB.R @@ -41,21 +41,16 @@ imageToText <- function(imagePath, download) { #' @return a data frame #' extractResponse <- function(pp, feature){ - if (feature == "LABEL_DETECTION") { - return(pp$content$responses$labelAnnotations[[1]]) - } - if (feature == "FACE_DETECTION") { - return(pp$content$responses$faceAnnotations[[1]]) - } - if (feature == "LOGO_DETECTION") { - return(pp$content$responses$logoAnnotations[[1]]) - } - if (feature == "TEXT_DETECTION") { - return(pp$content$responses$textAnnotations[[1]]) - } - if (feature == "LANDMARK_DETECTION") { - return(pp$content$responses$landmarkAnnotations[[1]]) - } + feature_map <- list( + LABEL_DETECTION = "labelAnnotations", + FACE_DETECTION = "faceAnnotations", + LOGO_DETECTION = "logoAnnotations", + TEXT_DETECTION = "textAnnotations", + LANDMARK_DETECTION = "landmarkAnnotations" + ) + + feature_name <- feature_map[[feature]] + do.call("rbind", pp$content$responses[[feature_name]]) }