-
Notifications
You must be signed in to change notification settings - Fork 9
/
responseHandling.go
180 lines (159 loc) · 5.55 KB
/
responseHandling.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
/**
* LiveDetect
* Copyright (c) 2018 Jolibrain
* Author: Corentin Barreau <[email protected]>
*
* This file is part of livedetect.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in livedetect without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of livedetect, and to permit persons to whom livedetect is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of livedetect.
*
* LIVEDETECT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH LIVEDETECT OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package main
import (
"bytes"
"fmt"
"image"
"image/draw"
"strconv"
"time"
"strings"
"github.com/jolibrain/godd"
"github.com/labstack/gommon/color"
jpeg "github.com/pixiv/go-libjpeg/jpeg"
)
func printResponse(request godd.PredictRequest, result godd.PredictResult, ID string, img image.Image, filePath string, startTime time.Time, index int) image.Image {
if result.Status.Code != 200 {
logError("Unexpected response status code: " + strconv.Itoa(result.Status.Code), "[ERROR]")
return img
}
// Initialize RGBA image
var imgRGBA *image.RGBA
// Initialize categories counter
categories := 0
// Print complete classes array if --verbose is true
if arguments.Verbose == "DEBUG" {
logSuccess("Raw classes: "+color.Cyan(result.Body.Predictions[0]),
"["+ID+"] [DEBUG]")
}
// Iterate through classes and print cat, probs and bbox if
// --detection is used
if len(result.Body.Predictions) != 0 {
// Convert to RGBA
b := img.Bounds()
imgRGBA = image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(imgRGBA, imgRGBA.Bounds(), img, b.Min, draw.Src)
// Loop over predictions
for _, class := range result.Body.Predictions[0].Classes {
cat := class.Cat
// Classes selection statement
if arguments.SelectClasses == true && sliceContains(*arguments.Classes, cat) == false {
} else {
if arguments.Verbose == "INFO" || arguments.Verbose == "DEBUG" {
fmt.Print("\n")
logSuccess("Category: "+color.Cyan(cat),
"["+ID+"] [INFO]")
}
prob := class.Prob
if arguments.Verbose == "INFO" || arguments.Verbose == "DEBUG" {
logSuccess("Probability: "+color.Cyan(prob),
"["+ID+"] ------")
}
// Print bbox if --detection
if arguments.Detection == true {
bbox := class.Bbox
if arguments.Verbose == "INFO" || arguments.Verbose == "DEBUG" {
logSuccess("Coordinates: "+color.Cyan(bbox),
"["+ID+"] ------")
}
// Parse mask output
if arguments.Mask == true {
mask := class.Mask
if arguments.Verbose == "INFO" || arguments.Verbose == "DEBUG" {
logSuccess("Mask Format: "+color.Cyan(mask.Format),
"["+ID+"] ------")
logSuccess("Mask Width: "+color.Cyan(mask.Width),
"["+ID+"] ------")
logSuccess("Mask Height: "+color.Cyan(mask.Height),
"["+ID+"] ------")
fmt.Print("\n")
}
if arguments.Preview != "" || arguments.KeepImg == true {
imgRGBA = writeMask(imgRGBA, result, categories, ID)
}
} else {
if arguments.Preview != "" || arguments.KeepImg == true {
imgRGBA = writeBoundingBox(imgRGBA, result, categories, ID, index)
}
}
}
// Push data to InfluxDB
if influxConnection == true {
if arguments.Verbose == "INFO" || arguments.Verbose == "DEBUG" {
logSuccess("Sending gathered data to "+
color.Cyan("InfluxDB"), "[INFO]")
}
go writePoints(influxClient, cat, floatToString(prob))
if arguments.Verbose == "INFO" || arguments.Verbose == "DEBUG" {
logSuccess("Data succesfully pushed to "+
color.Cyan("InfluxDB"), "[INFO]")
}
}
categories++
}
}
}
// Update real time buffer
if arguments.Verbose != "DEBUG" && arguments.Verbose != "INFO" {
elapsed := time.Since(startTime)
elapsedTimes = append(elapsedTimes, elapsed)
fmt.Fprintln(writer, color.Green("[✔] ["+ID+"]")+
color.Yellow(" Image processed! ")+
color.Green(strconv.Itoa(categories))+
color.Yellow(" categories found! ")+
color.Cyan(elapsed)+
color.Green(" | ")+
color.Yellow("Average processing time: ")+
color.Cyan(averageTime(elapsedTimes)))
}
// Preview window
if arguments.Preview != "" {
// Convert image to buffer
buf := new(bytes.Buffer)
if imgRGBA != nil {
err := jpeg.Encode(buf, imgRGBA, &jpeg.EncoderOptions{Quality: 50})
if err == nil {
go stream.Update(buf.Bytes())
} else {
logError("Can't encode frame to live stream.", "[ERROR]")
}
}
}
// Keep json on disk
if arguments.KeepJson == true {
// Place json file next to processed image file
var logPath string
logPath = strings.TrimSuffix(filePath, ".jpg") + ".json"
// Add Service name suffix if specified
if request.Service != "" {
logPath = strings.Replace(logPath, ".json", "_" + request.Service + ".json", -1)
}
// Write predict response inside json file
go keepJson(logPath, result)
}
return imgRGBA
}