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-03 17:25:37 +02:00
|
|
|
// IgnoreFace tests whether the embedding is generally unsuitable for matching.
|
|
|
|
func (m Embedding) IgnoreFace() bool {
|
|
|
|
if IgnoreDist <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return IgnoreEmbeddings.Contains(m, IgnoreDist)
|
2021-09-30 13:44:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
// KidsFace tests if the embedded face belongs to a baby or young child.
|
|
|
|
func (m Embedding) KidsFace() bool {
|
|
|
|
if KidsDist <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return KidsEmbeddings.Contains(m, KidsDist)
|
2021-10-07 09:32:17 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
// OmitMatch tests if the face embedding is unsuitable for matching.
|
|
|
|
func (m Embedding) OmitMatch() bool {
|
|
|
|
return m.KidsFace() || m.IgnoreFace()
|
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 {
|
|
|
|
return !m.IgnoreFace()
|
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 {
|
|
|
|
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
|
|
|
}
|