-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
460 lines (398 loc) · 9.5 KB
/
main.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
vision "cloud.google.com/go/vision/apiv1"
)
const (
serverURL = "http://localhost:8090/api/v1"
authToken = "8W5zQ8nrSHe4NdBG"
canvasId = "e90af7cf-2164-4ce3-b831-3fbb7d1449ae"
gvisionKeyFile = "/home/igor/gvision_keys.json"
)
type apiCallError struct {
code int
body []byte
}
func (e apiCallError) Error() string {
return fmt.Sprintf("Response status code is %v, response body: %s",
e.code, e.body)
}
func NewApiCallError(resp *http.Response) apiCallError {
var e apiCallError
e.code = resp.StatusCode
e.body, _ = ioutil.ReadAll(resp.Body)
return e
}
func readStreamingEndpoint(url string, output chan<- []byte) error {
defer close(output)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add("Private-Token", authToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return NewApiCallError(resp)
}
var buff [1]byte
var line []byte
Loop:
for {
n, err := resp.Body.Read(buff[:])
if err != nil {
return err
}
if n == 0 {
continue Loop
}
if buff[0] == '\n' {
if len(line) > 0 {
output <- line
}
line = nil
} else {
line = append(line, buff[0])
}
}
return nil
}
type WidgetSize struct {
Height float64 `json:"height"`
Width float64 `json:"width"`
}
type CanvasImage struct {
WidgetId string `json:"id"`
WidgetType string `json:"widget_type"`
State string `json:"state"`
Hash string `json:"hash"`
Size WidgetSize `json:"size"`
}
type CanvasNote struct {
WidgetId string `json:"id"`
ParentId string `json:"parent_id"`
WidgetType string `json:"widget_type"`
State string `json:"state"`
Text string `json:"text"`
BackgroundColor string `json:"background_color"`
}
type ImageStatus struct {
image CanvasImage
annotated bool
}
type ImageAnnotation struct {
image *CanvasImage
labels string
text string
}
var gImageDB map[string]*ImageStatus
var gVisonQueue chan *CanvasImage
var gAnnotationQueue chan *ImageAnnotation
func updateImageStatus(imageStatus *ImageStatus) {
image := imageStatus.image
if len(image.Hash) == 0 {
return
}
if imageStatus.annotated {
return
}
go annotateImage(image)
imageStatus.annotated = true
}
func updateImageDB(image CanvasImage) {
if image.WidgetType != "Image" {
log.Printf("Unexpected widget type: %s", image.WidgetType)
return
}
if len(image.WidgetId) == 0 {
log.Printf("Empty widget id")
return
}
var imageAlive bool
switch image.State {
case "normal":
imageAlive = true
case "deleted":
imageAlive = false
default:
log.Printf("Unexpected widget state: %s", image.State)
return
}
imageStatus, ok := gImageDB[image.WidgetId]
if !ok {
imageStatus := &ImageStatus{
image: image,
annotated: false,
}
gImageDB[image.WidgetId] = imageStatus
updateImageStatus(imageStatus)
} else {
if imageAlive {
imageStatus.image = image
updateImageStatus(imageStatus)
} else {
delete(gImageDB, image.WidgetId)
}
}
}
func processRawJsonStream(input <-chan []byte) {
for {
rawJson, ok := <-input
if !ok {
return
}
imageList := make([]CanvasImage, 0, 1)
//log.Printf("Raw json: %s", rawJson)
err := json.Unmarshal(rawJson, &imageList)
if err != nil {
log.Print(err.Error())
} else {
for _, image := range imageList {
updateImageDB(image)
}
}
}
}
func getImageDataStream(imageId string) (io.ReadCloser, error) {
endpoint := serverURL + "/canvases/" + canvasId + "/images/" + imageId + "/download"
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Add("Private-Token", authToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%v response is %v\n", endpoint, resp.StatusCode)
}
return resp.Body, nil
}
var currentColor int
var magicColors []string = []string{
"#ccff66",
"#99ff66",
"#66ff99",
"#99ccff",
}
func getMagicColor() string {
currentColor++
return magicColors[currentColor%len(magicColors)]
}
func isMagicColor(color string) bool {
for _, magic := range magicColors {
if strings.EqualFold(color, magic) {
return true
}
}
return false
}
func checkAnnnotations(image CanvasImage) (bool, error) {
url := serverURL + "/canvases/" + canvasId + "/notes"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false, err
}
req.Header.Add("Private-Token", authToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("Status code is %v, response: %s", resp.StatusCode, body)
}
var notes []CanvasNote
err = json.Unmarshal(body, ¬es)
if err != nil {
return false, err
}
for _, note := range notes {
if note.ParentId == image.WidgetId && isMagicColor(note.BackgroundColor) {
return true, nil
}
}
return false, nil
}
func annotateImage(image CanvasImage) {
log.Printf("Check image %s", image.WidgetId)
annotated, err := checkAnnnotations(image)
if err != nil {
log.Printf("Can't get info about existing annotations: %s", err.Error())
return
}
if annotated {
log.Printf("Image is already annotated")
return
}
gVisonQueue <- &image
}
func attachNote(image *CanvasImage, noteColor string, relPosX, relPosY float64, noteText string) {
url := serverURL + "/canvases/" + canvasId + "/notes"
note_side := 300.0
note_scale := (image.Size.Height / 4.5) / note_side
pos_x := image.Size.Width - (note_side*relPosX)*note_scale
if pos_x < 0 {
pos_x = image.Size.Width
}
pos_y := image.Size.Height - (note_side*relPosY)*note_scale
if pos_y < 0 {
pos_y = image.Size.Height
}
note := fmt.Sprintf(`{
"parent_id" : "%s",
"text": "%s",
"depth": 1,
"background_color" : "%s",
"size": {
"width": %v,
"height": %v
},
"location": {
"x": %v,
"y": %v
},
"scale" : %v
}`, image.WidgetId,
noteText,
noteColor,
note_side,
note_side,
pos_x,
pos_y, note_scale)
//log.Printf("Gonna annotate with JSON: %s", note)
req, err := http.NewRequest("POST", url, strings.NewReader(note))
if err != nil {
log.Printf(err.Error())
return
}
req.Header.Add("Private-Token", authToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf(err.Error())
return
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
log.Printf("Status code is %v, response: %s", resp.StatusCode, body)
return
}
log.Printf("Annotated %s", image.WidgetId)
}
func annotationLoop() {
Loop:
for {
annotation, ok := <-gAnnotationQueue
if !ok {
break Loop
}
if len(annotation.labels) > 0 {
attachNote(annotation.image, getMagicColor(), 0.5, 1.33, annotation.labels)
}
if len(annotation.text) > 0 {
attachNote(annotation.image, "#ffff66", 2.0, 0.7, annotation.text)
}
}
}
func gvisionLoop() {
ctx := context.Background()
// Creates a client.
client, err := vision.NewImageAnnotatorClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
Loop:
for {
canvasImage, ok := <-gVisonQueue
if !ok {
break Loop
}
data, err := getImageDataStream(canvasImage.WidgetId)
if err != nil {
gAnnotationQueue <- &ImageAnnotation{
image: canvasImage,
labels: fmt.Sprintf("Can't get data stream of image %s, error: %s", canvasImage.WidgetId, err.Error()),
}
continue Loop
}
defer data.Close()
image, err := vision.NewImageFromReader(data)
if err != nil {
gAnnotationQueue <- &ImageAnnotation{
image: canvasImage,
labels: fmt.Sprintf("Can't get data stream of image %s, error: %s", canvasImage.WidgetId, err.Error()),
}
continue Loop
}
texts, err := client.DetectTexts(ctx, image, nil, 10)
if err != nil {
gAnnotationQueue <- &ImageAnnotation{
image: canvasImage,
labels: fmt.Sprintf("Can't get %s texts, error %s", canvasImage.WidgetId, err.Error()),
}
continue Loop
}
var text string
if len(texts) > 0 {
text = texts[0].Description
}
labels, err := client.DetectLabels(ctx, image, nil, 10)
if err != nil {
gAnnotationQueue <- &ImageAnnotation{
image: canvasImage,
labels: fmt.Sprintf("Can't get %s labels, error: %s", canvasImage.WidgetId, err.Error()),
}
continue Loop
}
var descriptions []string
for _, label := range labels {
descriptions = append(descriptions, label.Description)
}
gAnnotationQueue <- &ImageAnnotation{
image: canvasImage,
labels: strings.Join(descriptions, "\r\n"),
text: text,
}
}
}
func mainLoop() {
url := serverURL + "/canvases/" + canvasId + "/images?subscribe"
for {
rawJsonStream := make(chan []byte)
go processRawJsonStream(rawJsonStream)
err := readStreamingEndpoint(url, rawJsonStream)
if apiErr, ok := err.(apiCallError); ok {
log.Print(apiErr.Error())
return // User needs to change config
}
// The network error, sleep and try again
const timeout = 3 * time.Second
log.Printf("Error %v, reconnect after %s", err.Error(), timeout)
time.Sleep(timeout)
}
}
func main() {
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", gvisionKeyFile)
gImageDB = make(map[string]*ImageStatus)
gVisonQueue = make(chan *CanvasImage)
gAnnotationQueue = make(chan *ImageAnnotation)
go gvisionLoop()
go annotationLoop()
mainLoop()
}