at kaneshin
Home Talks Posts Links

27 Feb 2016
Pigeon - Library for Cloud Vision API

go CloudVisionApi GoogleCloudPlatform

pigeon

Pigeon - Google Cloud Vision API on Golang

pigeon is a package for the Google Cloud Vision API on Golang. It also provides pigeon command and pigeon-app which is serving an image to submit.

Getting Started

Create a project with the Google Cloud Console, and enable the Vision API. Set up your environment with Application Default Credentials. For example, from the Cloud Console, you might create a service account, download its json credentials file, then set the appropriate environment variable:

$ export GOOGLE_APPLICATION_CREDENTIALS=~/.credentials/service_account.json

Installation

pigeon and pigeon-app commands

pigeon provides the command-line tools.

$ go get github.com/kaneshin/pigeon/tools/cmd/...

Make sure that pigeon was installed correctly:

$ pigeon -h
$ pigeon-app -h

pigeon package

Type the following line to install pigeon package.

$ go get github.com/kaneshin/pigeon

Usage

pigeon command

pigeon is available to submit request with external image source (i.e. Google Cloud Storage image location).

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

pigeon-cmd

pigeon-app command

# Default port is 8080.
# Default Detection is LabelDetection.
$ pigeon-app
$ pigeon-app -port=8000 -- -face -label -safe-search
$ curl -XGET localhost:8080/

pigeon-app

pigeon package

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")

	// creds will set a pointer of credentials object using env value of
	// "GOOGLE_APPLICATION_CREDENTIALS" if pass empty string to argument.
	// creds := credentials.NewApplicationCredentials("")

	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)
	}

	// Execute the "vision.images.annotate".
	res, err := client.ImagesService().Annotate(batch).Do()
	if err != nil {
		panic(err)
	}

	// Marshal annotations from responses
	body, _ := json.MarshalIndent(res.Responses, "", "  ")
	fmt.Println(string(body))
}

Results

Label Detection

pigeon.png

$ pigeon -label pigeon.png
[
    {
        "labelAnnotations": [
        {
            "description": "bird",
                "mid": "/m/015p6",
                "score": 0.825656
        },
        {
            "description": "anatidae",
            "mid": "/m/01c_0l",
            "score": 0.58264238
        }
        ]
    }
]

Safe Search Detection

lenna.jpg

$ pigeon -safe-search lenna.jpg
[
    {
        "safeSearchAnnotation": {
            "adult": "POSSIBLE",
            "medical": "UNLIKELY",
            "spoof": "VERY_UNLIKELY",
            "violence": "VERY_UNLIKELY"
        }
    }
]

Resources


Til next time,
kaneshin at 15:30