Skip to content

Instantly share code, notes, and snippets.

@flovv
Created March 28, 2016 16:04
Show Gist options
  • Save flovv/524f5bf2f465e5a4da25 to your computer and use it in GitHub Desktop.
Save flovv/524f5bf2f465e5a4da25 to your computer and use it in GitHub Desktop.
Using Google's Vision API for cace detection, logo recognition and OCR.with R
## please see flovv.github.com for a short blog post on running the code.
#devtools::install_github("MarkEdmondson1234/googleAuthR")
require(googleAuthR)
require(RCurl)
### plug in your credentials
## create project- enable billing- -- enable APIs Vision APi -- go to credentials create OAuth 2.0 client ID: copy client_id and client_secret from JSON file.
options("googleAuthR.client_id" = "xxx.apps.googleusercontent.com")
options("googleAuthR.client_secret" = "")
### define scope!
options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/cloud-platform"))
### use fantastic package!
googleAuthR::gar_auth()
################################# FACE DETECTION EXAMPLE
### encode image
imageFile <- "arnold_wife.jpg"
txt <- base64Encode(readBin(imageFile, "raw", file.info(imageFile)[1, "size"]), "txt")
### create Request, following the API Docs.
body= paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "FACE_DETECTION", "maxResults": 2} ], } ],}')
## generate function call
simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header="POST" )
## set the request!
pp <- simpleCall(the_body = body)
## obtain results.
pp$content$responses$faceAnnotations[[1]]
################################################################################
######################################## LOGO DETECTION EXAMPLE
imageFile <- "brandlogos.png"
txt <- base64Encode(readBin(imageFile, "raw", file.info(imageFile)[1, "size"]), "txt")
### create Request, following the API Docs.
body= paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "LOGO_DETECTION", "maxResults": 40} ], } ],}')
## generate function call
simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header="POST" )
## set the request!
pp <- simpleCall(the_body = body)
logos <- pp$content$responses$logoAnnotations[[1]]
##################################################################################
######################################## OCR EXAMPLE
imageFile <- "brandlogos.png"
txt <- base64Encode(readBin(imageFile, "raw", file.info(imageFile)[1, "size"]), "txt")
### create Request, following the API Docs.
body= paste0('{ "requests": [ { "image": { "content": "',txt,'" }, "features": [ { "type": "TEXT_DETECTION", "maxResults": 40} ], } ],}')
## generate function call
simpleCall <- gar_api_generator(baseURI = "https://vision.googleapis.com/v1/images:annotate", http_header="POST" )
## set the request!
pp <- simpleCall(the_body = body)
st <- pp$content$responses$textAnnotations[[1]]$description
####################################################################################
@ANURAGMN
Copy link

at
pp <- simpleCall(the_body = body)

I'm getting ERROR:400

HOW TO RESOLVE?

@dibyapanigrahi
Copy link

Getting error in this line -
pp <- simpleCall(the_body = body)
i 2020-10-04 19:32:07 > Request Status Code: 403
Error: API returned: Request had insufficient authentication scopes.

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