-
Notifications
You must be signed in to change notification settings - Fork 1
/
edge_server.go
1152 lines (1069 loc) · 34.9 KB
/
edge_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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
// "bufio"
"bytes"
"edge-for-image/pkg/payload"
"encoding/base64"
"encoding/json"
"errors"
"io"
"io/ioutil"
// "path/filepath"
// "flag"
"fmt"
// "log"
"crypto/md5"
"database/sql"
"html/template"
"net/http"
"net/url"
"os"
"strconv"
// "mime/multipart"
// "regexp"
"strings"
"time"
"edge-for-image/pkg"
"edge-for-image/pkg/accessai"
_ "github.com/go-sql-driver/mysql"
"github.com/golang/glog"
"github.com/satori/go.uuid"
)
// var (
// CustConfig *pkg.Config
// AiCloud *accessai.Accessai
// Mydb *sql.DB
// )
// func init() {
// AiCloud = accessai.NewAccessai()
// CustConfig = pkg.InitConfig()
// }
// Config use for server
// type Config struct {
// port string
// staticDir string
// aiurl string
// }
type Manager struct {
CustConfig *pkg.Config
AiCloud *accessai.Accessai
Mydb *sql.DB
FaceidMap map[string]string
}
type BoundingBox struct {
TopLeftX string `json:"topleftx"`
TopLeftY string `json:"toplefty"`
Width string `json:"width"`
Height string `json:"height"`
}
type Face struct {
Confidence string `json:"confidence"`
Bound BoundingBox `json:"bound"`
}
type FaceSet struct {
FaceSetName string `json:"facesetname"`
FaceSetID string `json:"facesetid"`
CreateTime string `json:"createtime"`
}
type FaceInfo struct {
Id string
FaceSetName string `json:"facesetname"`
FaceID string `json:"faceid"`
Face Face `json:"face"`
ImageBase64 string `json:"imagebase64"`
Name string `json:"name"`
Age string `json:"age"`
Address string `json:"address"`
Imageaddress string `json:"imageaddress"`
ImageURL string `json:"imageurl"`
// alreadyknow bool `json:"alreadyknow"`
CreateTime int64 `json:"createtime"`
SimilaryImageURL string `json:"similaryimageURL"`
Similarity string `json:"similarity"`
}
func checkFacesetExist(config *pkg.Config, aicloud *accessai.Accessai, facesetmap map[string]string) *sql.DB {
db, err := sql.Open("mysql", config.DBConnStr)
if err != nil {
glog.Errorf("open db err: %s", err.Error())
os.Exit(1)
}
if err = db.Ping(); err != nil {
db.Close()
glog.Errorf("ping db err: %s", err.Error())
os.Exit(1)
}
// rows, err := db.Query("select * from faceset where facesetname = ?", config.FaceSetName)
rows, err := db.Query("select * from faceset")
if err != nil {
db.Close()
glog.Errorf("Query db err: %s", err.Error())
os.Exit(1)
}
defer rows.Close()
var face FaceSet
// facesets := make([]FaceSet, 0)
for rows.Next() {
var id int
// var facesetname interface{}
err := rows.Scan(&id, &face.FaceSetName, &face.FaceSetID, &face.CreateTime)
if err != nil {
db.Close()
glog.Errorf("scan db err: %s", err.Error())
os.Exit(1)
}
// facesets = append(facesets, face)
facesetmap[face.FaceSetName] = face.FaceSetID
}
if _, ok := facesetmap[config.FaceSetName]; !ok {
urlStr := config.Aiurl + "/v1/faceSet"
body := []byte(fmt.Sprintf("{\"faceSetName\":\"%s\"}", config.FaceSetName))
// resp, err := aicloud.FakeCreateFaceset(urlStr, http.MethodPost, body)
resp, err := aicloud.CreateFaceset(urlStr, http.MethodPost, body)
if err != nil {
glog.Errorf("create faceset err: %s", err.Error())
}
// data, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// db.Close()
// glog.Error(err)
// os.Exit(1)
// }
// glog.Infof("create faceset resp: %#v", resp)
// data := resp
bdata := make(map[string]interface{})
err = json.Unmarshal(resp, &bdata)
if err != nil {
glog.Errorf("Unmarshal err: %s", err.Error())
}
glog.Infof("create faceset data: %#v", bdata)
stmt, err := db.Prepare("INSERT faceset SET facesetname=?,facesetid=?,createtime=?")
if err != nil {
glog.Errorf("Prepare INSERT faceinfo err: %s", err.Error())
db.Close()
glog.Errorf("scan db err: %s", err.Error())
os.Exit(1)
}
defer stmt.Close()
glog.Infof("config faceset name=%s", config.FaceSetName)
_, err = stmt.Exec(config.FaceSetName, bdata["faceSetID"].(string), time.Now().UnixNano()/1e6)
if err != nil {
glog.Errorf("INSERT faceinfo err: %s", err.Error())
os.Exit(1)
}
facesetmap[config.FaceSetName] = bdata["faceSetID"].(string)
}
direc := config.StaticDir + "/" + config.FaceSetName
if _, err := os.Stat(direc); os.IsNotExist(err) {
err = os.Mkdir(direc, 0777)
if err != nil {
glog.Errorf("mkdir dir err: %s", err.Error())
os.Exit(1)
}
}
glog.Infof("faceset map: %#v", facesetmap)
return db
}
func NewManager(config *pkg.Config) *Manager {
// if _, err := db.Exec("create database if not exists aicloud"); err != nil {
// db.Close()
// glog.Errorf("create db err: %s", err.Error())
// os.Exit(1)
// }
aicloud := accessai.NewAccessai()
facesetmap := make(map[string]string)
m := &Manager{
CustConfig: config,
AiCloud: aicloud,
Mydb: checkFacesetExist(config, aicloud, facesetmap),
FaceidMap: facesetmap,
}
glog.Infof("facemap:%#v", m.FaceidMap)
return m
}
func (m *Manager) listAlllfiles(facesetname, filename string) bool {
files, err := ioutil.ReadDir(m.CustConfig.StaticDir + "/" + facesetname)
if err != nil {
glog.Error(err)
}
for _, files := range files {
if files.Name() == filename {
return true
}
}
return false
}
func (m *Manager) detectFace(imagebase64 string) ([]byte, error) {
// first detect image face
urlStr := m.CustConfig.Aiurl + "/v1/faceDetect"
body := []byte(fmt.Sprintf("{\"imageBase64\": \"%s\"}", imagebase64))
// resp, err := m.AiCloud.FakeFaceSearch(urlStr, http.MethodPost, body)
resp, err := m.AiCloud.FaceDetect(urlStr, http.MethodPost, body)
if err != nil {
glog.Errorf(err.Error())
return nil, err
}
// facedetect := resp
fdata := make(map[string]interface{})
err = json.Unmarshal(resp, &fdata)
if err != nil {
glog.Errorf(err.Error())
return nil, err
}
if _, ok := fdata["faceNum"]; !ok {
glog.Errorf("detect face return none")
return nil, errors.New("detect face return none")
}
if fdata["faceNum"].(float64) <= 0.0 {
return nil, nil
}
dfaces := fdata["faces"].([]interface{})
if len(dfaces) == 0 {
glog.Errorf("dfaces len is 0")
return nil, errors.New("dfaces len is 0")
}
jdface, err := json.Marshal(dfaces[0])
if err != nil {
glog.Errorf("Marshal face err: %s", err)
return nil, err
}
// var dface Face
// dface.Bound.Height = dfaces[0]["boundingBox"].(map[string]string)["height"]
// dface.Bound.Width = dfaces[0]["boundingBox"].(map[string]string)["width"]
// dface.Bound.TopLeftX = dfaces[0]["boundingBox"].(map[string]string)["topLeftX"]
// dface.Bound.TopLeftY = dfaces[0]["boundingBox"].(map[string]string)["topLeftY"]
// dface.Confidence = dfaces[0]["confidence"].(string)
// jdface, err := json.Marshal(dface)
// if err != nil {
// glog.Errorf(err.Error())
// return nil, err
// }
// glog.Infof("dface: %#v", jdface)
return jdface, nil
}
func (m *Manager) saveImageToFile(imagename string, imageDecode []byte) error {
buf := bytes.NewBuffer(nil)
imageReader := bytes.NewReader(imageDecode)
if _, err := io.Copy(buf, imageReader); err != nil {
glog.Errorf("file copy to buf err: %s", err.Error())
}
imageaddress := m.CustConfig.StaticDir + "/" + m.CustConfig.FaceSetName + "/" + imagename
fileToSave, err := os.OpenFile(imageaddress, os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
glog.Error(err)
return err
}
defer fileToSave.Close()
if _, err := io.Copy(fileToSave, buf); err != nil {
glog.Errorf("buf copy to file err: %s", err.Error())
}
return nil
}
func (m *Manager) createFacesetIfNotExist(facesetname string) error {
// rows, err := db.Query("select * from faceset where facesetname = ?", config.FaceSetName)
for key := range m.FaceidMap {
if key == facesetname {
glog.Infof("faceset %s is already exist!", facesetname)
return nil
}
}
urlStr := m.CustConfig.Aiurl + "/v1/faceSet"
body := []byte(fmt.Sprintf("{\"faceSetName\":\"%s\"}", facesetname))
// resp, err := aicloud.FakeCreateFaceset(urlStr, http.MethodPost, body)
resp, err := m.AiCloud.CreateFaceset(urlStr, http.MethodPost, body)
if err != nil {
glog.Errorf("create faceset err: %s", err.Error())
return err
}
bdata := make(map[string]interface{})
err = json.Unmarshal(resp, &bdata)
if err != nil {
glog.Errorf("Unmarshal err: %s", err.Error())
return err
}
glog.Infof("create faceset data: %#v", bdata)
stmt, err := m.Mydb.Prepare("INSERT faceset SET facesetname=?,facesetid=?,createtime=?")
if err != nil {
glog.Errorf("Prepare INSERT faceinfo err: %s", err.Error())
return err
}
defer stmt.Close()
glog.Infof("config faceset name=%s", facesetname)
_, err = stmt.Exec(facesetname, bdata["faceSetID"].(string), time.Now().UnixNano()/1e6)
if err != nil {
glog.Errorf("INSERT faceinfo err: %s", err.Error())
return err
}
m.FaceidMap[facesetname] = bdata["faceSetID"].(string)
direc := m.CustConfig.StaticDir + "/" + facesetname
if _, err := os.Stat(direc); os.IsNotExist(err) {
err = os.Mkdir(direc, 0777)
if err != nil {
glog.Errorf("mkdir dir err: %s", err.Error())
return err
}
}
glog.Infof("create facesetname: %s", facesetname)
return nil
}
func (m *Manager) searchFace(imageBase64, imagename, facesetname string) error {
if err := m.createFacesetIfNotExist(facesetname); err != nil {
return err
}
imageDecode, err := base64.StdEncoding.DecodeString(imageBase64)
if err != nil {
glog.Error("DecodeString err")
return errors.New("DecodeString image err")
}
buf := bytes.NewBuffer(nil)
imageReader := bytes.NewReader(imageDecode)
if _, err := io.Copy(buf, imageReader); err != nil {
glog.Errorf("file copy to buf err: %s", err.Error())
}
// imageBase64 := base64.StdEncoding.EncodeToString(buf.Bytes())
// imageBase64 = ""
// glog.Infof("image base64: %s", imageBase64)
// defer upFile.Close()
uid, err := uuid.NewV4()
if err != nil {
glog.Errorf("uuid generate error: %s", err.Error())
}
imagename = fmt.Sprintf("%s", uid) + "-" + imagename
filexist := m.listAlllfiles(m.CustConfig.FaceSetName, imagename)
if filexist {
glog.Error("file name already exist")
return errors.New("file name already exist")
}
// first detect image face
jdface, err := m.detectFace(imageBase64)
if err != nil {
glog.Error(err)
return err
}
glog.Infof("dface: %#v", jdface)
// search face in faceset
// /v1/faceSet/13345/faceSearch?url=http://100.114.203.102/data/2_8.png
if jdface != nil {
// save to file
imageaddress := m.CustConfig.StaticDir + "/" + m.CustConfig.FaceSetName + "/" + imagename
fileToSave, err := os.OpenFile(imageaddress, os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
glog.Error(err)
return err
}
defer fileToSave.Close()
if _, err := io.Copy(fileToSave, buf); err != nil {
glog.Errorf("buf copy to file err: %s", err.Error())
}
imageurl := "http://" + m.CustConfig.PublicHost + ":" + m.CustConfig.Port + "/" + m.CustConfig.FaceSetName + "/" + imagename
urlStr := m.CustConfig.Aiurl + "/v1/faceSet/" + m.FaceidMap[m.CustConfig.FaceSetName] + "/faceSearch?url=" + imageurl
// body := []byte(fmt.Sprintf("{\"imageUrl\": \"%s\"}", imageurl))
// resp, err := m.AiCloud.FakeFaceSearch(urlStr, http.MethodPost, body)
resp, err := m.AiCloud.FaceSearch(urlStr, http.MethodGet, nil)
if err != nil {
glog.Errorf(err.Error())
return err
}
if strings.Contains(string(resp), "have no face") {
glog.Error("image have no face")
return nil
}
data := resp
// glog.Infof("resp :%#v", data)
bdata := make(map[string]interface{})
err = json.Unmarshal(data, &bdata)
if err != nil {
glog.Errorf(err.Error())
return err
}
faces := bdata["faces"].([]interface{})
// glog.Infof("resp :%#v", faces)
largeface := make(map[string]interface{})
var largesimilar int64
if len(faces) > 0 {
// found := false
for _, v := range faces {
face := v.(map[string]interface{})
similar, err := strconv.ParseInt(face["similarity"].(string), 10, 32)
if err != nil {
glog.Errorf("parse error: %s", err.Error())
}
if similar >= largesimilar {
// success
largesimilar = similar
largeface = face
}
glog.Infof("face similarity: %s", face["similarity"])
}
// if !found {
// // todo
// glog.Infof("all similarities are two small")
// // m.insertIntoFacedb(imageaddress, "http://"+m.CustConfig.Host + ":" + m.CustConfig.Port+"/"+handler.Filename, "", "", "")
// // insert unknowfaceinfo
// err = pkg.InsertIntoFacedb(m.Mydb, m.CustConfig.FaceSetName, "", jdface, "", "", "", "", imageaddress, imageurl, time.Now().UnixNano()/1e6, "unknowfaceinfo")
// if err != nil {
// glog.Errorf("INSERT unknowfaceinfo err: %s", err)
// return err
// }
// }
}
if largesimilar >= m.CustConfig.Similarity {
return m.insertIntoKnow(strconv.FormatInt(largesimilar, 10), imageaddress, imageurl, largeface)
}
glog.Infof("no image are match in search")
// m.insertIntoFacedb(imageaddress, "http://"+m.CustConfig.Host + ":" + m.CustConfig.Port+"/"+handler.Filename, "", "", "")
err = pkg.InsertIntoFacedb(m.Mydb, m.CustConfig.FaceSetName, "", jdface, "", "", "", "", imageaddress, imageurl, time.Now().UnixNano()/1e6, "", "", "unknowfaceinfo")
if err != nil {
glog.Errorf("INSERT unknowfaceinfo err: %s", err)
return err
}
} else {
glog.Warning("image detect no face")
return nil
}
return nil
// glog.Infof("resp :%s", data)
}
func (m *Manager) insertIntoKnow(largesimilar, imageaddress, imageurl string, face map[string]interface{}) error {
// re := regexp.M
faceidToS := face["faceID"].(string)
index := 0
for k := range faceidToS {
if string(faceidToS[k]) != "0" {
index = k
break
}
}
glog.Infof("index: %d, face:%s", index, faceidToS[index:len(faceidToS)])
// rows, err := m.Mydb.Query(fmt.Sprintf("select * from facedb where faceid regexp '%s$'", face["faceID"]))
rows, err := m.Mydb.Query("select * from facedb where faceid = ?", faceidToS[index:len(faceidToS)])
if err != nil {
glog.Errorf("Query db err: %s", err.Error())
return err
}
defer rows.Close()
var knowface FaceInfo
// var faceinteface interface{}
faceinteface := make([]byte, 255)
var id int
knowsfaces := make([]FaceInfo, 0)
for rows.Next() {
err := rows.Scan(&id, &knowface.FaceSetName, &knowface.FaceID, &faceinteface, &knowface.ImageBase64, &knowface.Name, &knowface.Age, &knowface.Address, &knowface.Imageaddress, &knowface.ImageURL, &knowface.CreateTime, &knowface.SimilaryImageURL, &knowface.Similarity)
if err != nil {
glog.Errorf("scan db err: %s", err.Error())
return err
}
knowsfaces = append(knowsfaces, knowface)
}
if len(knowsfaces) == 1 {
// insert know face
glog.Infof("byte:%#v", faceinteface)
// jsonface, err := json.Marshal(faceinteface)
// if err != nil {
// glog.Errorf("Marshal face err: %s", err.Error())
// return err
// }
err = pkg.InsertIntoFacedb(m.Mydb, m.CustConfig.FaceSetName, strconv.Itoa(id), faceinteface, "", knowface.Name, knowface.Age, knowface.Address, imageaddress, imageurl, time.Now().UnixNano()/1e6, knowface.ImageURL, largesimilar, "knowfaceinfo")
if err != nil {
glog.Errorf("INSERT faceinfo err: %s", err)
return err
}
glog.Infof("found similarity face: %s", face["similarity"])
} else if len(knowsfaces) == 0 {
glog.Errorf("facedb has no record of that faceid: %s", face["faceID"])
return errors.New("facedb has no record of that faceid")
} else {
glog.Errorf("facedb has two many faces that match the faceid: %s", face["faceID"])
return errors.New("facedb has two many faces that match the faceid")
}
return nil
}
// func (m *Manager) insertIntoFacedb(facesetname, imageaddress, imageurl, name, age, address string, face interface{}) {
func (m *Manager) insertIntoFacedb(facesetname, imageaddress, imageurl, name, age, address string, face []byte) {
// /v1/faceSet/13345/addFace
urlStr := m.CustConfig.Aiurl + "/v1/faceSet/" + m.FaceidMap[facesetname] + "/addFace"
body := []byte(fmt.Sprintf("{\"imageUrl\": \"%s\", \"externalImageID\": \"%s\"}", imageurl, m.FaceidMap[facesetname]))
// resp, err := m.AiCloud.FakeAddFace(urlStr, http.MethodPost, body)
resp, err := m.AiCloud.AddFace(urlStr, http.MethodPut, body)
if err != nil {
glog.Errorf("search face err: %s", err.Error())
}
data := resp
// glog.Infof("string:%s, resp :%#v", data, data)
if len(resp) == 0 {
glog.Errorf("add face return 0 length")
return
}
bdata := make(map[string]interface{})
json.Unmarshal(data, &bdata)
// glog.Infof("data: %#v", data)
// glog.Infof("bdata: %#v", bdata["face"])
// jsonface, err := json.Marshal(bdata["face"])
// if err != nil {
// glog.Errorf("Marshal face err: %s", err)
// return
// }
// jdface, err := json.Marshal(face)
// glog.Infof("jdface:%#v", jdface)
// if err != nil {
// glog.Errorf("Marshal face err: %s", err)
// return
// }
err = pkg.InsertIntoFacedb(m.Mydb, facesetname, bdata["faceID"].(string), face, "", name, age, address, imageaddress, imageurl, time.Now().UnixNano()/1e6, "", "", "facedb")
if err != nil {
glog.Errorf("Prepare INSERT faceinfo err: %s", err.Error())
}
}
// faceset logic
func (m *Manager) faceset(w http.ResponseWriter, r *http.Request) {
glog.Infof("method: %s", r.Method)
if r.Method == "POST" {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
py := &payload.FacesetRequest{}
err = json.Unmarshal(data, &py)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("The input body is not valid format.")))
return
}
err = m.createFacesetIfNotExist(py.Faceset.Name);
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("create faceset %s error!", py.Faceset.Name)))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("creating faceset %s success or already exist.", py.Faceset.Name)))
return
} else {
glog.Infof("the method %s is not implemented yet.", r.Method)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("the method %s is not implemented yet.", r.Method)))
return
}
}
// upload logic
func (m *Manager) upload(w http.ResponseWriter, r *http.Request) {
glog.Infof("method: %s", r.Method)
if r.Method == "GET" {
crutime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("upload.gtpl")
t.Execute(w, token)
} else {
// r.ParseMultipartForm(32 << 20)
// upFile, handler, err := r.FormFile("uploadfile")
// if err != nil {
// glog.Error(err)
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(fmt.Sprintf("500 %s", err)))
// return
// }
data, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
bdata := make(map[string]interface{})
err = json.Unmarshal(data, &bdata)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
filename, ok := bdata["filename"].(string)
if !ok {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
}
imagebody, ok := bdata["imageBase64"].(string)
if !ok {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
}
facesetname, ok := bdata["facesetname"].(string)
if !ok {
glog.Error(err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("400 %s", err)))
}
go m.searchFace(imagebody, filename, facesetname)
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(fmt.Sprintf("500 %s", err)))
// return
// }
w.WriteHeader(http.StatusOK)
// fmt.Fprintf(w, "%v", handler.Header)
}
}
func (m *Manager) getAllfaces(facesetname, table string, start, end, numbers int64, timeby bool) ([]FaceInfo, error) {
sort := "desc"
if !timeby {
sort = "asc"
}
qstr := fmt.Sprintf("select * from %s where facesetname='%s' order by createtime %s", table, facesetname, sort)
glog.Infof(qstr)
rows, err := m.Mydb.Query(qstr)
if err != nil {
glog.Errorf("Query db err: %s", err.Error())
return nil, err
}
defer rows.Close()
var knowface FaceInfo
// var id int
var index int64
var num int64
num = 0
index = 0
knowsfaces := make([]FaceInfo, 0)
faceinteface := make([]byte, 255)
for rows.Next() && (numbers == 0 || num < numbers) {
// var faceinteface interface{}
err := rows.Scan(&knowface.Id, &knowface.FaceSetName, &knowface.FaceID, &faceinteface, &knowface.ImageBase64, &knowface.Name, &knowface.Age, &knowface.Address, &knowface.Imageaddress, &knowface.ImageURL, &knowface.CreateTime, &knowface.SimilaryImageURL, &knowface.Similarity)
if err != nil {
glog.Errorf("scan db err: %s", err.Error())
return nil, err
}
index, _ = strconv.ParseInt(knowface.Id, 10, 64)
// glog.Infof("index:%s", index)
if !((start == -1 || (start > 0 && index >= start)) && (end == -1 || (end > 0 && index <= end))) {
continue
}
// glog.Infof("numstart:%s", num)
if faceinteface != nil && len(faceinteface) != 0 {
values := make(map[string]interface{})
err = json.Unmarshal(faceinteface, &values)
if err != nil {
glog.Error(err)
return nil, err
}
// values := faceinteface.(map[string]interface{})
if values != nil {
knowface.Face.Confidence = values["confidence"].(string)
if _, ok := values["boundingBox"]; ok {
secvalue := values["boundingBox"].(map[string]interface{})
if secvalue != nil {
knowface.Face.Bound.Height = strconv.FormatFloat(secvalue["height"].(float64), 'f', 0, 64)
knowface.Face.Bound.Width = strconv.FormatFloat(secvalue["width"].(float64), 'f', 0, 64)
knowface.Face.Bound.TopLeftX = strconv.FormatFloat(secvalue["topLeftX"].(float64), 'f', 0, 64)
knowface.Face.Bound.TopLeftY = strconv.FormatFloat(secvalue["topLeftY"].(float64), 'f', 0, 64)
}
}
}
}
knowsfaces = append(knowsfaces, knowface)
num++
}
return knowsfaces, nil
}
func (m *Manager) listfaces(w http.ResponseWriter, r *http.Request) {
glog.Infof("method: %s", r.Method)
if r.Method == "GET" {
urlpara, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusBadRequest)
}
glog.Infof("url : %#v", urlpara)
// data, err := ioutil.ReadAll(r.Body)
// if err != nil {
// glog.Error(err)
// return
// }
// bdata := make(map[string]interface{})
// // var bdata map[string]interface{}
// err = json.Unmarshal(data, &bdata)
// if err != nil {
// glog.Error(err)
// return
// }
// start, err := strconv.ParseInt(bdata["start"].(string), 10, 32)
// end, err := strconv.ParseInt(bdata["end"].(string), 10, 32)
// number, err := strconv.ParseInt(bdata["numbers"].(string), 10, 32)
// facesetname := bdata["facesetname"].(string)
// timeby := bdata["timeby"].(bool)
// isKnow := bdata["isknown"].(string)
start, err := strconv.ParseInt(urlpara["start"][0], 10, 32)
end, err := strconv.ParseInt(urlpara["end"][0], 10, 32)
number, err := strconv.ParseInt(urlpara["numbers"][0], 10, 32)
facesetname := urlpara["facesetname"][0]
timeby, err := strconv.ParseBool(urlpara["timeby"][0])
isKnow := urlpara["isknown"][0]
glog.Infof("start:%s, end:%s, number:%s", start, end, number)
// start, err := r.URL.Query()["start"]
// start, err := string(r.URL.Query()["start"])
// end, err := strconv.ParseInt(bdata["end"].(string), 10, 32)
// number, err := strconv.ParseInt(bdata["numbers"].(string), 10, 32)
// facesetname := bdata["facesetname"].(string)
// timeby := bdata["timeby"].(bool)
// if err != nil {
// glog.Error(err)
// return
// }
var table string
if isKnow == "0" {
table = "knowfaceinfo"
} else if isKnow == "1" {
table = "unknowfaceinfo"
} else if isKnow == "2" {
table = "facedb"
} else {
glog.Error("unkonw search")
return
}
faces, err := m.getAllfaces(facesetname, table, start, end, number, timeby)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
// glog.Infof("list: %#v", faces)
// glog.Infof("list: %+v", faces)
jsonface, err := json.Marshal(faces)
if err != nil {
glog.Error(err)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", jsonface)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
}
// func (m *Manager) listunknowfaces(w http.ResponseWriter, r *http.Request) {
// glog.Infof("method: %s", r.Method)
// fmt.Fprintf(w, "%v", "")
// }
func (m *Manager) deletefaces(facesetname string, blist []interface{}, deleteimage bool) error {
glog.Infof("%#v", blist)
var id string
var faceid string
var imageurl string
var isknown string
for _, value := range blist {
v, ok := value.(map[string]interface{})
if ok && v != nil {
id = v["Id"].(string)
faceid = v["faceid"].(string)
imageurl = v["imageurl"].(string)
isknown = v["isknown"].(string)
} else {
v2 := value.(map[string]string)
id = v2["Id"]
faceid = v2["faceid"]
imageurl = v2["imageurl"]
isknown = v2["isknown"]
}
// glog.Infof("%s, %s, %s", id, faceid, imageurl)
if isknown == "0" {
// glog.Infof("in delete knowfaceinfo")
_, err := m.Mydb.Exec("delete from knowfaceinfo where id=? and faceid=? and facesetname=?", id, faceid, facesetname)
if err != nil {
glog.Error(err)
return err
}
} else if isknown == "1" {
// glog.Infof("in delete unknowfaceinfo")
_, err := m.Mydb.Exec("delete from unknowfaceinfo where id=? and facesetname=?", id, facesetname)
if err != nil {
glog.Error(err)
return err
}
// if deleteimage {
// imageaddress := m.CustConfig.StaticDir+ "/" +facesetname + strings.Split(imageurl, ":"+m.CustConfig.Port)[1]
// err = os.Remove(imageaddress)
// if err != nil {
// glog.Error("remove err:%s", err)
// }
// }
} else if isknown == "2" {
// delete from faceset
urlStr := m.CustConfig.Aiurl + "/v1/faceSet/" + m.FaceidMap[facesetname] + "/" + faceid
_, err := m.AiCloud.DeleteFace(urlStr, http.MethodDelete)
if err != nil {
glog.Error(err)
return err
}
_, err = m.Mydb.Exec("delete from facedb where id=? and facesetname=?", id, facesetname)
if err != nil {
glog.Error(err)
return err
}
} else {
glog.Warning("unkonw face table to delete")
}
if deleteimage {
glog.Infof("image location:%s", strings.Split(imageurl, ":"+m.CustConfig.Port)[1])
imageaddress := m.CustConfig.StaticDir + strings.Split(imageurl, ":"+m.CustConfig.Port)[1]
err := os.Remove(imageaddress)
if err != nil {
glog.Error("remove err:%s", err)
}
}
}
return nil
}
func (m *Manager) batchdeletefaces(w http.ResponseWriter, r *http.Request) {
glog.Infof("method: %s", r.Method)
if r.Method == "POST" {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
bdata := make(map[string]interface{})
err = json.Unmarshal(data, &bdata)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
blist := bdata["faces"].([]interface{})
facesetname := bdata["facesetname"].(string)
err = m.deletefaces(facesetname, blist, true)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func (m *Manager) updateface(face map[string]interface{}) error {
id := face["Id"].(string)
facesetname := face["facesetname"].(string)
faceid := face["faceid"].(string)
name := face["name"].(string)
age := face["age"].(string)
address := face["address"].(string)
imageurl := face["imageurl"].(string)
isadd := face["isadd"].(bool)
isknown := face["isknown"].(string)
facem, err := json.Marshal(face["face"])
if err != nil {
return err
}
// glog.Infof("face:%#v", facem)
if isknown == "1" && isadd && faceid == "" {
splitstr := ":" + m.CustConfig.Port
imageaddress := m.CustConfig.StaticDir + strings.Split(imageurl, splitstr)[1]
m.insertIntoFacedb(facesetname, imageaddress, imageurl, name, age, address, facem)
blist := make([]interface{}, 0)
fmap := make(map[string]string)
fmap["Id"] = id
fmap["faceid"] = faceid
fmap["imageurl"] = imageurl
fmap["isknown"] = "1"
blist = append(blist, fmap)
m.deletefaces(facesetname, blist, false)
} else {
// glog.Infof("faceid:%s", faceid)
if isknown == "0" {
_, err := m.Mydb.Exec("update knowfaceinfo set name=?, age=?, address=? where id=? and faceid=?", name, age, address, id, faceid)
if err != nil {
return err
}
} else if isknown == "1" {
_, err := m.Mydb.Exec("update unknowfaceinfo set name=?, age=?, address=? where id=?", name, age, address, id)
if err != nil {
return err
}
} else if isknown == "2" {
_, err := m.Mydb.Exec("update facedb set name=?, age=?, address=? where id=?", name, age, address, id)
if err != nil {
return err
}
} else {
glog.Warning("unknow table to update")
}
}
return nil
}
func (m *Manager) faceinforegister(w http.ResponseWriter, r *http.Request) {
glog.Infof("method: %s", r.Method)
if r.Method == "POST" {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
bdata := make(map[string]interface{})
err = json.Unmarshal(data, &bdata)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
return
}
blist := bdata["faceinfo"].(map[string]interface{})
err = m.updateface(blist)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500 %s", err)))
}
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}