2021-08-14 18:13:03 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2021-08-22 21:06:44 +02:00
|
|
|
"fmt"
|
|
|
|
|
2023-02-21 04:49:06 +01:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2021-09-30 13:44:23 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2021-09-01 12:48:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/face"
|
2021-10-06 11:50:48 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-08-14 18:13:03 +02:00
|
|
|
)
|
|
|
|
|
2022-04-14 10:49:56 +02:00
|
|
|
// IDs represents a list of identifier strings.
|
2022-04-13 01:59:32 +02:00
|
|
|
type IDs []string
|
|
|
|
|
2022-04-14 10:49:56 +02:00
|
|
|
// FaceMap maps identification strings to face entities.
|
|
|
|
type FaceMap map[string]entity.Face
|
2022-04-13 01:59:32 +02:00
|
|
|
|
|
|
|
// FacesByID retrieves faces from the database and returns a map with the Face ID as key.
|
2023-02-21 04:49:06 +01:00
|
|
|
func FacesByID(knownOnly, unmatchedOnly, hidden, ignored bool) (FaceMap, IDs, error) {
|
|
|
|
faces, err := Faces(knownOnly, unmatchedOnly, hidden, ignored)
|
2022-04-13 01:59:32 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
faceIds := make(IDs, len(faces))
|
|
|
|
faceMap := make(FaceMap, len(faces))
|
|
|
|
|
|
|
|
for i, f := range faces {
|
|
|
|
faceMap[f.ID] = f
|
|
|
|
faceIds[i] = f.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
return faceMap, faceIds, nil
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
// Faces returns all (known / unmatched) faces from the index.
|
2023-02-21 04:49:06 +01:00
|
|
|
func Faces(knownOnly, unmatchedOnly, hidden, ignored bool) (result entity.Faces, err error) {
|
2021-09-03 16:14:09 +02:00
|
|
|
stmt := Db()
|
2021-08-22 21:06:44 +02:00
|
|
|
|
2022-04-13 01:59:32 +02:00
|
|
|
if knownOnly {
|
|
|
|
stmt = stmt.Where("subj_uid <> ''")
|
|
|
|
}
|
|
|
|
|
|
|
|
if unmatchedOnly {
|
2021-08-29 13:26:05 +02:00
|
|
|
stmt = stmt.Where("matched_at IS NULL")
|
2021-08-22 21:06:44 +02:00
|
|
|
}
|
2021-08-14 18:13:03 +02:00
|
|
|
|
2023-02-21 04:49:06 +01:00
|
|
|
if !hidden {
|
2022-04-13 01:59:32 +02:00
|
|
|
stmt = stmt.Where("face_hidden = ?", false)
|
2021-08-14 18:13:03 +02:00
|
|
|
}
|
|
|
|
|
2023-02-21 04:49:06 +01:00
|
|
|
if !ignored {
|
|
|
|
stmt = stmt.Where("face_kind <= 1")
|
|
|
|
}
|
|
|
|
|
2022-04-13 01:59:32 +02:00
|
|
|
err = stmt.Order("subj_uid, samples DESC").Find(&result).Error
|
2021-08-29 13:26:05 +02:00
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ManuallyAddedFaces returns all manually added face clusters.
|
2023-02-21 04:49:06 +01:00
|
|
|
func ManuallyAddedFaces(hidden, ignored bool) (result entity.Faces, err error) {
|
|
|
|
stmt := Db().
|
2021-09-30 13:44:23 +02:00
|
|
|
Where("face_hidden = ?", hidden).
|
2021-08-29 13:26:05 +02:00
|
|
|
Where("face_src = ?", entity.SrcManual).
|
2023-02-21 04:49:06 +01:00
|
|
|
Where("subj_uid <> ''")
|
|
|
|
|
|
|
|
if !ignored {
|
|
|
|
stmt = stmt.Where("face_kind <= 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stmt.Order("subj_uid, samples DESC").Find(&result).Error
|
2021-08-14 18:13:03 +02:00
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:12:51 +02:00
|
|
|
// MatchFaceMarkers matches markers with known faces.
|
|
|
|
func MatchFaceMarkers() (affected int64, err error) {
|
2023-02-21 04:49:06 +01:00
|
|
|
faces, err := Faces(true, false, false, false)
|
2021-08-14 18:13:03 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
|
2021-08-23 16:22:01 +02:00
|
|
|
for _, f := range faces {
|
2021-08-14 18:13:03 +02:00
|
|
|
if res := Db().Model(&entity.Marker{}).
|
2021-09-30 13:44:23 +02:00
|
|
|
Where("marker_invalid = 0").
|
2021-08-23 16:22:01 +02:00
|
|
|
Where("face_id = ?", f.ID).
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("subj_src = ?", entity.SrcAuto).
|
|
|
|
Where("subj_uid <> ?", f.SubjUID).
|
2022-04-04 08:54:03 +02:00
|
|
|
UpdateColumns(entity.Values{"subj_uid": f.SubjUID, "marker_review": false}); res.Error != nil {
|
2021-08-14 18:13:03 +02:00
|
|
|
return affected, err
|
|
|
|
} else if res.RowsAffected > 0 {
|
|
|
|
affected += res.RowsAffected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected, nil
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
// RemoveAnonymousFaceClusters removes anonymous faces from the index.
|
2022-04-13 01:59:32 +02:00
|
|
|
func RemoveAnonymousFaceClusters() (removed int, err error) {
|
2021-09-24 22:46:03 +02:00
|
|
|
res := UnscopedDb().
|
|
|
|
Delete(entity.Face{}, "subj_uid = '' AND face_src = ?", entity.SrcAuto)
|
2021-08-22 16:14:34 +02:00
|
|
|
|
2022-04-13 01:59:32 +02:00
|
|
|
return int(res.RowsAffected), res.Error
|
2021-08-14 19:52:49 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
// RemoveAutoFaceClusters removes automatically added face clusters from the index.
|
2022-04-13 01:59:32 +02:00
|
|
|
func RemoveAutoFaceClusters() (removed int, err error) {
|
2021-08-22 16:14:34 +02:00
|
|
|
res := UnscopedDb().
|
2021-09-03 16:14:09 +02:00
|
|
|
Delete(entity.Face{}, "face_src = ?", entity.SrcAuto)
|
2021-08-22 16:14:34 +02:00
|
|
|
|
2022-04-13 01:59:32 +02:00
|
|
|
return int(res.RowsAffected), res.Error
|
2021-08-15 14:14:27 +02:00
|
|
|
}
|
|
|
|
|
2021-08-23 16:22:01 +02:00
|
|
|
// CountNewFaceMarkers counts the number of new face markers in the index.
|
2021-08-29 13:26:05 +02:00
|
|
|
func CountNewFaceMarkers(size, score int) (n int) {
|
2021-08-14 19:52:49 +02:00
|
|
|
var f entity.Face
|
|
|
|
|
2021-08-24 15:20:05 +02:00
|
|
|
if err := Db().Where("face_src = ?", entity.SrcAuto).
|
|
|
|
Order("created_at DESC").Limit(1).Take(&f).Error; err != nil {
|
2021-10-05 18:42:39 +02:00
|
|
|
log.Debugf("faces: found no existing clusters")
|
2021-08-14 19:52:49 +02:00
|
|
|
}
|
|
|
|
|
2021-08-23 16:22:01 +02:00
|
|
|
q := Db().Model(&entity.Markers{}).
|
|
|
|
Where("marker_type = ?", entity.MarkerFace).
|
|
|
|
Where("face_id = '' AND marker_invalid = 0 AND embeddings_json <> ''")
|
2021-08-14 19:52:49 +02:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
if size > 0 {
|
|
|
|
q = q.Where("size >= ?", size)
|
|
|
|
}
|
|
|
|
|
2021-08-24 20:15:36 +02:00
|
|
|
if score > 0 {
|
|
|
|
q = q.Where("score >= ?", score)
|
|
|
|
}
|
|
|
|
|
2021-08-14 19:52:49 +02:00
|
|
|
if !f.CreatedAt.IsZero() {
|
|
|
|
q = q.Where("created_at > ?", f.CreatedAt)
|
|
|
|
}
|
|
|
|
|
2021-08-23 16:22:01 +02:00
|
|
|
if err := q.Count(&n).Error; err != nil {
|
2021-08-14 19:52:49 +02:00
|
|
|
log.Errorf("faces: %s (count new markers)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
2021-08-14 18:13:03 +02:00
|
|
|
}
|
2021-08-22 21:06:44 +02:00
|
|
|
|
2021-08-31 15:33:42 +02:00
|
|
|
// PurgeOrphanFaces removes unused faces from the index.
|
2023-02-21 04:49:06 +01:00
|
|
|
func PurgeOrphanFaces(faceIds []string, ignored bool) (removed int64, err error) {
|
2021-08-29 13:26:05 +02:00
|
|
|
// Remove invalid face IDs.
|
2023-02-21 04:49:06 +01:00
|
|
|
stmt := Db().
|
2021-08-29 13:26:05 +02:00
|
|
|
Where("id IN (?)", faceIds).
|
2023-02-21 04:49:06 +01:00
|
|
|
Where("id NOT IN (SELECT face_id FROM ?)", gorm.Expr(entity.Marker{}.TableName()))
|
|
|
|
|
|
|
|
if !ignored {
|
|
|
|
stmt = stmt.Where("face_kind <= 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := stmt.Delete(&entity.Face{}); res.Error != nil {
|
2021-08-31 20:08:53 +02:00
|
|
|
return removed, fmt.Errorf("faces: %s while purging orphans", res.Error)
|
2021-08-29 13:26:05 +02:00
|
|
|
} else {
|
|
|
|
removed += res.RowsAffected
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed, nil
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:06:44 +02:00
|
|
|
// MergeFaces returns a new face that replaces multiple others.
|
2023-02-21 04:49:06 +01:00
|
|
|
func MergeFaces(merge entity.Faces, ignored bool) (merged *entity.Face, err error) {
|
2021-08-22 21:06:44 +02:00
|
|
|
if len(merge) < 2 {
|
|
|
|
// Nothing to merge.
|
2021-08-31 15:33:42 +02:00
|
|
|
return merged, fmt.Errorf("faces: two or more clusters required for merging")
|
2021-08-22 21:06:44 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
subjUID := merge[0].SubjUID
|
2021-08-31 15:33:42 +02:00
|
|
|
|
|
|
|
for i := 1; i < len(merge); i++ {
|
2021-09-17 14:26:12 +02:00
|
|
|
if merge[i].SubjUID != subjUID {
|
2022-01-05 11:40:44 +01:00
|
|
|
return merged, fmt.Errorf("faces: cannot merge clusters with conflicting subjects %s <> %s",
|
2022-04-15 09:42:07 +02:00
|
|
|
clean.Log(subjUID), clean.Log(merge[i].SubjUID))
|
2021-08-31 15:33:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find or create merged face cluster.
|
2021-09-17 14:26:12 +02:00
|
|
|
if merged = entity.NewFace(merge[0].SubjUID, merge[0].FaceSrc, merge.Embeddings()); merged == nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
return merged, fmt.Errorf("faces: new cluster is nil for subject %s", clean.Log(subjUID))
|
2021-08-31 15:33:42 +02:00
|
|
|
} else if merged = entity.FirstOrCreateFace(merged); merged == nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
return merged, fmt.Errorf("faces: failed creating new cluster for subject %s", clean.Log(subjUID))
|
2021-08-29 13:26:05 +02:00
|
|
|
} else if err := merged.MatchMarkers(append(merge.IDs(), "")); err != nil {
|
2021-08-22 21:06:44 +02:00
|
|
|
return merged, err
|
|
|
|
}
|
|
|
|
|
2021-08-31 15:33:42 +02:00
|
|
|
// PurgeOrphanFaces removes unused faces from the index.
|
2023-02-21 04:49:06 +01:00
|
|
|
if removed, err := PurgeOrphanFaces(merge.IDs(), ignored); err != nil {
|
2021-08-31 15:33:42 +02:00
|
|
|
return merged, err
|
|
|
|
} else if removed > 0 {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("faces: removed %d orphans for subject %s", removed, clean.Log(subjUID))
|
2021-08-29 13:26:05 +02:00
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("faces: failed removing merged clusters for subject %s", clean.Log(subjUID))
|
2021-08-22 21:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return merged, err
|
|
|
|
}
|
2021-09-01 12:48:17 +02:00
|
|
|
|
|
|
|
// ResolveFaceCollisions resolves collisions of different subject's faces.
|
|
|
|
func ResolveFaceCollisions() (conflicts, resolved int, err error) {
|
2023-02-21 04:49:06 +01:00
|
|
|
faces, ids, err := FacesByID(true, false, false, false)
|
2021-09-01 12:48:17 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return conflicts, resolved, err
|
|
|
|
}
|
|
|
|
|
2022-04-13 01:59:32 +02:00
|
|
|
// Remembers matched combinations.
|
|
|
|
done := make(map[string]bool, len(ids)*len(ids))
|
|
|
|
|
|
|
|
// Find face assignment collisions.
|
|
|
|
for _, i := range ids {
|
|
|
|
for _, j := range ids {
|
|
|
|
var f1, f2 entity.Face
|
|
|
|
|
|
|
|
if f, ok := faces[i]; ok {
|
|
|
|
f1 = f
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, ok := faces[j]; ok {
|
|
|
|
f2 = f
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var matchId string
|
|
|
|
|
|
|
|
// Skip?
|
|
|
|
if matchId = f1.MatchId(f2); matchId == "" || done[matchId] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare face 1 with face 2.
|
2021-09-30 13:44:23 +02:00
|
|
|
if matched, dist := f1.Match(face.Embeddings{f2.Embedding()}); matched {
|
2021-09-17 14:26:12 +02:00
|
|
|
if f1.SubjUID == f2.SubjUID {
|
2021-09-01 12:48:17 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
conflicts++
|
|
|
|
|
2021-09-23 13:16:05 +02:00
|
|
|
r := f1.SampleRadius + face.MatchDist
|
2021-09-01 12:48:17 +02:00
|
|
|
|
2022-04-13 01:59:32 +02:00
|
|
|
log.Infof("faces: face %s has ambiguous subject at dist %f, Ø %f from %d samples, collision Ø %f", f1.ID, dist, r, f1.Samples, f1.CollisionRadius)
|
2021-09-01 12:48:17 +02:00
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
if f1.SubjUID != "" {
|
2022-04-13 01:59:32 +02:00
|
|
|
log.Debugf("faces: face %s has %s subject %s (%s)", f1.ID, entity.SrcString(f1.FaceSrc), entity.SubjNames.Log(f1.SubjUID), f1.SubjUID)
|
2021-09-01 12:48:17 +02:00
|
|
|
} else {
|
2022-04-13 01:59:32 +02:00
|
|
|
log.Debugf("faces: face %s has unknown subject (%s)", f1.ID, entity.SrcString(f1.FaceSrc))
|
2021-09-01 12:48:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
if f2.SubjUID != "" {
|
2022-04-13 01:59:32 +02:00
|
|
|
log.Debugf("faces: face %s has %s subject %s (%s)", f2.ID, entity.SrcString(f2.FaceSrc), entity.SubjNames.Log(f2.SubjUID), f2.SubjUID)
|
2021-09-01 12:48:17 +02:00
|
|
|
} else {
|
2022-04-13 01:59:32 +02:00
|
|
|
log.Debugf("faces: face %s has unknown subject (%s)", f2.ID, entity.SrcString(f2.FaceSrc))
|
2021-09-01 12:48:17 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 01:59:32 +02:00
|
|
|
// Resolve.
|
|
|
|
success, failed := f1.ResolveCollision(face.Embeddings{f2.Embedding()})
|
|
|
|
|
2022-10-31 13:25:02 +01:00
|
|
|
// Failed?
|
2022-04-13 01:59:32 +02:00
|
|
|
if failed != nil {
|
|
|
|
log.Errorf("faces: conflict resolution for %s failed, face %s has collisions with other persons (%s)", entity.SubjNames.Log(f1.SubjUID), f1.ID, failed)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success?
|
|
|
|
if success {
|
|
|
|
log.Infof("faces: successful conflict resolution for %s, face %s had collisions with other persons", entity.SubjNames.Log(f1.SubjUID), f1.ID)
|
2021-09-01 12:48:17 +02:00
|
|
|
resolved++
|
2023-02-21 04:49:06 +01:00
|
|
|
faces, _, err = FacesByID(true, false, false, false)
|
2022-04-13 01:59:32 +02:00
|
|
|
logErr("faces", "refresh", err)
|
2021-09-01 12:48:17 +02:00
|
|
|
} else {
|
2022-04-13 01:59:32 +02:00
|
|
|
log.Infof("faces: conflict resolution for %s not successful, face %s still has collisions with other persons", entity.SubjNames.Log(f1.SubjUID), f1.ID)
|
2021-09-01 12:48:17 +02:00
|
|
|
}
|
2022-04-13 01:59:32 +02:00
|
|
|
|
|
|
|
done[matchId] = true
|
2021-09-01 12:48:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conflicts, resolved, nil
|
|
|
|
}
|
2021-09-22 19:33:41 +02:00
|
|
|
|
2021-10-05 22:33:29 +02:00
|
|
|
// RemovePeopleAndFaces permanently removes all people, faces, and face markers.
|
2021-09-22 19:33:41 +02:00
|
|
|
func RemovePeopleAndFaces() (err error) {
|
2021-12-09 02:33:41 +01:00
|
|
|
mutex.Index.Lock()
|
|
|
|
defer mutex.Index.Unlock()
|
2021-10-06 11:50:48 +02:00
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
// Delete people.
|
|
|
|
if err = UnscopedDb().Delete(entity.Subject{}, "subj_type = ?", entity.SubjPerson).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all faces.
|
|
|
|
if err = UnscopedDb().Delete(entity.Face{}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete face markers.
|
|
|
|
if err = UnscopedDb().Delete(entity.Marker{}, "marker_type = ?", entity.MarkerFace).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset face counters.
|
|
|
|
if err = UnscopedDb().Model(entity.Photo{}).
|
2022-04-04 08:54:03 +02:00
|
|
|
UpdateColumn("photo_faces", 0).Error; err != nil {
|
2021-09-22 19:33:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset people label.
|
|
|
|
if label, err := LabelBySlug("people"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err = UnscopedDb().
|
|
|
|
Delete(entity.PhotoLabel{}, "label_id = ?", label.ID).Error; err != nil {
|
|
|
|
return err
|
|
|
|
} else if err = label.Update("PhotoCount", 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset portrait label.
|
|
|
|
if label, err := LabelBySlug("portrait"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err = UnscopedDb().
|
|
|
|
Delete(entity.PhotoLabel{}, "label_id = ?", label.ID).Error; err != nil {
|
|
|
|
return err
|
|
|
|
} else if err = label.Update("PhotoCount", 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|