-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
108 lines (96 loc) · 2.75 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
package main
import (
"flag"
"image"
"net/http"
"os"
"strconv"
"github.com/jeromelesaux/facerecognition/logger"
"github.com/jeromelesaux/facerecognition/model"
"github.com/jeromelesaux/facerecognition/web"
)
type stringflags []string
func (i *stringflags) String() string {
return ""
}
func (i *stringflags) Set(value string) error {
*i = append(*i, value)
return nil
}
var imagesfiles stringflags
var (
httpport = flag.String("httpport", "", "HTTP port value (default 8099).")
firstname = flag.String("firstname", "", "Firstname of the person to add.")
lastname = flag.String("lastname", "", "Lastname ot the person to add.")
add = flag.Bool("add", false, "Add the person in user lib.")
recognize = flag.Bool("recognize", false, "Recognize person from image.")
config = flag.String("config", "", "Path to the configuration file.")
)
type Value interface {
String() string
Set(string) error
}
func main() {
flag.Var(&imagesfiles, "imagesfiles", "List of the images files of the person to add in database")
flag.Parse()
if flag.NFlag() == 0 {
flag.PrintDefaults()
} else {
logger.Logf("configuration file %s", *config)
if *config != "" {
model.SetAndLoad(*config)
} else {
logger.Log("No configuration file set cannot continue.")
return
}
if !*recognize {
lib := model.GetFaceRecognitionLib()
t := lib.GetTrainer(model.PCAFeatureType)
t.Train()
for _, i := range imagesfiles {
f, err := os.Open(i)
if err != nil {
logger.Logf("error while opening file %s with error %v", i, err)
} else {
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
logger.Logf("error while decoding file %s with error %v", i, err)
} else {
mats, _ := lib.FindFace(&img)
if len(mats) == 0 {
mats = append(mats, lib.MatrixNVectorize(&img))
}
logger.Logf("found %d faces.", len(mats))
for _, m := range mats {
p, distance := t.Recognize(m)
logger.Log("Found " + p + " distance " + strconv.FormatFloat(distance, 'e', 2, 32))
}
}
}
}
} else {
if !*add {
lib := model.GetFaceRecognitionLib()
uf := model.NewFaceRecognitionItem()
uf.User.FirstName = *firstname
uf.User.LastName = *lastname
logger.Log("Adding " + uf.GetKey())
uf.DetectFaces(imagesfiles)
lib.AddUserFace(uf)
} else {
if *httpport != "" {
http.HandleFunc("/train", web.Training)
http.HandleFunc("/compare", web.Compare)
http.HandleFunc("/listpersons", web.ListPersons)
http.HandleFunc("/person", web.GetPerson)
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./static"))))
err := http.ListenAndServe(":"+*httpport, nil)
if err != nil {
logger.Log(err.Error())
}
}
}
}
}
}