zhangzengfei
2024-05-07 5572a775f44cefed8c8c14da29ecef11c5d3593e
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
package compare
 
import (
    "fmt"
    "sdkCompare/util"
    "strconv"
 
    "sdkCompare/cache"
 
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/logger.git"
    "github.com/golang/protobuf/proto"
)
 
const thresholdLimit = float32(30)
 
func GetComparePersonBaseInfo(args protomsg.CompareArgs) []byte {
    if args.FaceFeature == nil {
        return nil
    }
 
    floatFeat := util.ByteSlice2float32Slice(args.FaceFeature)
 
    //指定最低分
    baseScore := thresholdLimit
    if args.CompareThreshold > thresholdLimit {
        baseScore = args.CompareThreshold
    }
 
    if args.IsCompareAll {
        baseScore = 0
    }
 
    var scResult protomsg.SdkCompareResult
 
    var walkedArea = make(map[string]struct{}, 0)
    // 优先比对传入的小区id
    if args.TreeNodes != nil && len(args.TreeNodes) > 0 {
        for _, id := range args.TreeNodes {
            if _, ok := cache.CacheMap.Area[id]; ok {
                targets := cache.CacheMap.Area[id].Walk(DoSdkCompare, floatFeat, baseScore)
                if len(targets) > 0 {
                    // 比对结果去重, 同一个人到访过多个小区, 缓存数据内会有多条记录
                    for idx, t := range targets {
                        var isRepeat bool
                        for _, r := range scResult.CompareResult {
                            if t.Id == r.Id {
                                isRepeat = true
                                break
                            }
                        }
                        if !isRepeat {
                            scResult.CompareResult = append(scResult.CompareResult, targets[idx])
                        }
                    }
                }
 
                walkedArea[id] = struct{}{}
            }
        }
 
        if len(scResult.CompareResult) > 0 {
            goto done
        }
    }
 
    // 比对以外的小区
    if !args.IsCompareAll && len(args.TreeNodes) > 0 {
        baseScore += 20
    }
 
    for key, val := range cache.CacheMap.Area {
        if _, ok := walkedArea[key]; ok {
            continue
        }
 
        targets := val.Walk(DoSdkCompare, floatFeat, baseScore)
        if len(targets) > 0 {
            if len(targets) > 0 {
                // 比对结果去重, 同一个人到访过多个小区, 缓存数据内会有多条记录
                for idx, t := range targets {
                    var isRepeat bool
                    for _, r := range scResult.CompareResult {
                        if t.Id == r.Id {
                            isRepeat = true
                            break
                        }
                    }
                    if !isRepeat {
                        scResult.CompareResult = append(scResult.CompareResult, targets[idx])
                    }
                }
            }
            // todo 添加小区外的关联关系, 下次优先比对
        }
    }
 
done:
    logger.Debugf("比对结果 %d条", len(scResult.CompareResult))
    if len(scResult.CompareResult) > 0 {
        logger.Debugf("比对结果%+v", scResult.CompareResult)
    }
 
    buf, err := proto.Marshal(&scResult)
    if err != nil {
        logger.Error("scResult Marshal error!", err)
        return nil
    }
 
    return buf
}
 
func DoSdkCompare(ci, co []float32) float32 {
    sec := DirectCompare(ci, co)
    //logger.Debug("比对得分为:", sec)
 
    sec = ParseScore(sec)
    return sec
}
 
func ParseScore(compareScore float32) float32 {
    if compareScore <= 1 {
        compareScore = compareScore * 100
    }
    if compareScore == 100 {
        return 100
    }
    f, _ := strconv.ParseFloat(fmt.Sprintf("%2.2f", compareScore), 32)
 
    return float32(f)
}