Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Golang + Cloud Vision API + GAE - GoCon Spring

Golang + Cloud Vision API + GAE - GoCon Spring

Shintaro Kaneko

April 23, 2016
Tweet

More Decks by Shintaro Kaneko

Other Decks in Programming

Transcript

  1. @kaneshin (Shintaro Kaneko) - Principal Engineering Manager at eureka, Inc.

    - Photographer when Not Coding - MacBook Air 11 Inch ʻ Camera+Lens
  2. • Online Dating Service • Responsible for dates, relationships and

    marriages. • Development • Language: PHP -> Golang • AWS: EC2/Aurora /DynamoDB/ElastiCache/… • GCP: BigQuery • Microservices
  3. Golang for • Web Application Framework • Revel -> Gin

    • O/R Mapper/DB • Xorm/Wizard (DB Shard Library) • Vendor Package Management • Glide • etc…
  4. Google Cloud Platform • Compute • Compute Engine • Container

    Engine • App Engine • Storage • Cloud Storage (Nearline) • Cloud Datastore • Cloud Bigtable • Networking • Cloud CDN • Cloud DNS • Big Data • BigQuery • Machine Learning • Cloud Machine Learning • Cloud Vision API • Operations, Tools, …
  5. Google Cloud Platform • Compute • Compute Engine • Container

    Engine • App Engine • Storage • Cloud Storage (Nearline) • Cloud Datastore • Cloud Bigtable • Networking • Cloud CDN • Cloud DNS • Big Data • BigQuery • Machine Learning • Cloud Machine Learning • Cloud Vision API • Operations, Tools, …
  6. Google Cloud Platform • Compute • Compute Engine • Container

    Engine • App Engine • Storage • Cloud Storage (Nearline) • Cloud Datastore • Cloud Bigtable • Networking • Cloud CDN • Cloud DNS • Big Data • BigQuery • Machine Learning • Cloud Machine Learning • Cloud Vision API • Operations, Tools, …
  7. • Understands the content of submitted images. • Encapsulating powerful

    machine learning models. • Classifies images into thousands of categories. • detects individual objects • faces within images • finds and reads printed words contained within images. • Official Documentation • https://cloud.google.com/vision/docs/ Cloud Vision API
  8. pigeon • A package for the Google Cloud Vision API

    on Golang. • It provides command and web app. • Repository • https://github.com/kaneshin/pigeon
  9. Getting Started • Enabling the Google Cloud Vision API •

    API Manager > Overview > Cloud Vision API • Setting Up a Service Account • API Manager > Credentials > Create Credentials • Get service account credentials json file $ export GOOGLE_APPLICATION_CREDENTIALS =~/.credentials/service_account.json
  10. Installation • The pigeon provides the command-line tools. • a

    • Make sure that pigeon was installed correctly: $ go get github.com/kaneshin/pigeon/tools/cmd/...
  11. Usage - command # Default Detection is LabelDetection. $ pigeon

    assets/lenna.jpg $ pigeon -face gs://bucket_name/lenna.jpg $ pigeon -label https://httpbin.org/image/jpeg
  12. Usage - command # Default Detection is LabelDetection. $ pigeon

    assets/lenna.jpg $ pigeon -face gs://bucket_name/lenna.jpg $ pigeon -label https://httpbin.org/image/jpeg This flag is specified detection of Cloud Vision API Given image file path (OK: GCS, Over http/s)
  13. Usage - web app # Default port is 8080. #

    Default Detection is LabelDetection. $ pigeon-app $ pigeon-app -port=8000 -- -face -label -safe-search
  14. Deploy - pigeon # Requisite appengine go sdk and direnv

    $ cd /path/to/pigeon/tools/appengine $ direnv $ goapp deploy -application [your-project-id] ./src/app
  15. import "github.com/kaneshin/pigeon" import "github.com/kaneshin/pigeon/credentials" func main() { // Initialize vision

    service by a credentials json. creds := credentials.NewApplicationCredentials("credentials.json") config := NewConfig().WithCredentials(creds) client, err := pigeon.New(config) if err != nil { panic(err) } // To call multiple image annotation requests. feature := pigeon.NewFeature(pigeon.LabelDetection) batch, err := pigeon.NewBatchAnnotateImageRequest([]string{"lenna.jpg"}, feature) if err != nil { panic(err) http://kaneshin.github.io/2016/02/27/pigeon/
  16. How to use API on GAE • Cloud Vision API

    • Calendar API • Cloud Storage API • Youtube API • …
  17. Non GAE config, err := google.JWTConfigFromJSON(b, scope) if err !=

    nil { return nil, err } client := config.Client(context.Background()) srv, err := vision.New(client) if err != nil { return nil, err }
  18. http.DefaultTransport and http.DefaultClient are not available in App Engine. See

    https:// developers.google.com/appengine/docs/go/ urlfetch/overview
  19. http.DefaultTransport and http.DefaultClient are not available in App Engine. See

    https:// developers.google.com/appengine/docs/go/ urlfetch/overview
  20. http.Client on GAE via OAuth2 httpClient := &http.Client{ Transport: &oauth2.Transport{

    Source: google.AppEngineTokenSource(ctx, scope), Base: &urlfetch.Transport{Context: ctx}, }, }
  21. GAE Oriented • Unavailable packages including Sirupsen/logrus • PR: https://github.com/Sirupsen/logrus/pull/343

    • Some packages can’t access to http.Client to send a request. • Web Application Framework • Double Context Problem. (Complicated)
  22. GAE Oriented - http.Client Best Practice type ( // A

    Config provides service configuration for service clients. // all clients will use the {defaults.DefaultConfig} structure. Config struct { // The credentials object to use when signing requests. // Defaults to application credentials file. Credentials *credentials.Credentials // The HTTP client to use when sending requests. // Defaults to `http.DefaultClient`. HTTPClient *http.Client } )