/
embedding.go
91 lines (70 loc) Β· 1.94 KB
/
embedding.go
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
package face
import (
"encoding/json"
"fmt"
"strings"
"github.com/photoprism/photoprism/pkg/clusters"
)
// Embedding represents a face embedding.
type Embedding []float64
var NullEmbedding = make(Embedding, 512)
// NewEmbedding creates a new embedding from an inference result.
func NewEmbedding(inference []float32) Embedding {
result := make(Embedding, len(inference))
var v float32
var i int
for i, v = range inference {
result[i] = float64(v)
}
return result
}
// Kind returns the type of face e.g. regular, kids, or ignored.
func (m Embedding) Kind() Kind {
if m.KidsFace() {
return KidsFace
} else if m.Ignored() {
return IgnoredFace
}
return RegularFace
}
// SkipMatching checks if the face embedding seems unsuitable for matching.
func (m Embedding) SkipMatching() bool {
return m.KidsFace() || m.Ignored()
}
// CanMatch tests if the face embedding is not blacklisted.
func (m Embedding) CanMatch() bool {
return !m.Ignored()
}
// Dist calculates the distance to another face embedding.
func (m Embedding) Dist(other Embedding) float64 {
if len(other) == 0 || len(m) != len(other) {
return -1
}
return clusters.EuclideanDist(m, other)
}
// Magnitude returns the face embedding vector length (magnitude).
func (m Embedding) Magnitude() float64 {
return m.Dist(NullEmbedding)
}
// JSON returns the face embedding as JSON bytes.
func (m Embedding) JSON() []byte {
var noResult = []byte("")
if len(m) < 1 {
return noResult
}
if result, err := json.Marshal(m); err != nil {
return noResult
} else {
return result
}
}
// UnmarshalEmbedding parses a single face embedding JSON.
func UnmarshalEmbedding(s string) (result Embedding, err error) {
if s == "" {
return result, fmt.Errorf("cannot unmarshal embedding, empty string provided")
} else if !strings.HasPrefix(s, "[") {
return result, fmt.Errorf("cannot unmarshal embedding, invalid json provided")
}
err = json.Unmarshal([]byte(s), &result)
return result, err
}