2021-09-30 13:44:23 +02:00
|
|
|
package face
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-04-03 17:25:37 +02:00
|
|
|
"fmt"
|
2021-09-30 13:44:23 +02:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:22:31 +02:00
|
|
|
// 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
|
2022-04-03 17:25:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 21:22:31 +02:00
|
|
|
return RegularFace
|
2021-09-30 13:44:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 21:22:31 +02:00
|
|
|
// SkipMatching checks if the face embedding seems unsuitable for matching.
|
|
|
|
func (m Embedding) SkipMatching() bool {
|
|
|
|
return m.KidsFace() || m.Ignored()
|
2021-10-07 09:32:17 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
// CanMatch tests if the face embedding is not blacklisted.
|
|
|
|
func (m Embedding) CanMatch() bool {
|
2022-04-04 21:22:31 +02:00
|
|
|
return !m.Ignored()
|
2021-09-30 13:44:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
// Dist calculates the distance to another face embedding.
|
|
|
|
func (m Embedding) Dist(other Embedding) float64 {
|
2022-04-04 21:22:31 +02:00
|
|
|
if len(other) == 0 || len(m) != len(other) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
return clusters.EuclideanDist(m, other)
|
2021-09-30 13:44:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
// Magnitude returns the face embedding vector length (magnitude).
|
|
|
|
func (m Embedding) Magnitude() float64 {
|
|
|
|
return m.Dist(NullEmbedding)
|
2021-09-30 13:44:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 09:32:17 +02:00
|
|
|
// JSON returns the face embedding as JSON bytes.
|
2021-09-30 13:44:23 +02:00
|
|
|
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.
|
2022-04-03 17:25:37 +02:00
|
|
|
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")
|
2021-09-30 13:44:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
err = json.Unmarshal([]byte(s), &result)
|
2021-09-30 13:44:23 +02:00
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
return result, err
|
2021-09-30 13:44:23 +02:00
|
|
|
}
|