package cache
|
|
import (
|
"flag"
|
"fmt"
|
"time"
|
"sync"
|
"encoding/base64"
|
|
"encoding/json"
|
|
"basic.com/pubsub/cache.git/esutil"
|
"basic.com/pubsub/cache.git/shardmap"
|
|
"basic.com/valib/gosdk.git"
|
)
|
|
var querynum = flag.Int("querynum", 10, "the query number from database")
|
var threadnum = flag.Int("threadnum",32, "the number of thread to deal data.")
|
|
type CmapItem struct {
|
sync.Mutex
|
Cam map[string]*shardmap.ShardMap
|
}
|
|
var Cmap *CmapItem
|
|
func Init(indexName string, serverIp string, serverPort string, analyServerId string){
|
flag.Parse()
|
gosdk.InitFaceExtractor(16, 0)
|
|
Cmap = &CmapItem{
|
Cam: make(map[string]*shardmap.ShardMap),
|
}
|
|
temptime := time.Now()
|
var wg sync.WaitGroup
|
|
for i:=0; i<*threadnum; i++ {
|
j := i*(*querynum)
|
wg.Add(1)
|
go func(qs int){
|
defer wg.Done()
|
escache, err := esutil.Personinfos(qs, *querynum, indexName, serverIp, serverPort, analyServerId)
|
if err != nil {
|
fmt.Println(err)
|
return
|
}
|
|
Cmap.Lock()
|
|
var tableidstring string
|
for _, value := range escache{
|
|
// 如果没有tableid 则 tableidstring = capturetable
|
if value.Tableid == ""{
|
tableidstring="capturetable"
|
}else{
|
tableidstring = value.Tableid
|
}
|
|
if _, ok :=Cmap.Cam[tableidstring]; !ok {
|
Cmap.Cam[tableidstring]=shardmap.New(uint8(*threadnum))
|
}
|
|
Cmap.Cam[tableidstring].Set(value.Id,value.FaceFeature)
|
}
|
|
Cmap.Unlock()
|
|
}(j)
|
}
|
wg.Wait()
|
fmt.Println("time of get data from es.", time.Since(temptime))
|
fmt.Println()
|
}
|
|
func GetComparePersonBaseInfo(tableid []string, faceFeature []byte, compareThreshold float32) []byte {
|
|
totalmap := make(map[string]float32)
|
|
if faceFeature == nil {
|
return nil
|
}
|
|
if tableid == nil {
|
//对比全部
|
for _, val := range Cmap.Cam {
|
tmpmap := val.Walk(Printest, faceFeature)
|
for key, sec := range tmpmap {
|
if sec > 50*0.01 {
|
fmt.Println("map为",totalmap[key],sec)
|
totalmap[key] = sec
|
}
|
}
|
}
|
} else {
|
for _, tid := range tableid {
|
shardins, ok := Cmap.Cam[tid]
|
fmt.Println(ok)
|
if !ok {
|
fmt.Println("get shad error by id", shardins)
|
continue
|
}
|
tmpmap := shardins.Walk(Printest, faceFeature)
|
for key, sec := range tmpmap {
|
if compareThreshold > 50*0.01{
|
if sec > compareThreshold {
|
fmt.Println("map为",totalmap[key],sec)
|
totalmap[key] = sec
|
}
|
}else {
|
if sec > 50*0.01 {
|
fmt.Println("map为",totalmap[key],sec)
|
totalmap[key] = sec
|
}
|
}
|
|
}
|
|
}
|
}
|
|
fmt.Println(totalmap)
|
|
buf, err := json.Marshal(totalmap)
|
if err != nil {
|
fmt.Println("map to json error!", err)
|
return nil
|
}
|
return buf
|
}
|
|
func Printest(ci []byte, co string ) (float32){
|
|
co_d, err := base64.StdEncoding.DecodeString(co)
|
if err != nil {
|
fmt.Println("co_d : error : ", err)
|
return -1
|
}
|
|
sec := gosdk.FaceCompare(ci, co_d)
|
fmt.Println("比对得分为:",sec)
|
return sec
|
}
|