forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabel.go
89 lines (77 loc) · 1.97 KB
/
label.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Command label uses the Vision API's label detection capabilities to find a label based on an image's content.
package main
import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/vision/v1"
)
// findLabels gets labels from the Vision API for an image at the given file path.
func findLabels(file string) ([]string, error) {
ctx := context.Background()
// Authenticate to generate a vision service.
client, err := google.DefaultClient(ctx, vision.CloudPlatformScope)
if err != nil {
return nil, err
}
service, err := vision.New(client)
if err != nil {
return nil, err
}
// Read the image.
b, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
// Construct a label request, encoding the image in base64.
req := &vision.AnnotateImageRequest{
Image: &vision.Image{
Content: base64.StdEncoding.EncodeToString(b),
},
Features: []*vision.Feature{{Type: "LABEL_DETECTION"}},
}
batch := &vision.BatchAnnotateImagesRequest{
Requests: []*vision.AnnotateImageRequest{req},
}
res, err := service.Images.Annotate(batch).Do()
if err != nil {
return nil, err
}
labels := make([]string, 0)
for _, annotation := range res.Responses[0].LabelAnnotations {
labels = append(labels, annotation.Description)
}
return labels, nil
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <path-to-image>\n", filepath.Base(os.Args[0]))
}
flag.Parse()
args := flag.Args()
if len(args) == 0 {
flag.Usage()
os.Exit(1)
}
labels, err := findLabels(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if len(labels) == 0 {
fmt.Println("No labels found.")
} else {
fmt.Println("Found labels:")
for _, label := range labels {
fmt.Println(label)
}
}
}