-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.go
342 lines (279 loc) · 7.91 KB
/
requests.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package clarifai2
import (
"fmt"
"encoding/json"
"encoding/base64"
"io/ioutil"
)
/*
Data Structure Definition for JSON
*/
type Bytes []byte
type Input struct {
Id string `json:"id,omitempty"`
Data *Data`json:"data,omitempty"`
}
type Image struct {
URL string `json:"url,omitempty"`
Base string `json:"base64,omitempty"`
Crop []float64 `json:"crop,omitempty"`
}
type Video struct {
URL string `json:"url,omitempty"`
Base string `json:"base64,omitempty"`
}
type OutputConfig struct {
SelectConcepts []Concept `json:"select_concepts,omitempty"`
MaxConcepts int `json:"max_concepts,omitempty"`
MinValue int `json:"min_value",omitempty"`
ConceptsMutuallyExclusive bool `json:"concepts_mutually_exclusive,omitempty"`
ClosedEnvironment bool `json:"closed_environment,omitempty"`
}
type OutputInfo struct {
Message string `json:"message,omitempty"`
Type string `json:"type,omitempty"`
TypeExt string `json:"type_ext,omitempty"`
OutputConfig *OutputConfig `json:"output_config,omitempty"`
Data Data `json:"data,omitempty"`
}
type Model struct {
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
AppId string `json:"app_id,omitempty"`
OutputInfo *OutputInfo `json:"output_info"`
ModelVersion *ModelVersion `json:"model_version,omitempty"`
}
type ModelVersion struct {
Id string `json:"id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Status Status `json:"status,omitempty"`
}
type Concept struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
AppId string `json:"app_id,omitempty"`
Value float32 `json:"value,omitempty"`
}
type Status struct {
code string `json:"code"`
description string `json:"description"`
}
type GeoLimit struct {
Type string `json:"type"`
Value int `json:"value"`
}
type Geo struct {
GeoPoint struct {
Longitude int `json:"longitude"`
Latitude int `json:"latitude"`
} `json:"geo_point"`
GeoLimit *GeoLimit `json:"geo_limit,omitempty"`
}
type Data struct {
Image *Image `json:"image,omitempty"`
Video *Video `json:"video,omitempty"`
Concepts []Concept `json:"concepts,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Geo *Geo `json:"geo,omitempty"`
}
type Output struct {
Data *Data `json:"data,omitempty"`
Input *Input `json:"input,omitempty"`
}
type Ands struct {
Input *Input `json:"input,omitempty"`
Output *Output `json:"output,omitempty"`
}
type SearchReq struct {
Query struct {
Ands []Ands `json:"ands"`
} `json:"query"`
}
type InputReq struct {
Input []Input `json:"inputs,omitempty"`
Action string `json:"action,omitempty"`
DeleteAll bool `json:"delete_all,omitempty"`
}
type ModelReq struct {
Models []Model `json:"models,omitempty"`
Concepts []Concept `json:"concepts,omitempty"`
Action string `json:"action,omitempty"`
}
type PredictReq struct {
Input []Input `json:"inputs"`
Model *Model `json:"model,omitempty"`
}
type Response struct {
Status Status `json:"status"`
Output []struct {
Id string `json:"id"`
Data Data `json:"data"`
CreatedAt string `json:"created_at"`
Model Model `json:"model"`
Input Input `json:"input"`
Status Status `json:"status"`
} `json:"outputs"`
Input []struct {
Id string `json:"id"`
Data Data `json:"data"`
CreatedAt string`json:"created_at"`
ModifiedAt string `json:"modified_at"`
Status Status `json:"status"`
} `json:"inputs"`
}
/*
Predict - analyze images to find tags
*/
func (client *Client) Predict(req PredictReq, model string) (*Response, error) {
// build endpoint
endpoint := "models/" + model + "/outputs"
res, err := client.commonHTTPRequest(req, endpoint, "POST", false)
if err != nil {
return nil, err
}
prediction := new(Response)
err = json.Unmarshal(res, prediction)
return prediction, err
}
// Send images by specifying external URLs
func (client *Client) PredictByUrls(url []string, model string) (*Response, error) {
req := PredictReq{Input: make([]Input, len(url))}
for i := range url {
req.Input[i].Data = new(Data)
req.Input[i].Data.Image = new(Image)
req.Input[i].Data.Image.URL = url[i]
}
return client.Predict(req, model)
}
// Send images as binary data (i.e. []byte)
func (client *Client) PredictByBytes(data []Bytes, model string) (*Response, error) {
// encode by base64
req := PredictReq{Input: make([]Input, len(data))}
for i := range data {
req.Input[i].Data = new(Data)
req.Input[i].Data.Image = new(Image)
req.Input[i].Data.Image.Base = base64.StdEncoding.EncodeToString(data[i])
}
return client.Predict(req, model)
}
// Send image files stored locally
func (client *Client) PredictByFiles(path []string, model string) (*Response, error) {
body := make([]Bytes, len(path))
for i := range path {
b, err := ioutil.ReadFile(path[i])
body[i] = b
if err != nil {
return nil, err
}
}
return client.PredictByBytes(body, model)
}
// Send videos by specifying external URLs
func (client *Client) PredictVideoByUrls(url []string, model string) (*Response, error) {
req := PredictReq{Input: make([]Input, len(url))}
for i := range url {
req.Input[i].Data = new(Data)
req.Input[i].Data.Video = new(Video)
req.Input[i].Data.Video.URL = url[i]
}
return client.Predict(req, model)
}
// Send videos as binary data (i.e. [] byte)
func (client *Client) PredictVideoByBytes(data []Bytes, model string) (*Response, error) {
// encode by base64
req := PredictReq{Input: make([]Input, len(data))}
for i := range data {
req.Input[i].Data = new(Data)
req.Input[i].Data.Video = new(Video)
req.Input[i].Data.Video.Base = base64.StdEncoding.EncodeToString(data[i])
}
return client.Predict(req, model)
}
// Send video files stored locally
func (client *Client) PredictVideoByFiles(path []string, model string) (*Response, error) {
body := make([]Bytes, len(path))
for i := range path {
b, err := ioutil.ReadFile(path[i])
body[i] = b
if err != nil {
return nil, err
}
}
return client.PredictVideoByBytes(body, model)
}
/*
Input - Store images to Clarifai server
*/
func (client *Client) Input(req InputReq) (*Response, error) {
// build endpoint
endpoint := "inputs"
res, err := client.commonHTTPRequest(req, endpoint, "POST", false)
if err != nil {
return nil, err
}
response := new(Response)
err = json.Unmarshal(res, response)
return response, err
}
// Send images specified by URLs to Clarifai
func (client *Client) InputByUrls(url []string) (*Response, error) {
req := InputReq{Input: make([]Input, len(url))}
for i := range url {
req.Input[i].Data = new(Data)
req.Input[i].Data.Image = new(Image)
req.Input[i].Data.Image.URL = url[i]
}
return client.Input(req)
}
// Send images stored locally to Clarifai
func (client *Client) InputByBytes(data []Bytes) (*Response, error) {
// encode by base64
req := InputReq{Input: make([]Input, len(data))}
for i := range data {
req.Input[i].Data = new(Data)
req.Input[i].Data.Image = new(Image)
req.Input[i].Data.Image.Base = base64.StdEncoding.EncodeToString(data[i])
}
return client.Input(req)
}
// Send local files onto Clarifai server
func (client *Client) InputByFiles(path []string) (*Response, error) {
body := make([]Bytes, len(path))
for i := range path {
b, err := ioutil.ReadFile(path[i])
body[i] = b
if err != nil {
return nil, err
}
}
return client.InputByBytes(body)
}
// Delete all the images on Clarifai server
func (client *Client) DeleteAllInputs() (*Response, error) {
req := InputReq{DeleteAll:true}
endpoint := "inputs"
res, err := client.commonHTTPRequest(req, endpoint, "DELETE", false)
if err != nil {
return nil, err
}
response := new(Response)
err = json.Unmarshal(res, response)
return response, err
}
/*
Search - find an image that matches a criteria
*/
// need more work...
/*
Utility functions
*/
// Obtain top five tags
func (client *Client) TopFive(prediction *Response) (tags []string) {
tags = make([]string, 5)
for i := 0; i < 5; i++ {
tags[i] = prediction.Output[0].Data.Concepts[i].Name
fmt.Println(tags[i])
}
return tags
}