Skip to content

Instantly share code, notes, and snippets.

@afeld
Last active December 11, 2016 21:59
Show Gist options
  • Save afeld/655e6e02bf085feb02193f468516fca5 to your computer and use it in GitHub Desktop.
Save afeld/655e6e02bf085feb02193f468516fca5 to your computer and use it in GitHub Desktop.
face detection using the Google Cloud Vision API in Ruby
# Create a JSON service account key here:
#
# https://console.cloud.google.com/apis/credentials/serviceaccountkey
#
# then run with
#
# GOOGLE_APPLICATION_CREDENTIALS=<file>.json IMG=... ruby google.rb
#
# http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/VisionV1/VisionService
#
require 'googleauth'
require 'google/apis/vision_v1'
require 'pp'
Vision = Google::Apis::VisionV1 # Alias the module
service = Vision::VisionService.new
scopes = ['https://www.googleapis.com/auth/cloud-platform']
authorization = Google::Auth.get_application_default(scopes)
service.authorization = authorization
content = File.read(ENV['IMG'])
image = Vision::Image.new(content: content)
feature = Vision::Feature.new(type: 'FACE_DETECTION')
req = Vision::BatchAnnotateImagesRequest.new(requests: [
{
image: image,
features: [feature]
}
])
# https://cloud.google.com/prediction/docs/reference/v1.6/performance#partial
res = service.annotate_image(req, fields: 'responses(faceAnnotations(landmarks))')
pp res.to_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment