2021-08-12 04:54:20 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
2021-08-15 20:57:26 +02:00
|
|
|
|
|
|
|
"github.com/mpraski/clusters"
|
2021-08-12 04:54:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Embedding = []float64
|
|
|
|
|
2021-08-15 20:57:26 +02:00
|
|
|
// Embeddings represents an embedding cluster.
|
2021-08-12 04:54:20 +02:00
|
|
|
type Embeddings = []Embedding
|
|
|
|
|
|
|
|
// EmbeddingsMidpoint returns the embeddings vector midpoint.
|
2021-08-15 20:57:26 +02:00
|
|
|
func EmbeddingsMidpoint(m Embeddings) (result Embedding, radius float64, count int) {
|
|
|
|
count = len(m)
|
|
|
|
|
2021-08-12 04:54:20 +02:00
|
|
|
for i, emb := range m {
|
|
|
|
if i == 0 {
|
|
|
|
result = emb
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for j, val := range result {
|
|
|
|
result[j] = (val + emb[j]) / 2
|
|
|
|
}
|
2021-08-15 20:57:26 +02:00
|
|
|
|
|
|
|
if d := clusters.EuclideanDistance(result, emb); d > radius {
|
|
|
|
radius = d
|
|
|
|
}
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-15 20:57:26 +02:00
|
|
|
return result, radius, count
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalEmbeddings parses face embedding JSON.
|
|
|
|
func UnmarshalEmbeddings(s string) (result Embeddings) {
|
|
|
|
if !strings.HasPrefix(s, "[[") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(s), &result); err != nil {
|
|
|
|
log.Errorf("faces: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|