2021-09-30 13:44:23 +02:00
|
|
|
package face
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:32:17 +02:00
|
|
|
// Blacklisted tests if the face embedding is blacklisted.
|
2021-09-30 13:44:23 +02:00
|
|
|
func (m Embedding) Blacklisted() bool {
|
|
|
|
return Blacklist.Contains(m, BlacklistRadius)
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:32:17 +02:00
|
|
|
// Child tests if the face embedding belongs to a child.
|
|
|
|
func (m Embedding) Child() bool {
|
|
|
|
return Children.Contains(m, ChildrenRadius)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsuitable tests if the face embedding is unsuitable for clustering and matching.
|
|
|
|
func (m Embedding) Unsuitable() bool {
|
|
|
|
return m.Child() || m.Blacklisted()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Distance calculates the distance to another face embedding.
|
2021-09-30 13:44:23 +02:00
|
|
|
func (m Embedding) Distance(other Embedding) float64 {
|
|
|
|
return clusters.EuclideanDistance(m, other)
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:32:17 +02:00
|
|
|
// Magnitude returns the face embedding vector length (magnitude).
|
2021-09-30 13:44:23 +02:00
|
|
|
func (m Embedding) Magnitude() float64 {
|
|
|
|
return m.Distance(NullEmbedding)
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:32:17 +02:00
|
|
|
// NotBlacklisted tests if the face embedding is not blacklisted.
|
2021-09-30 13:44:23 +02:00
|
|
|
func (m Embedding) NotBlacklisted() bool {
|
|
|
|
return !m.Blacklisted()
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func UnmarshalEmbedding(s string) (result Embedding) {
|
|
|
|
if !strings.HasPrefix(s, "[") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(s), &result); err != nil {
|
|
|
|
log.Errorf("faces: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|