-
Notifications
You must be signed in to change notification settings - Fork 24
/
handlers.go
275 lines (229 loc) · 6.85 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"gocv.io/x/gocv"
)
type imageScoringResult struct {
AppVersion string `json:"app_version"`
OpenNsfwScore float32 `json:"open_nsfw_score"`
AnAlgorithmForNudityDetection bool `json:"an_algorithm_for_nudity_detection"`
ImageName string `json:"image_name"`
}
type testResult struct {
OpenNsfwScore float32 `json:"open_nsfw_score,omitempty"`
Nudity bool `json:"an_algorithm_for_nudity_detection"`
}
type pdfResponse struct {
AppVersion string `json:"app_version"`
Result map[string]testResult `json:"result"`
NudityDetectionDisabled bool `json:"nudity_detection_disabled"`
ImageScoringDisabled bool `json:"image_scoring_disabled"`
ImageName string `json:"image_name"`
AlgorithmForNudityDetection bool `json:"an_algorithm_for_nudity_detection"`
OpenNsfwScore float32 `json:"open_nsfw_score,omitempty"`
}
func proceedPDF(w http.ResponseWriter, r *http.Request) {
dir, err := ioutil.TempDir(os.TempDir(), "adult-image-detector-*-pdf")
if err != nil {
HandleError(w, err)
return
}
parsedForm, err := uploadFileFormHandler(r, dir, "pdf")
if err != nil {
HandleError(w, err)
return
}
if parsedForm.FileExt != strings.ToLower("pdf") {
HandleError(w, fmt.Errorf("bad request. Invalid file type"))
return
}
images, dir, err := getImagesFromPDF(parsedForm.FilePath, dir, parsedForm.password)
if err != nil {
HandleError(w, err)
return
}
body := pdfResponse{
AppVersion: VERSION,
ImageScoringDisabled: parsedForm.disableOpenNsfw,
NudityDetectionDisabled: parsedForm.disableAnAlgorithm,
ImageName: parsedForm.Filename,
}
var res = make(map[string]testResult)
protoPath, _ := filepath.Abs("./models/open_nsfw/nsfw_model/deploy.prototxt")
modelPath, _ := filepath.Abs("./models/open_nsfw/nsfw_model/resnet_50_1by2_nsfw.caffemodel")
net := gocv.ReadNetFromCaffe(
protoPath,
modelPath,
)
if net.Empty() {
HandleError(w, err)
return
}
defer net.Close()
for _, v := range images {
var r testResult
// r.ImageName = v
if !parsedForm.disableOpenNsfw {
openNsfwScore, err := getOpenNsfwScore(v, net)
if err != nil {
continue
}
if body.OpenNsfwScore < openNsfwScore {
body.OpenNsfwScore = openNsfwScore
}
r.OpenNsfwScore = openNsfwScore
}
if !parsedForm.disableAnAlgorithm {
nudity, err := getAnAlgorithmForNudityDetectionResult(v, parsedForm.debug)
if err != nil {
continue
}
if nudity {
body.AlgorithmForNudityDetection = nudity
}
r.Nudity = nudity
}
res[filepath.Base(v)] = r
}
body.Result = res
// remove uploaded file
removeFile(parsedForm.FilePath)
removeFile(dir)
// serialize answer
out, err := json.Marshal(body)
if err != nil {
HandleError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(out)
}
// save uploaded image and get scoring
func proceedImage(w http.ResponseWriter, r *http.Request) {
dir, err := ioutil.TempDir(os.TempDir(), "adult-image-detector-*-image")
if err != nil {
HandleError(w, err)
return
}
defer removeFile(dir)
// save image and get options
parsedForm, err := uploadFileFormHandler(r, dir, "image")
if err != nil {
HandleError(w, err)
return
}
log.Printf("Uploaded file %s, saved as %s", parsedForm.Filename, parsedForm.SaveAsFilename)
res := imageScoringResult{
ImageName: parsedForm.Filename,
AppVersion: VERSION,
}
protoPath, _ := filepath.Abs("./models/open_nsfw/nsfw_model/deploy.prototxt")
modelPath, _ := filepath.Abs("./models/open_nsfw/nsfw_model/resnet_50_1by2_nsfw.caffemodel")
net := gocv.ReadNetFromCaffe(
protoPath,
modelPath,
)
if net.Empty() {
HandleError(w, err)
return
}
defer net.Close()
if parsedForm.disableOpenNsfw == false {
// get yahoo open nfsw score
openNsfwScore, err := getOpenNsfwScore(parsedForm.FilePath, net)
if err != nil {
HandleError(w, err)
return
}
res.OpenNsfwScore = openNsfwScore
log.Printf("For file %s, openNsfwScore=%f", parsedForm.SaveAsFilename, openNsfwScore)
}
if parsedForm.disableAnAlgorithm == false {
// get An Algorithm for Nudity Detection
anAlgorithmForNudityDetection, err := getAnAlgorithmForNudityDetectionResult(parsedForm.FilePath, parsedForm.debug)
if err != nil {
HandleError(w, err)
return
}
res.AnAlgorithmForNudityDetection = anAlgorithmForNudityDetection
log.Printf("For file %s, anAlgorithmForNudityDetection=%t", parsedForm.SaveAsFilename, anAlgorithmForNudityDetection)
}
// remove uploaded file
removeFile(parsedForm.FilePath)
// serialize answer
out, err := json.Marshal(res)
if err != nil {
HandleError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(out)
}
// HandleError handles http error.
func HandleError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(500)
fmt.Fprintln(w, err.Error())
}
// ShowImageForm to upload jpg image.
func ShowImageForm(w http.ResponseWriter) {
form := `
<html>
<body>
<form action="/api/v1/detect" method="post" enctype="multipart/form-data">
<label>Select image file</label>
<input type="file" name="image" required accept="image/*"><br/>
<label>
<input type="checkbox" value="true" name="disableOpenNsfw"> disable open nsfw
</label><br/>
<label>
<input type="checkbox" value="true" name="disableAnAlgorithm"> disable an algorithm
</label><br/>
<button type="submit">Calc nude scores</button>
</form>
<pre>
curl -i -X POST -F "image=@Daddy_Lets_Me_Ride_His_Cock_preview_720p.mp4.jpg" http://localhost:9191/api/v1/detect
</pre>
</body>
</html>
`
w.Write([]byte(form))
}
// ShowPDFForm to upload pdf file.
func ShowPDFForm(w http.ResponseWriter) {
form := `
<html>
<body>
<form action="/api/v1/pdf_detect" method="post" enctype="multipart/form-data">
<label>Select pdf file</label>
<input type="file" name="pdf" required accept="application/pdf"><br/>
<label>
<input type="checkbox" value="true" name="disableOpenNsfw"> disable open nsfw
</label><br/>
<label>
<input type="checkbox" value="true" name="disableAnAlgorithm"> disable an algorithm
</label><br/>
<label>
<input type="checkbox" value="true" name="debug"> debug
</label><br/>
<label>
<input type="text" value="" name="password"> file password
</label><br/>
<button type="submit">Calc nude scores</button>
</form>
<pre>
curl -i -X POST -F "image=@Daddy_Lets_Me_Ride_His_Cock_preview_720p.mp4.pdf" http://localhost:9191/api/v1/pdf_detect
</pre>
</body>
</html>
`
w.Write([]byte(form))
}