Skip to content

Instantly share code, notes, and snippets.

@tmasjc
Last active January 24, 2018 13:24
Show Gist options
  • Save tmasjc/58dc3149d0b107c9ad165b2dc1260cc2 to your computer and use it in GitHub Desktop.
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
#-- 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
@tmasjc
Copy link
Author

tmasjc commented Dec 21, 2017

@ashleylijie Fixed typo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment