2021-05-25 11:38:04 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
2021-08-15 20:57:26 +02:00
|
|
|
"encoding/json"
|
2021-05-26 14:41:59 +02:00
|
|
|
"fmt"
|
2021-05-25 11:38:04 +02:00
|
|
|
"time"
|
2021-05-31 15:40:52 +02:00
|
|
|
|
2021-06-02 17:25:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
"github.com/ulule/deepcopier"
|
|
|
|
|
2021-05-31 15:40:52 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/face"
|
2021-05-25 11:38:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MarkerUnknown = ""
|
2021-08-16 00:29:36 +02:00
|
|
|
MarkerFace = "face"
|
|
|
|
MarkerLabel = "label"
|
2021-05-25 11:38:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Marker represents an image marker point.
|
|
|
|
type Marker struct {
|
2021-08-16 01:45:36 +02:00
|
|
|
ID uint `gorm:"primary_key" json:"ID" yaml:"-"`
|
|
|
|
FileID uint `gorm:"index;" json:"-" yaml:"-"`
|
2021-08-22 16:14:34 +02:00
|
|
|
MarkerType string `gorm:"type:VARBINARY(8);default:'';" json:"Type" yaml:"Type"`
|
2021-08-16 01:45:36 +02:00
|
|
|
MarkerSrc string `gorm:"type:VARBINARY(8);default:'';" json:"Src" yaml:"Src,omitempty"`
|
|
|
|
MarkerName string `gorm:"type:VARCHAR(255);" json:"Name" yaml:"Name,omitempty"`
|
2021-08-22 16:14:34 +02:00
|
|
|
SubjectUID string `gorm:"type:VARBINARY(42);index;" json:"SubjectUID" yaml:"SubjectUID,omitempty"`
|
2021-08-16 01:45:36 +02:00
|
|
|
SubjectSrc string `gorm:"type:VARBINARY(8);default:'';" json:"SubjectSrc" yaml:"SubjectSrc,omitempty"`
|
|
|
|
Subject *Subject `gorm:"foreignkey:SubjectUID;association_foreignkey:SubjectUID;association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"Subject,omitempty" yaml:"-"`
|
|
|
|
FaceID string `gorm:"type:VARBINARY(42);index;" json:"FaceID" yaml:"FaceID,omitempty"`
|
2021-08-19 23:12:51 +02:00
|
|
|
Face *Face `gorm:"foreignkey:FaceID;association_foreignkey:ID;association_autoupdate:false;association_autocreate:false;association_save_reference:false" json:"-" yaml:"-"`
|
2021-08-16 01:45:36 +02:00
|
|
|
EmbeddingsJSON json.RawMessage `gorm:"type:MEDIUMBLOB;" json:"-" yaml:"EmbeddingsJSON,omitempty"`
|
|
|
|
embeddings Embeddings `gorm:"-"`
|
|
|
|
LandmarksJSON json.RawMessage `gorm:"type:MEDIUMBLOB;" json:"-" yaml:"LandmarksJSON,omitempty"`
|
|
|
|
X float32 `gorm:"type:FLOAT;" json:"X" yaml:"X,omitempty"`
|
|
|
|
Y float32 `gorm:"type:FLOAT;" json:"Y" yaml:"Y,omitempty"`
|
|
|
|
W float32 `gorm:"type:FLOAT;" json:"W" yaml:"W,omitempty"`
|
|
|
|
H float32 `gorm:"type:FLOAT;" json:"H" yaml:"H,omitempty"`
|
|
|
|
Score int `gorm:"type:SMALLINT" json:"Score" yaml:"Score,omitempty"`
|
|
|
|
MarkerInvalid bool `json:"Invalid" yaml:"Invalid,omitempty"`
|
2021-08-15 20:57:26 +02:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
2021-05-25 11:38:04 +02:00
|
|
|
}
|
|
|
|
|
2021-06-02 17:25:04 +02:00
|
|
|
// UnknownMarker can be used as a default for unknown markers.
|
2021-08-15 20:57:26 +02:00
|
|
|
var UnknownMarker = NewMarker(0, "", SrcDefault, MarkerUnknown, 0, 0, 0, 0)
|
2021-06-02 17:25:04 +02:00
|
|
|
|
2021-05-25 11:38:04 +02:00
|
|
|
// TableName returns the entity database table name.
|
|
|
|
func (Marker) TableName() string {
|
2021-08-16 00:29:36 +02:00
|
|
|
return "markers_dev3"
|
2021-05-25 11:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMarker creates a new entity.
|
2021-08-16 01:45:36 +02:00
|
|
|
func NewMarker(fileID uint, subjectUID, markerSrc, markerType string, x, y, w, h float32) *Marker {
|
2021-05-26 14:41:59 +02:00
|
|
|
m := &Marker{
|
2021-08-16 01:45:36 +02:00
|
|
|
FileID: fileID,
|
|
|
|
SubjectUID: subjectUID,
|
2021-05-25 11:38:04 +02:00
|
|
|
MarkerSrc: markerSrc,
|
|
|
|
MarkerType: markerType,
|
2021-05-26 14:41:59 +02:00
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
W: w,
|
|
|
|
H: h,
|
2021-05-25 11:38:04 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 14:41:59 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFaceMarker creates a new entity.
|
2021-05-31 15:40:52 +02:00
|
|
|
func NewFaceMarker(f face.Face, fileID uint, refUID string) *Marker {
|
2021-06-01 17:39:03 +02:00
|
|
|
pos := f.Marker()
|
|
|
|
|
|
|
|
m := NewMarker(fileID, refUID, SrcImage, MarkerFace, pos.X, pos.Y, pos.W, pos.H)
|
2021-05-26 14:41:59 +02:00
|
|
|
|
2021-08-15 20:57:26 +02:00
|
|
|
m.EmbeddingsJSON = f.EmbeddingsJSON()
|
2021-08-16 01:45:36 +02:00
|
|
|
m.LandmarksJSON = f.RelativeLandmarksJSON()
|
|
|
|
m.Score = f.Score
|
2021-05-26 14:41:59 +02:00
|
|
|
|
|
|
|
return m
|
2021-05-25 11:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates multiple columns in the database.
|
|
|
|
func (m *Marker) Updates(values interface{}) error {
|
2021-08-12 12:05:10 +02:00
|
|
|
return UnscopedDb().Model(m).Updates(values).Error
|
2021-05-25 11:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates a column in the database.
|
|
|
|
func (m *Marker) Update(attr string, value interface{}) error {
|
2021-08-12 12:05:10 +02:00
|
|
|
return UnscopedDb().Model(m).Update(attr, value).Error
|
2021-05-25 11:38:04 +02:00
|
|
|
}
|
|
|
|
|
2021-06-02 17:25:04 +02:00
|
|
|
// SaveForm updates the entity using form data and stores it in the database.
|
|
|
|
func (m *Marker) SaveForm(f form.Marker) error {
|
|
|
|
if err := deepcopier.Copy(m).From(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:29:36 +02:00
|
|
|
if f.MarkerName != "" {
|
|
|
|
m.MarkerName = txt.Title(txt.Clip(f.MarkerName, txt.ClipKeyword))
|
2021-06-02 17:25:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
if err := m.SyncSubject(true); err != nil {
|
2021-06-02 17:25:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
return m.Save()
|
|
|
|
}
|
2021-08-14 21:37:57 +02:00
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
// SetFace sets a new face for this marker.
|
|
|
|
func (m *Marker) SetFace(f *Face) (updated bool, err error) {
|
|
|
|
if f == nil {
|
|
|
|
return false, fmt.Errorf("face is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.MarkerType != MarkerFace {
|
|
|
|
return false, fmt.Errorf("not a face marker")
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
// Any reason we don't want to set a new face for this marker?
|
|
|
|
if m.SubjectSrc != SrcManual || f.SubjectUID == m.SubjectUID {
|
|
|
|
// Don't skip if subject wasn't set manually, or subjects match.
|
|
|
|
} else if f.SubjectUID != "" {
|
|
|
|
log.Debugf("faces: ambiguous subjects %s / %s for marker %d", txt.Quote(f.SubjectUID), txt.Quote(m.SubjectUID), m.ID)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update face with known subject from marker?
|
2021-08-19 21:12:38 +02:00
|
|
|
if f.SubjectUID != "" || m.SubjectUID == "" {
|
2021-08-22 16:14:34 +02:00
|
|
|
// Don't update if face has a known subject, or marker subject is unknown.
|
2021-08-19 21:12:38 +02:00
|
|
|
} else if err := f.Update("SubjectUID", m.SubjectUID); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
// Skip update if the same face is already set.
|
|
|
|
if m.SubjectUID == f.SubjectUID && m.FaceID == f.ID {
|
2021-08-19 21:12:38 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
// Remember current values for comparison.
|
2021-08-19 21:12:38 +02:00
|
|
|
faceID := m.FaceID
|
|
|
|
subjectUID := m.SubjectUID
|
|
|
|
SubjectSrc := m.SubjectSrc
|
|
|
|
|
|
|
|
m.FaceID = f.ID
|
|
|
|
|
|
|
|
if f.SubjectUID != "" {
|
|
|
|
m.SubjectUID = f.SubjectUID
|
|
|
|
m.SubjectSrc = SrcAuto
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.SyncSubject(false); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update face subject?
|
|
|
|
if m.SubjectUID == "" || f.SubjectUID != m.SubjectUID {
|
|
|
|
// Not needed.
|
|
|
|
} else if err := f.Update("SubjectUID", m.SubjectUID); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update database only if anything has changed.
|
|
|
|
if m.FaceID != faceID || m.SubjectUID != subjectUID || m.SubjectSrc != SubjectSrc {
|
|
|
|
return true, m.Updates(Values{"FaceID": m.FaceID, "SubjectUID": m.SubjectUID, "SubjectSrc": m.SubjectSrc})
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
2021-06-02 17:25:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
// SyncSubject maintains the marker subject relationship.
|
|
|
|
func (m *Marker) SyncSubject(updateRelated bool) error {
|
|
|
|
// Face marker? If not, return.
|
|
|
|
if m.MarkerType != MarkerFace {
|
2021-08-16 00:29:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
subj := m.GetSubject()
|
2021-08-16 00:29:36 +02:00
|
|
|
|
|
|
|
if subj == nil {
|
2021-08-19 21:12:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update subject with marker name?
|
|
|
|
if m.MarkerName == "" || subj.SubjectName == m.MarkerName {
|
|
|
|
// Do nothing.
|
|
|
|
} else if err := subj.UpdateName(m.MarkerName); err != nil {
|
|
|
|
return err
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
// Create known face for subject?
|
|
|
|
if m.FaceID != "" || m.SubjectSrc != SrcManual {
|
|
|
|
// Do nothing.
|
2021-08-19 23:12:51 +02:00
|
|
|
} else if f := m.GetFace(); f != nil {
|
2021-08-19 21:12:38 +02:00
|
|
|
m.FaceID = f.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update related markers?
|
|
|
|
if m.FaceID == "" || m.SubjectUID == "" {
|
|
|
|
// Do nothing.
|
|
|
|
} else if err := Db().Model(&Face{}).Where("id = ? AND subject_uid = ''", m.FaceID).Update("SubjectUID", m.SubjectUID).Error; err != nil {
|
|
|
|
return fmt.Errorf("%s (update known face)", err)
|
|
|
|
} else if !updateRelated {
|
|
|
|
return nil
|
|
|
|
} else if err := Db().Model(&Marker{}).
|
|
|
|
Where("id <> ?", m.ID).
|
|
|
|
Where("face_id = ?", m.FaceID).
|
|
|
|
Where("subject_src = ?", SrcAuto).
|
|
|
|
Where("subject_uid <> ?", m.SubjectUID).
|
|
|
|
Updates(Values{"SubjectUID": m.SubjectUID, "SubjectSrc": SrcAuto}).Error; err != nil {
|
|
|
|
return fmt.Errorf("%s (update related markers)", err)
|
|
|
|
} else {
|
2021-08-19 23:12:51 +02:00
|
|
|
log.Debugf("marker: matched subject %s with face %s", subj.SubjectName, m.FaceID)
|
2021-08-19 21:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-08-16 00:29:36 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 11:38:04 +02:00
|
|
|
// Save updates the existing or inserts a new row.
|
|
|
|
func (m *Marker) Save() error {
|
2021-05-26 14:41:59 +02:00
|
|
|
if m.X == 0 || m.Y == 0 || m.X > 1 || m.Y > 1 || m.X < -1 || m.Y < -1 {
|
|
|
|
return fmt.Errorf("marker: invalid position")
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:38:04 +02:00
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Marker) Create() error {
|
2021-05-26 14:41:59 +02:00
|
|
|
if m.X == 0 || m.Y == 0 || m.X > 1 || m.Y > 1 || m.X < -1 || m.Y < -1 {
|
|
|
|
return fmt.Errorf("marker: invalid position")
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:38:04 +02:00
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
2021-08-15 20:57:26 +02:00
|
|
|
// Embeddings returns parsed marker embeddings.
|
|
|
|
func (m *Marker) Embeddings() Embeddings {
|
|
|
|
if len(m.EmbeddingsJSON) == 0 {
|
|
|
|
return Embeddings{}
|
|
|
|
} else if len(m.embeddings) > 0 {
|
|
|
|
return m.embeddings
|
|
|
|
} else if err := json.Unmarshal(m.EmbeddingsJSON, &m.embeddings); err != nil {
|
|
|
|
log.Errorf("failed parsing marker embeddings json: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.embeddings
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 23:12:51 +02:00
|
|
|
// GetSubject returns a subject entity if possible.
|
2021-08-19 21:12:38 +02:00
|
|
|
func (m *Marker) GetSubject() (subj *Subject) {
|
|
|
|
if m.Subject != nil {
|
|
|
|
return m.Subject
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.SubjectUID == "" && m.MarkerName != "" {
|
|
|
|
if subj = NewSubject(m.MarkerName, SubjectPerson, SrcMarker); subj == nil {
|
|
|
|
return nil
|
|
|
|
} else if subj = FirstOrCreateSubject(subj); subj == nil {
|
|
|
|
log.Debugf("marker: invalid subject %s", txt.Quote(m.MarkerName))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.SubjectUID = subj.SubjectUID
|
|
|
|
m.SubjectSrc = SrcManual
|
|
|
|
|
|
|
|
return subj
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Subject = FindSubject(m.SubjectUID)
|
|
|
|
|
|
|
|
return m.Subject
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:36:00 +02:00
|
|
|
// ClearSubject removes an existing subject association, and reports a collision.
|
2021-08-22 16:14:34 +02:00
|
|
|
func (m *Marker) ClearSubject(src string) error {
|
2021-08-21 16:36:00 +02:00
|
|
|
if m.Face == nil {
|
|
|
|
m.Face = FindFace(m.FaceID)
|
2021-08-22 16:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.Face == nil {
|
2021-08-21 16:36:00 +02:00
|
|
|
// Do nothing
|
2021-08-22 16:14:34 +02:00
|
|
|
} else if reported, err := m.Face.ReportCollision(m.Embeddings()); err != nil {
|
2021-08-21 16:36:00 +02:00
|
|
|
return err
|
2021-08-22 16:14:34 +02:00
|
|
|
} else if err := m.Updates(Values{"MarkerName": "", "FaceID": "", "SubjectUID": "", "SubjectSrc": src}); err != nil {
|
2021-08-21 16:36:00 +02:00
|
|
|
return err
|
2021-08-22 16:14:34 +02:00
|
|
|
} else if reported {
|
|
|
|
log.Debugf("faces: collision with %s", m.Face.ID)
|
2021-08-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
m.Face = nil
|
|
|
|
|
2021-08-21 17:11:02 +02:00
|
|
|
m.MarkerName = ""
|
|
|
|
m.FaceID = ""
|
|
|
|
m.SubjectUID = ""
|
2021-08-22 16:14:34 +02:00
|
|
|
m.SubjectSrc = src
|
2021-08-21 16:36:00 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:12:51 +02:00
|
|
|
// GetFace returns a matching face entity if possible.
|
|
|
|
func (m *Marker) GetFace() (f *Face) {
|
|
|
|
if m.Face != nil {
|
|
|
|
return m.Face
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.FaceID == "" && m.SubjectSrc == SrcManual {
|
|
|
|
if f = NewFace(m.SubjectUID, SrcManual, m.Embeddings()); f == nil {
|
|
|
|
return nil
|
|
|
|
} else if f = FirstOrCreateFace(f); f == nil {
|
|
|
|
log.Debugf("marker: invalid face")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.FaceID = f.ID
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Face = FindFace(m.FaceID)
|
|
|
|
|
|
|
|
return m.Face
|
|
|
|
}
|
|
|
|
|
2021-08-12 04:54:20 +02:00
|
|
|
// FindMarker returns an existing row if exists.
|
|
|
|
func FindMarker(id uint) *Marker {
|
|
|
|
result := Marker{}
|
|
|
|
|
|
|
|
if err := Db().Where("id = ?", id).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 14:41:59 +02:00
|
|
|
// UpdateOrCreateMarker updates a marker in the database or creates a new one if needed.
|
|
|
|
func UpdateOrCreateMarker(m *Marker) (*Marker, error) {
|
2021-06-01 17:39:03 +02:00
|
|
|
const d = 0.07
|
|
|
|
|
2021-05-25 11:38:04 +02:00
|
|
|
result := Marker{}
|
|
|
|
|
2021-05-26 14:41:59 +02:00
|
|
|
if m.ID > 0 {
|
|
|
|
err := m.Save()
|
2021-05-31 15:40:52 +02:00
|
|
|
log.Debugf("faces: saved marker %d for file %d", m.ID, m.FileID)
|
2021-05-26 14:41:59 +02:00
|
|
|
return m, err
|
2021-05-31 15:40:52 +02:00
|
|
|
} else if err := Db().Where(`file_id = ? AND x > ? AND x < ? AND y > ? AND y < ?`,
|
2021-06-01 17:39:03 +02:00
|
|
|
m.FileID, m.X-d, m.X+d, m.Y-d, m.Y+d).First(&result).Error; err == nil {
|
2021-05-26 14:41:59 +02:00
|
|
|
|
|
|
|
if SrcPriority[m.MarkerSrc] < SrcPriority[result.MarkerSrc] {
|
|
|
|
// Ignore.
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := result.Updates(map[string]interface{}{
|
2021-08-15 20:57:26 +02:00
|
|
|
"X": m.X,
|
|
|
|
"Y": m.Y,
|
|
|
|
"W": m.W,
|
|
|
|
"H": m.H,
|
2021-08-16 01:45:36 +02:00
|
|
|
"Score": m.Score,
|
|
|
|
"LandmarksJSON": m.LandmarksJSON,
|
2021-08-15 20:57:26 +02:00
|
|
|
"EmbeddingsJSON": m.EmbeddingsJSON,
|
2021-08-16 00:29:36 +02:00
|
|
|
"SubjectUID": m.SubjectUID,
|
2021-05-26 14:41:59 +02:00
|
|
|
})
|
|
|
|
|
2021-05-31 15:40:52 +02:00
|
|
|
log.Debugf("faces: updated existing marker %d for file %d", result.ID, result.FileID)
|
2021-05-26 14:41:59 +02:00
|
|
|
|
|
|
|
return &result, err
|
2021-05-25 11:38:04 +02:00
|
|
|
} else if err := m.Create(); err != nil {
|
2021-05-31 15:40:52 +02:00
|
|
|
log.Debugf("faces: added marker %d for file %d", m.ID, m.FileID)
|
2021-05-26 14:41:59 +02:00
|
|
|
return m, err
|
2021-05-25 11:38:04 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 14:41:59 +02:00
|
|
|
return m, nil
|
2021-05-25 11:38:04 +02:00
|
|
|
}
|