diff --git a/R/googleVision-LIB.R b/R/googleVision-LIB.R index 5971f6e..912d94c 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") } @@ -32,25 +37,20 @@ 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){ - 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]]) } @@ -61,24 +61,44 @@ 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 +#' @examples #' f <- system.file("exampleImages", "brandlogos.png", package = "RoogleVision") #' 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) - ### create Request, following the API Docs. - 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,'" } ], } ],}') + requests <- c() + + for (singlePath in imagePath) { + txt <- imageToText(singlePath, 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)) { + request <- paste0('{ ', imageContent, ', "features": [ { "type": "',feature,'", "maxResults": ',numResults,'} ], }') + } else { + request <- paste0('{ ', imageContent, ', "features": [ { "type": "',feature,'" } ], }') + } + + requests <- c(requests, request) } + body <- paste0('{ "requests": [ ', paste0(requests, collapse = ", ") , ' ],}') + simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header = "POST") ## set the request! pp <- simpleCall(the_body = body)