-
Notifications
You must be signed in to change notification settings - Fork 0
/
glasses.go
106 lines (85 loc) · 2.85 KB
/
glasses.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package glasses
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
const (
CLOUD_VISION_ENDPOINT = "https://vision.googleapis.com/v1alpha1/images:annotate"
)
type Glasses struct {
Client *http.Client
}
type CloudVisionRequest struct {
Requests []*AnnotateRequest `json:"requests"`
User string `json:"user"`
}
type AnnotateRequest struct {
Image *Image `json:"image"`
Features []Feature `json:"features"`
ImageContext *ImageContext `json:"imageContext,omitempty"`
}
type Feature struct {
Type string `json:"type"`
MaxResults int `json:"maxResults"`
}
type ImageContext struct {
LatLongRect interface{} `json:"latLongRect"`
ImageContextSearchExtension interface{} `json:"imageContextSearchExtension"`
}
//type AnnotateResponse struct {
//FaceAnnotations []FaceAnnotation `json:"faceAnnotations"`
//LandmarkAnnotations []LandmarkAnnotation `json:"landmarkAnnotation"`
//LogoAnnotations []LogoAnnotation `json:"logoAnnotations"`
//LabelAnnotations []LabelAnnotation `json:"labelAnnotations"`
//TextAnnotations []TextAnnotation `json:"textAnnotations"`
//SafeSearchAnnotation SafeSearchAnnotation `json:"safeSearchAnnotation"`
//SuggestAnnotations []SuggestAnnotation `json:"suggestAnnotations"`
//QueryAnnotation QueryAnnotation `json:"queryAnnotation"`
//Error Status `json:"error"`
//}
type AnnotateResponses struct {
Responses []AnnotateResponse `json:"responses"`
}
type AnnotateResponse struct {
FaceAnnotations []interface{} `json:"faceAnnotations"`
LandmarkAnnotations []interface{} `json:"landmarkAnnotation"`
LogoAnnotations []interface{} `json:"logoAnnotations"`
LabelAnnotations []interface{} `json:"labelAnnotations"`
TextAnnotations []interface{} `json:"textAnnotations"`
SafeSearchAnnotation interface{} `json:"safeSearchAnnotation"`
SuggestAnnotations []interface{} `json:"suggestAnnotations"`
QueryAnnotation interface{} `json:"queryAnnotation"`
Error interface{} `json:"error"`
}
func NewGlasses() (*Glasses, error) {
client, err := google.DefaultClient(oauth2.NoContext, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return nil, err
}
return &Glasses{client}, nil
}
func (g *Glasses) Do(r *CloudVisionRequest) (*AnnotateResponses, error) {
jR, err := json.Marshal(r)
if err != nil {
return nil, err
}
rawResp, err := g.Client.Post(CLOUD_VISION_ENDPOINT, "application/json", bytes.NewBuffer(jR))
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(rawResp.Body)
if err != nil {
return nil, err
}
//log.Println("Body:", string(body))
var resp *AnnotateResponses
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, nil
}