Skip to content

Instantly share code, notes, and snippets.

@atotto
Created August 13, 2016 07:42
Show Gist options
  • Save atotto/4fe3ec32d30b82d46206c459c1fd0aa8 to your computer and use it in GitHub Desktop.
Save atotto/4fe3ec32d30b82d46206c459c1fd0aa8 to your computer and use it in GitHub Desktop.
google cloud vision golang example
package gcv_test
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"google.golang.org/api/googleapi/transport"
vision "google.golang.org/api/vision/v1"
)
// https://github.com/google/google-api-go-client/blob/master/GettingStarted.md
const developerKey = `Your ServerKey`
func ExampleGoogleCloudVisionAPI() {
data, err := ioutil.ReadFile("fiveyears.jpg")
enc := base64.StdEncoding.EncodeToString(data)
img := &vision.Image{Content: enc}
feature := &vision.Feature{
Type: "LABEL_DETECTION",
MaxResults: 10,
}
req := &vision.AnnotateImageRequest{
Image: img,
Features: []*vision.Feature{feature},
}
batch := &vision.BatchAnnotateImagesRequest{
Requests: []*vision.AnnotateImageRequest{req},
}
client := &http.Client{
Transport: &transport.APIKey{Key: developerKey},
}
svc, err := vision.New(client)
if err != nil {
log.Fatal(err)
}
res, err := svc.Images.Annotate(batch).Do()
if err != nil {
log.Fatal(err)
}
body, err := json.Marshal(res.Responses[0].LabelAnnotations)
fmt.Println(string(body))
}
@atotto
Copy link
Author

atotto commented Aug 13, 2016

[
  {
    "description": "text",
    "mid": "/m/07s6nbt",
    "score": 0.94959313
  },
  {
    "description": "cartoon",
    "mid": "/m/0215n",
    "score": 0.8991816
  },
  {
    "description": "sketch",
    "mid": "/m/07glzq",
    "score": 0.75349158
  },
  {
    "description": "illustration",
    "mid": "/m/01kr8f",
    "score": 0.66491926
  },
  {
    "description": "drawing",
    "mid": "/m/02csf",
    "score": 0.66085011
  }
]

@sathishvj
Copy link

sathishvj commented Apr 4, 2018

Hey, I reused your sample code with Type: "DOCUMENT_TEXT_DETECTION".

I get textAnnotations but I'm unable to get fullTextAnnotations. Any idea why that would be?

Ref: stackoverflow question

p.s. this was resolved. No issue with code; it was a GOPATH setting mistake on my side. :-(

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