zhangzengfei
2024-03-30 7e7a2baab4c5ccb9dd8dc5104041c9efe718ed9a
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
package compare
 
import (
    "encoding/base64"
    "fmt"
    "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
    }
 
    //指定最低分
    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, args.FaceFeature, baseScore)
                if len(targets) > 0 {
                    scResult.CompareResult = append(scResult.CompareResult, targets...)
                }
 
                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, args.FaceFeature, baseScore)
        if len(targets) > 0 {
            scResult.CompareResult = append(scResult.CompareResult, targets...)
            // todo 添加小区外的关联关系, 下次优先比对
        }
    }
 
done:
    logger.Debugf("比对结果 %d条", len(scResult.CompareResult))
    buf, err := proto.Marshal(&scResult)
    if err != nil {
        logger.Error("scResult Marshal error!", err)
        return nil
    }
 
    return buf
}
 
func DoSdkCompare(ci []byte, co string) float32 {
    co_d, err := base64.StdEncoding.DecodeString(co)
    if err != nil {
        logger.Error("DoSdkCompare err:", err)
        return -1
    }
    sec := DecCompare(ci, co_d)
    //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)
}