-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
95 lines (72 loc) · 1.55 KB
/
server.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
package main
import (
"bytes"
"image"
"io"
"log"
"net/http"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/gin-gonic/gin"
"github.com/hatobus/Teikyo/callapi"
img "github.com/hatobus/Teikyo/imgprocessing"
"github.com/hatobus/Teikyo/util"
)
func main() {
r := gin.Default()
err := util.Loadenv()
if err != nil {
panic(err)
}
r.POST("/detect", createTeikyohandler)
r.Run(":8080")
}
func createTeikyohandler(c *gin.Context) {
form, _ := c.MultipartForm()
files := form.File["upload[]"]
errch := make(map[string]string, len(form.File))
b := new(bytes.Buffer)
var mul bool
for i, file := range files {
log.Println(file.Filename)
f, err := file.Open()
defer f.Close()
// 一回DecodeConfigでファイルをいじるとファイルが壊れるために
// 別のbufにコピーをして回避しておく
io.Copy(b, f)
_, format, err := image.DecodeConfig(b)
if err != nil {
errch[file.Filename] = err.Error()
b.Reset()
break
} else if format != "jpeg" {
errch[file.Filename] = "Filetype must be jpeg"
b.Reset()
break
}
b.Reset()
landmark, err := callapi.DetectFace(f)
if err != nil {
errch[file.Filename] = err.Error()
break
}
if len(landmark) == 0 {
errch[file.Filename] = "Human not found."
}
mul = false
if len(landmark) > 1 {
mul = true
}
for k, L := range landmark {
LM := L.ToLandmark()
err := img.GenTeikyo(f, LM, mul, i, k)
if err != nil {
errch[file.Filename] = err.Error()
}
}
}
c.JSON(http.StatusOK, gin.H{
"errors": errch,
})
}