Last active
January 24, 2018 13:24
-
-
Save tmasjc/58dc3149d0b107c9ad165b2dc1260cc2 to your computer and use it in GitHub Desktop.
A Simple Post Request to Google Cloud Vision API in R #rstats #google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-- BASED ON https://cloud.google.com/vision/docs/request#json_request_format --# | |
# image url on Google Cloud Storage | |
imgURI <- "gs://vacation_room/dublin-001.jpg" | |
# Requests are POST requests to | |
endpoint <- "https://vision.googleapis.com/v1/images:annotate" | |
#### remember to set api token in ``options`` | |
# options(gcv_api_token = "YOUR_API_TOKEN") | |
## authenticate using API token method | |
if(!is.null(getOption("gcv_api_token"))){ | |
baseURI <- paste0(endpoint, "?key=", getOption("gcv_api_token")) | |
} | |
# request body | |
body <- sprintf('{ | |
"requests":[ | |
{ | |
"image":{ | |
"source":{ | |
"imageUri": | |
"%s" | |
} | |
}, | |
"features":[ | |
{ | |
"type":"LABEL_DETECTION", | |
"maxResults":%i | |
} | |
] | |
} | |
] | |
}', imgURI, 5) | |
# send post request | |
res <- httr::POST(baseURI, body = body, encode = "json") | |
## extract content (specific mime type) | |
lbs <- httr::content(res, type = "application/json") | |
# convert into data frame | |
lbs$responses[[1]]$labelAnnotations %>% bind_rows() | |
## The End |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ashleylijie Fixed typo