2021-08-14 18:13:03 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Faces returns (known) faces from the index.
|
|
|
|
func Faces(knownOnly bool) (result entity.Faces, err error) {
|
|
|
|
stmt := Db().
|
2021-08-14 19:52:49 +02:00
|
|
|
Where("id <> ?", entity.UnknownFace.ID).
|
2021-08-14 18:13:03 +02:00
|
|
|
Order("id")
|
|
|
|
|
|
|
|
if knownOnly {
|
|
|
|
stmt = stmt.Where("person_uid <> ''")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stmt.Find(&result).Error
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchKnownFaces matches known faces with file markers.
|
|
|
|
func MatchKnownFaces() (affected int64, err error) {
|
|
|
|
faces, err := Faces(true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range faces {
|
|
|
|
if res := Db().Model(&entity.Marker{}).
|
|
|
|
Where("face_id = ?", match.ID).
|
|
|
|
Updates(entity.Val{"RefUID": match.PersonUID, "RefSrc": entity.SrcPeople, "FaceID": ""}); res.Error != nil {
|
|
|
|
return affected, err
|
|
|
|
} else if res.RowsAffected > 0 {
|
|
|
|
affected += res.RowsAffected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected, nil
|
|
|
|
}
|
|
|
|
|
2021-08-14 19:52:49 +02:00
|
|
|
// PurgeAnonymousFaces removes anonymous faces from the index.
|
|
|
|
func PurgeAnonymousFaces() error {
|
2021-08-14 18:13:03 +02:00
|
|
|
return UnscopedDb().Delete(
|
|
|
|
entity.Face{},
|
2021-08-14 19:52:49 +02:00
|
|
|
"id <> ? AND person_uid = '' AND updated_at < ?", entity.UnknownFace.ID, entity.Yesterday()).Error
|
|
|
|
}
|
|
|
|
|
2021-08-15 14:14:27 +02:00
|
|
|
// ResetFaces removes all face clusters from the index.
|
|
|
|
func ResetFaces() error {
|
|
|
|
return UnscopedDb().
|
|
|
|
Delete(entity.Face{}, "id <> ?", entity.UnknownFace.ID).
|
|
|
|
Error
|
|
|
|
}
|
|
|
|
|
2021-08-14 19:52:49 +02:00
|
|
|
// CountNewFaceMarkers returns the number of new face markers in the index.
|
|
|
|
func CountNewFaceMarkers() (n int) {
|
|
|
|
var f entity.Face
|
|
|
|
|
|
|
|
if err := Db().Where("id <> ?", entity.UnknownFace.ID).Order("created_at DESC").Take(&f).Error; err != nil {
|
2021-08-15 14:14:27 +02:00
|
|
|
log.Debugf("faces: no existing clusters")
|
2021-08-14 19:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
q := Db().Model(&entity.Markers{}).Where("marker_type = ? AND embeddings <> ''", entity.MarkerFace)
|
|
|
|
|
|
|
|
if !f.CreatedAt.IsZero() {
|
|
|
|
q = q.Where("created_at > ?", f.CreatedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := q.Order("created_at DESC").Count(&n).Error; err != nil {
|
|
|
|
log.Errorf("faces: %s (count new markers)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
2021-08-14 18:13:03 +02:00
|
|
|
}
|