zhangzengfei
2024-09-26 88b1f1d1d14a8fe9e3dde2f363a89d821fc0e641
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
package compare
 
import (
    "sdkCompare/util"
)
 
// #if FEATURE_NORMALIZE
// float CosineDistance(const BYTE* fea1, const BYTE* fea2, int length)
// {
//     int i;
//     const float* jfea1 = (const float*)fea1;
//     const float* jfea2 = (const float*)fea2;
 
//     float score = 0.0f;
//     for (i = 0; i < length; i++)
//     {
//         score += jfea1[i] * jfea2[i];
//     }
 
//     return score;
// }
// #else
// float CosineDistance(const BYTE* fea1, const BYTE* fea2, int length)
// {
//     int i;
//     const float* jfea1 = (const float*)fea1;
//     const float* jfea2 = (const float*)fea2;
//     double normTemp1 = 0.0;
//     double normTemp2 = 0.0;
//     double normTemp = 0.0;
 
//     double score = 0.0f;
//     for (i = 0; i < length; i++) {
//         score += jfea1[i] * jfea2[i];
//         normTemp1 += jfea1[i] * jfea1[i];
//         normTemp2 += jfea2[i] * jfea2[i];
//     }
//     normTemp = sqrt(normTemp1)*sqrt(normTemp2);
 
//     score = score / normTemp;
 
//     return score;
// }
// #endif
 
// int feaDim = FEATURE_RAW_SIZE / 4;
 
// THFEATURE_API float EF_Compare(BYTE* pFeature1,BYTE* pFeature2)
// {
//     if(pFeature1==NULL||pFeature2==NULL) return 0.0f;
 
//     float fscore;
 
//     BYTE* pFea1=pFeature1;
//     BYTE* pFea2=pFeature2;
 
//     fscore = CosineDistance(pFea1, pFea2, feaDim);
//     fscore+=0.05f;
 
//     if(fscore>0.9999f) fscore=0.9999f;
//     if(fscore<0.0001f) fscore=0.0001f;
 
//        return  fscore;
//    }
func DirectCompare(feat1 []float32, feat2 []float32) float32 {
    if len(feat1) != len(feat2) {
        return 0
    }
 
    var score float32
    for i := 0; i < 1536; i++ {
        score += feat1[i] * feat2[i]
    }
    score += 0.05
    if score > 0.9999 {
        score = 0.9999
    }
    if score < 0.0001 {
        score = 0.0001
    }
 
    //fmt.Println("score:", score)
    return score
}
 
func DecCompare(feat1, feat2 []byte) float32 {
    ffeat1 := util.ByteSlice2float32Slice(feat1)
    ffeat2 := util.ByteSlice2float32Slice(feat2)
    if len(feat1) != len(feat2) {
        return 0
    }
    //fmt.Println("len:", len(ffeat1), len(feat2))
    //fmt.Println("ffeat1:", ffeat1, "ffeat2:", ffeat2, "len:", len(ffeat1), len(feat2))
    // normalize
    var score float32
    for i := 0; i < 1536; i++ {
        score += ffeat1[i] * ffeat2[i]
    }
    score += 0.05
    if score > 0.9999 {
        score = 0.9999
    }
    if score < 0.0001 {
        score = 0.0001
    }
    //fmt.Println("score:", score)
    return score
}