-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
93 lines (73 loc) · 1.81 KB
/
detect.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
package main
import (
"bytes"
"errors"
"html/template"
"image"
"image/color"
"image/draw"
"image/jpeg"
_ "image/png"
"net/http"
"github.com/Kagami/go-face"
)
var FaceDetector *face.Recognizer
func init() {
if fd, err := face.NewRecognizer("./models"); err != nil {
LogCode(err)
} else {
FaceDetector = fd
}
http.HandleFunc("/", faceDetect)
}
func faceDetect(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
LogCode()
if data, _, err := r.FormFile("foto"); err != nil {
LogCode(err)
} else {
d := streamToByte(data)
if _, dd, err := faceDetecting(d); err != nil {
LogCode(err)
} else {
if dd != nil {
w.Header().Add("Content-type", http.DetectContentType(dd))
w.Write(dd)
}
}
}
w.Write([]byte(r.FormValue("name")))
} else {
var tmpl, _ = template.ParseFiles("form.html")
tmpl.Execute(w, nil)
}
}
func faceDetecting(data []byte) ([]ObjectDetected, []byte, error) {
if faces, err := FaceDetector.Recognize(data); err != nil {
return nil, nil, err
} else {
if len(faces) > 0 {
if image_res, _, err := image.Decode(bytes.NewReader(data)); err != nil {
return nil, nil, err
} else {
var detected []ObjectDetected
image_dst := image.NewRGBA(image_res.Bounds())
draw.Draw(image_dst, image_res.Bounds(), image_res, image.Point{}, draw.Src)
for i, f := range faces {
LogCode()
draw.Draw(image_dst, f.Rectangle, &image.Uniform{C: color.Black}, image.Point{}, draw.Src)
detected = append(detected, ObjectDetected{
Id: i,
Rect: f.Rectangle,
})
}
var rsb bytes.Buffer
if err := jpeg.Encode(&rsb, image_dst, &jpeg.Options{Quality: 100}); err != nil {
LogCode(err)
}
return detected, rsb.Bytes(), err
}
}
}
return nil, data, errors.New("can't detect faces")
}