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/montanaflynn/stats"
|
|
|
|
"github.com/photoprism/photoprism/pkg/clusters"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Embeddings represents a face embedding cluster.
|
|
|
|
type Embeddings []Embedding
|
|
|
|
|
|
|
|
// NewEmbeddings creates a new embeddings from inference results.
|
|
|
|
func NewEmbeddings(inference [][]float32) Embeddings {
|
|
|
|
result := make(Embeddings, len(inference))
|
|
|
|
|
|
|
|
var v []float32
|
|
|
|
var i int
|
|
|
|
|
|
|
|
for i, v = range inference {
|
|
|
|
e := NewEmbedding(v)
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
if e.CanMatch() {
|
2021-09-30 13:44:23 +02:00
|
|
|
result[i] = e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty tests if embeddings are empty.
|
|
|
|
func (embeddings Embeddings) Empty() bool {
|
|
|
|
if len(embeddings) < 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(embeddings[0]) < 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the number of embeddings.
|
|
|
|
func (embeddings Embeddings) Count() int {
|
|
|
|
if embeddings.Empty() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(embeddings)
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:03:13 +01:00
|
|
|
// Kind returns the type of face e.g. regular, kids, or ignored.
|
|
|
|
func (embeddings Embeddings) Kind() (result Kind) {
|
|
|
|
for _, e := range embeddings {
|
|
|
|
if k := e.Kind(); k > result {
|
|
|
|
result = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-09-30 13:44:23 +02:00
|
|
|
// One tests if there is exactly one embedding.
|
|
|
|
func (embeddings Embeddings) One() bool {
|
|
|
|
return embeddings.Count() == 1
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:32:17 +02:00
|
|
|
// First returns the first face embedding.
|
|
|
|
func (embeddings Embeddings) First() Embedding {
|
|
|
|
if embeddings.Empty() {
|
|
|
|
return NullEmbedding
|
|
|
|
}
|
|
|
|
|
|
|
|
return embeddings[0]
|
|
|
|
}
|
|
|
|
|
2021-09-30 13:44:23 +02:00
|
|
|
// Float64 returns embeddings as a float64 slice.
|
|
|
|
func (embeddings Embeddings) Float64() [][]float64 {
|
|
|
|
result := make([][]float64, len(embeddings))
|
|
|
|
|
|
|
|
for i, e := range embeddings {
|
|
|
|
result[i] = e
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains tests if another embeddings is contained within a radius.
|
|
|
|
func (embeddings Embeddings) Contains(other Embedding, radius float64) bool {
|
|
|
|
for _, e := range embeddings {
|
2022-04-03 17:25:37 +02:00
|
|
|
if d := e.Dist(other); d < radius {
|
2021-09-30 13:44:23 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-03 17:25:37 +02:00
|
|
|
// Dist returns the minimum distance to an embedding.
|
|
|
|
func (embeddings Embeddings) Dist(other Embedding) (dist float64) {
|
2021-09-30 13:44:23 +02:00
|
|
|
dist = -1
|
|
|
|
|
|
|
|
for _, e := range embeddings {
|
2022-04-03 17:25:37 +02:00
|
|
|
if d := e.Dist(other); d < dist || dist < 0 {
|
2021-09-30 13:44:23 +02:00
|
|
|
dist = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dist
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON returns the embeddings as JSON bytes.
|
|
|
|
func (embeddings Embeddings) JSON() []byte {
|
|
|
|
var noResult = []byte("")
|
|
|
|
|
|
|
|
if embeddings.Empty() {
|
|
|
|
return noResult
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, err := json.Marshal(embeddings); err != nil {
|
|
|
|
return noResult
|
|
|
|
} else {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmbeddingsMidpoint returns the embeddings vector midpoint.
|
|
|
|
func EmbeddingsMidpoint(embeddings Embeddings) (result Embedding, radius float64, count int) {
|
|
|
|
// Return if there are no embeddings.
|
|
|
|
if embeddings.Empty() {
|
|
|
|
return Embedding{}, 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count embeddings.
|
|
|
|
count = len(embeddings)
|
|
|
|
|
|
|
|
// Only one embedding?
|
|
|
|
if count == 1 {
|
|
|
|
// Return embedding if there is only one.
|
|
|
|
return embeddings[0], 0.0, 1
|
|
|
|
}
|
|
|
|
|
|
|
|
dim := len(embeddings[0])
|
|
|
|
|
|
|
|
// No embedding values?
|
|
|
|
if dim == 0 {
|
|
|
|
return Embedding{}, 0.0, count
|
|
|
|
}
|
|
|
|
|
|
|
|
result = make(Embedding, dim)
|
|
|
|
|
|
|
|
// The mean of a set of vectors is calculated component-wise.
|
|
|
|
for i := 0; i < dim; i++ {
|
|
|
|
values := make(stats.Float64Data, count)
|
|
|
|
|
|
|
|
for j := 0; j < count; j++ {
|
|
|
|
values[j] = embeddings[j][i]
|
|
|
|
}
|
|
|
|
|
|
|
|
if m, err := stats.Mean(values); err != nil {
|
|
|
|
log.Warnf("embeddings: %s", err)
|
|
|
|
} else {
|
|
|
|
result[i] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Radius is the max embedding distance + 0.01 from result.
|
|
|
|
for _, emb := range embeddings {
|
2022-04-03 17:25:37 +02:00
|
|
|
if d := clusters.EuclideanDist(result, emb); d > radius {
|
2021-09-30 13:44:23 +02:00
|
|
|
radius = d + 0.01
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, radius, count
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalEmbeddings parses face embedding JSON.
|
2022-04-03 17:25:37 +02:00
|
|
|
func UnmarshalEmbeddings(s string) (result Embeddings, err error) {
|
|
|
|
if s == "" {
|
|
|
|
return result, fmt.Errorf("cannot unmarshal empeddings, empty string provided")
|
|
|
|
} else if !strings.HasPrefix(s, "[[") {
|
|
|
|
return result, fmt.Errorf("cannot unmarshal empeddings, 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
|
|
|
}
|