Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for batches and no downloads #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 47 additions & 27 deletions R/googleVision-LIB.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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]])
}


Expand All @@ -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)
Expand Down