/
response.go
153 lines (132 loc) · 3.23 KB
/
response.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package vision
import SDK "google.golang.org/api/vision/v1"
// Response contains response from Annotate API.
type Response struct {
*SDK.BatchAnnotateImagesResponse
}
// Len returns size of response.
// This coinsides the number of images in request.
func (r *Response) Len() int {
if r.BatchAnnotateImagesResponse == nil {
return 0
}
return len(r.BatchAnnotateImagesResponse.Responses)
}
// FaceResult returns all of detection score(0-1) from face detection results.
func (r *Response) FaceResult() []float64 {
if r.Len() == 0 {
return nil
}
var list []float64
for _, resp := range r.Responses {
for _, anno := range resp.FaceAnnotations {
list = append(list, anno.DetectionConfidence)
}
}
return list
}
// LandmarkResult returns all of scores from landmark detection results.
func (r *Response) LandmarkResult() []Entity {
if r.Len() == 0 {
return nil
}
var list []Entity
for _, resp := range r.Responses {
for _, anno := range resp.LandmarkAnnotations {
list = append(list, Entity{
Description: anno.Description,
Score: anno.Score,
})
}
}
return list
}
// LogoResult returns all of scores from logo detection results.
func (r *Response) LogoResult() []Entity {
if r.Len() == 0 {
return nil
}
var list []Entity
for _, resp := range r.Responses {
for _, anno := range resp.LogoAnnotations {
list = append(list, Entity{
Description: anno.Description,
Score: anno.Score,
})
}
}
return list
}
// LabelResult returns all of scores from label detection results.
func (r *Response) LabelResult() []Entity {
if r.Len() == 0 {
return nil
}
var list []Entity
for _, resp := range r.Responses {
for _, anno := range resp.LabelAnnotations {
list = append(list, Entity{
Description: anno.Description,
Score: anno.Score,
})
}
}
return list
}
// TextResult returns all of text from OCR results.
func (r *Response) TextResult() []string {
if r.Len() == 0 {
return nil
}
var list []string
for _, resp := range r.Responses {
for _, anno := range resp.TextAnnotations {
list = append(list, anno.Description)
}
}
return list
}
// DocumentResult returns all of full text from OCR results.
func (r *Response) DocumentResult() []DocumentEntity {
if r.Len() == 0 {
return nil
}
list := make([]DocumentEntity, r.Len())
for i, resp := range r.Responses {
list[i] = NewDocumentEntity(resp.FullTextAnnotation)
}
return list
}
// SafeResult returns all of text from safe-search results.
func (r *Response) SafeResult() []SafeEntity {
if r.Len() == 0 {
return nil
}
list := make([]SafeEntity, r.Len())
for i, resp := range r.Responses {
list[i] = NewSafeEntity(resp.SafeSearchAnnotation)
}
return list
}
// CropResult returns all of data from crop hints results.
func (r *Response) CropResult() []CropEntity {
if r.Len() == 0 {
return nil
}
list := make([]CropEntity, r.Len())
for i, resp := range r.Responses {
list[i] = NewCropEntity(resp.CropHintsAnnotation)
}
return list
}
// WebResult returns all of data from Web Detection results.
func (r *Response) WebResult() []WebEntity {
if r.Len() == 0 {
return nil
}
list := make([]WebEntity, r.Len())
for i, resp := range r.Responses {
list[i] = NewWebEntity(resp.WebDetection)
}
return list
}