2021-08-12 04:54:20 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2021-08-12 12:05:10 +02:00
|
|
|
"encoding/base32"
|
2021-08-15 20:57:26 +02:00
|
|
|
"encoding/json"
|
2021-08-16 00:29:36 +02:00
|
|
|
"sync"
|
2021-08-12 04:54:20 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
var faceMutex = sync.Mutex{}
|
|
|
|
|
2021-08-15 20:57:26 +02:00
|
|
|
// Faces represents a Face slice.
|
2021-08-13 20:31:41 +02:00
|
|
|
type Faces []Face
|
2021-08-12 04:54:20 +02:00
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
// Face represents the face of a Subject.
|
2021-08-13 20:31:41 +02:00
|
|
|
type Face struct {
|
2021-08-16 01:45:36 +02:00
|
|
|
ID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"ID" yaml:"ID"`
|
|
|
|
FaceSrc string `gorm:"type:VARBINARY(8);" json:"Src" yaml:"Src,omitempty"`
|
|
|
|
SubjectUID string `gorm:"type:VARBINARY(42);index;" json:"SubjectUID" yaml:"SubjectUID,omitempty"`
|
|
|
|
Collisions int `json:"Collisions" yaml:"Collisions,omitempty"`
|
|
|
|
Samples int `json:"Samples" yaml:"Samples,omitempty"`
|
|
|
|
Radius float64 `json:"Radius" yaml:"Radius,omitempty"`
|
|
|
|
EmbeddingJSON json.RawMessage `gorm:"type:MEDIUMBLOB;" json:"-" yaml:"EmbeddingJSON,omitempty"`
|
|
|
|
CreatedAt time.Time `json:"CreatedAt" yaml:"CreatedAt,omitempty"`
|
|
|
|
UpdatedAt time.Time `json:"UpdatedAt" yaml:"UpdatedAt,omitempty"`
|
|
|
|
embedding Embedding `gorm:"-"`
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 18:13:03 +02:00
|
|
|
// UnknownFace can be used as a placeholder for unknown faces.
|
|
|
|
var UnknownFace = Face{
|
2021-08-15 20:57:26 +02:00
|
|
|
ID: "zz",
|
|
|
|
FaceSrc: SrcDefault,
|
2021-08-16 00:29:36 +02:00
|
|
|
SubjectUID: UnknownPerson.SubjectUID,
|
2021-08-15 20:57:26 +02:00
|
|
|
EmbeddingJSON: []byte{},
|
2021-08-14 18:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUnknownFace initializes the database with a placeholder for unknown faces.
|
|
|
|
func CreateUnknownFace() {
|
|
|
|
_ = UnknownFace.Create()
|
|
|
|
}
|
|
|
|
|
2021-08-12 04:54:20 +02:00
|
|
|
// TableName returns the entity database table name.
|
2021-08-13 20:31:41 +02:00
|
|
|
func (Face) TableName() string {
|
2021-08-16 00:29:36 +02:00
|
|
|
return "faces_dev3"
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 15:45:51 +02:00
|
|
|
// NewFace returns a new face.
|
2021-08-16 00:29:36 +02:00
|
|
|
func NewFace(subjectUID string, embeddings Embeddings) *Face {
|
2021-08-13 20:31:41 +02:00
|
|
|
result := &Face{
|
2021-08-16 00:29:36 +02:00
|
|
|
SubjectUID: subjectUID,
|
2021-08-15 20:57:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := result.SetEmbeddings(embeddings); err != nil {
|
|
|
|
log.Errorf("face: failed setting embeddings (%s)", err)
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-08-15 20:57:26 +02:00
|
|
|
// SetEmbeddings assigns face embeddings.
|
|
|
|
func (m *Face) SetEmbeddings(embeddings Embeddings) (err error) {
|
|
|
|
m.embedding, m.Radius, m.Samples = EmbeddingsMidpoint(embeddings)
|
|
|
|
m.EmbeddingJSON, err = json.Marshal(m.embedding)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := sha1.Sum(m.EmbeddingJSON)
|
|
|
|
m.ID = base32.StdEncoding.EncodeToString(s[:])
|
|
|
|
m.UpdatedAt = Timestamp()
|
|
|
|
|
|
|
|
if m.CreatedAt.IsZero() {
|
|
|
|
m.CreatedAt = m.UpdatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Embedding returns parsed face embedding.
|
|
|
|
func (m *Face) Embedding() Embedding {
|
|
|
|
if len(m.EmbeddingJSON) == 0 {
|
|
|
|
return Embedding{}
|
|
|
|
} else if len(m.embedding) > 0 {
|
|
|
|
return m.embedding
|
|
|
|
} else if err := json.Unmarshal(m.EmbeddingJSON, &m.embedding); err != nil {
|
|
|
|
log.Errorf("failed parsing face embedding json: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.embedding
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates the existing or inserts a new face.
|
2021-08-13 20:31:41 +02:00
|
|
|
func (m *Face) Save() error {
|
2021-08-16 00:29:36 +02:00
|
|
|
faceMutex.Lock()
|
|
|
|
defer faceMutex.Unlock()
|
2021-08-12 04:54:20 +02:00
|
|
|
|
2021-08-12 12:05:10 +02:00
|
|
|
return Save(m, "ID")
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts the face to the database.
|
2021-08-13 20:31:41 +02:00
|
|
|
func (m *Face) Create() error {
|
2021-08-16 00:29:36 +02:00
|
|
|
faceMutex.Lock()
|
|
|
|
defer faceMutex.Unlock()
|
2021-08-12 04:54:20 +02:00
|
|
|
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the face from the database.
|
2021-08-13 20:31:41 +02:00
|
|
|
func (m *Face) Delete() error {
|
2021-08-12 04:54:20 +02:00
|
|
|
return Db().Delete(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update a face property in the database.
|
2021-08-13 20:31:41 +02:00
|
|
|
func (m *Face) Update(attr string, value interface{}) error {
|
2021-08-12 12:05:10 +02:00
|
|
|
return UnscopedDb().Model(m).Update(attr, value).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates face properties in the database.
|
2021-08-13 20:31:41 +02:00
|
|
|
func (m *Face) Updates(values interface{}) error {
|
2021-08-12 12:05:10 +02:00
|
|
|
return UnscopedDb().Model(m).Updates(values).Error
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|