sunty
2019-07-18 6a413e1d7d485f506e81c669bc692868c29cefb9
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
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
}