Skip to content

Instantly share code, notes, and snippets.

@KarthiAru
Last active December 24, 2016 07:18
Show Gist options
  • Save KarthiAru/935ee8e12521c9791c9d95ae9d860365 to your computer and use it in GitHub Desktop.
Save KarthiAru/935ee8e12521c9791c9d95ae9d860365 to your computer and use it in GitHub Desktop.
library(googleAuthR)
library(RCurl)
library(stringi)
# Google AuthR Credentials
options('googleAuthR.client_id' = '[your client id]')
options('googleAuthR.client_secret' = '[your client secret]')
# Input
img_url <- '[Image URL]'
feature <- '[Selected Feature]'
# Define Scope
options('googleAuthR.scopes.selected' = c('https://www.googleapis.com/auth/cloud-platform'))
googleAuthR::gar_auth()
# Create a Google Vision API function
f <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate",
http_header = "POST")
# Encode the Image URL
img_url_encoded <- base64Encode(getBinaryURL(img_url), "img_url_encoded")
# Create the JSON request
body <- paste0('{ "requests": [ { "image": { "content": "',img_url_encoded,'" },"features": [ { "type": "',feature,'" } ], } ],}')
# Submit the JOB and retrieve the result
result <- f(the_body = body)
# Check status
status <- result$status_code
# Merge the text result
ocr_text <- paste(result$content$responses$textAnnotations[[1]]$description, sep=' ', collapse=' ')
ocr_text <- stri_replace_all(ocr_text, regex = "\n|\r", replacement = " ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment