2021-06-02 17:25:04 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2021-08-19 21:12:38 +02:00
|
|
|
"fmt"
|
2021-08-24 20:15:36 +02:00
|
|
|
"time"
|
2021-08-19 21:12:38 +02:00
|
|
|
|
2021-06-02 17:25:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2021-09-30 13:44:23 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/face"
|
2021-06-02 17:25:04 +02:00
|
|
|
)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// MarkerByUID returns a Marker based on the UID.
|
|
|
|
func MarkerByUID(uid string) (*entity.Marker, error) {
|
|
|
|
result := entity.Marker{}
|
|
|
|
|
|
|
|
err := UnscopedDb().Where("marker_uid = ?", uid).First(&result).Error
|
2021-06-02 17:25:04 +02:00
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
return &result, err
|
2021-06-02 17:25:04 +02:00
|
|
|
}
|
2021-08-12 04:54:20 +02:00
|
|
|
|
|
|
|
// Markers finds a list of file markers filtered by type, embeddings, and sorted by id.
|
2021-08-24 20:15:36 +02:00
|
|
|
func Markers(limit, offset int, markerType string, embeddings, subjects bool, matchedBefore time.Time) (result entity.Markers, err error) {
|
2021-08-14 20:48:38 +02:00
|
|
|
db := Db()
|
2021-08-12 04:54:20 +02:00
|
|
|
|
|
|
|
if markerType != "" {
|
2021-08-14 20:48:38 +02:00
|
|
|
db = db.Where("marker_type = ?", markerType)
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if embeddings {
|
2021-08-15 20:57:26 +02:00
|
|
|
db = db.Where("embeddings_json <> ''")
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
if subjects {
|
2021-09-17 14:26:12 +02:00
|
|
|
db = db.Where("subj_uid <> ''")
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 20:15:36 +02:00
|
|
|
if !matchedBefore.IsZero() {
|
|
|
|
db = db.Where("matched_at IS NULL OR matched_at < ?", matchedBefore)
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
db = db.Order("matched_at, marker_uid").Limit(limit).Offset(offset)
|
2021-08-14 20:48:38 +02:00
|
|
|
|
|
|
|
err = db.Find(&result).Error
|
2021-08-12 04:54:20 +02:00
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
// UnmatchedFaceMarkers finds all currently unmatched face markers.
|
|
|
|
func UnmatchedFaceMarkers(limit, offset int, matchedBefore *time.Time) (result entity.Markers, err error) {
|
|
|
|
db := Db().
|
|
|
|
Where("marker_type = ?", entity.MarkerFace).
|
|
|
|
Where("marker_invalid = 0").
|
|
|
|
Where("embeddings_json <> ''")
|
|
|
|
|
|
|
|
if matchedBefore == nil {
|
|
|
|
db = db.Where("matched_at IS NULL")
|
|
|
|
} else if !matchedBefore.IsZero() {
|
|
|
|
db = db.Where("matched_at IS NULL OR matched_at < ?", matchedBefore)
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
db = db.Order("matched_at, marker_uid").Limit(limit).Offset(offset)
|
2021-08-29 13:26:05 +02:00
|
|
|
|
|
|
|
err = db.Find(&result).Error
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FaceMarkers returns all face markers sorted by id.
|
|
|
|
func FaceMarkers(limit, offset int) (result entity.Markers, err error) {
|
|
|
|
err = Db().
|
|
|
|
Where("marker_type = ?", entity.MarkerFace).
|
2021-09-01 20:46:15 +02:00
|
|
|
Order("marker_uid").Limit(limit).Offset(offset).
|
2021-08-29 13:26:05 +02:00
|
|
|
Find(&result).Error
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
// Embeddings returns existing face embeddings.
|
2021-09-30 13:44:23 +02:00
|
|
|
func Embeddings(single, unclustered bool, size, score int) (result face.Embeddings, err error) {
|
2021-08-12 04:54:20 +02:00
|
|
|
var col []string
|
|
|
|
|
|
|
|
stmt := Db().
|
|
|
|
Model(&entity.Marker{}).
|
|
|
|
Where("marker_type = ?", entity.MarkerFace).
|
2021-08-19 21:12:38 +02:00
|
|
|
Where("marker_invalid = 0").
|
2021-08-15 20:57:26 +02:00
|
|
|
Where("embeddings_json <> ''").
|
2021-09-01 20:46:15 +02:00
|
|
|
Order("marker_uid")
|
2021-08-12 04:54:20 +02:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
if size > 0 {
|
|
|
|
stmt = stmt.Where("size >= ?", size)
|
|
|
|
}
|
|
|
|
|
2021-08-24 20:15:36 +02:00
|
|
|
if score > 0 {
|
|
|
|
stmt = stmt.Where("score >= ?", score)
|
|
|
|
}
|
|
|
|
|
2021-08-23 16:22:01 +02:00
|
|
|
if unclustered {
|
|
|
|
stmt = stmt.Where("face_id = ''")
|
|
|
|
}
|
|
|
|
|
2021-08-15 20:57:26 +02:00
|
|
|
if err := stmt.Pluck("embeddings_json", &col).Error; err != nil {
|
2021-08-12 04:54:20 +02:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, embeddingsJson := range col {
|
2021-09-30 13:44:23 +02:00
|
|
|
if embeddings := face.UnmarshalEmbeddings(embeddingsJson); !embeddings.Empty() {
|
2021-08-13 20:04:59 +02:00
|
|
|
if single {
|
|
|
|
// Single embedding per face detected.
|
|
|
|
result = append(result, embeddings[0])
|
|
|
|
} else {
|
|
|
|
// Return all embedding otherwise.
|
|
|
|
result = append(result, embeddings...)
|
|
|
|
}
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2021-08-14 20:48:38 +02:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
// RemoveInvalidMarkerReferences removes face and subject references from invalid markers.
|
2021-08-23 16:22:01 +02:00
|
|
|
func RemoveInvalidMarkerReferences() (removed int64, err error) {
|
2021-08-29 13:26:05 +02:00
|
|
|
res := Db().
|
2021-08-19 21:12:38 +02:00
|
|
|
Model(&entity.Marker{}).
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("marker_invalid = 1 AND (subj_uid <> '' OR face_id <> '')").
|
|
|
|
UpdateColumns(entity.Values{"subj_uid": "", "face_id": "", "face_dist": -1.0, "matched_at": nil})
|
2021-08-19 21:12:38 +02:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
return res.RowsAffected, res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveNonExistentMarkerFaces removes non-existent face IDs from the markers table.
|
|
|
|
func RemoveNonExistentMarkerFaces() (removed int64, err error) {
|
|
|
|
|
|
|
|
res := Db().
|
2021-08-19 21:12:38 +02:00
|
|
|
Model(&entity.Marker{}).
|
2021-08-23 16:22:01 +02:00
|
|
|
Where("marker_type = ?", entity.MarkerFace).
|
2021-08-22 16:14:34 +02:00
|
|
|
Where(fmt.Sprintf("face_id <> '' AND face_id NOT IN (SELECT id FROM %s)", entity.Face{}.TableName())).
|
2021-08-29 13:26:05 +02:00
|
|
|
UpdateColumns(entity.Values{"face_id": "", "face_dist": -1.0, "matched_at": nil})
|
2021-08-23 16:22:01 +02:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
return res.RowsAffected, res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveNonExistentMarkerSubjects removes non-existent subject UIDs from the markers table.
|
|
|
|
func RemoveNonExistentMarkerSubjects() (removed int64, err error) {
|
|
|
|
res := Db().
|
2021-08-23 16:22:01 +02:00
|
|
|
Model(&entity.Marker{}).
|
2021-09-17 14:26:12 +02:00
|
|
|
Where(fmt.Sprintf("subj_uid <> '' AND subj_uid NOT IN (SELECT subj_uid FROM %s)", entity.Subject{}.TableName())).
|
|
|
|
UpdateColumns(entity.Values{"subj_uid": "", "matched_at": nil})
|
2021-08-29 13:26:05 +02:00
|
|
|
|
|
|
|
return res.RowsAffected, res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FixMarkerReferences repairs invalid or non-existent references in the markers table.
|
|
|
|
func FixMarkerReferences() (removed int64, err error) {
|
|
|
|
if r, err := RemoveInvalidMarkerReferences(); err != nil {
|
|
|
|
return removed, err
|
|
|
|
} else {
|
|
|
|
removed += r
|
|
|
|
}
|
|
|
|
|
|
|
|
if r, err := RemoveNonExistentMarkerFaces(); err != nil {
|
|
|
|
return removed, err
|
2021-08-23 16:22:01 +02:00
|
|
|
} else {
|
2021-08-29 13:26:05 +02:00
|
|
|
removed += r
|
|
|
|
}
|
|
|
|
|
|
|
|
if r, err := RemoveNonExistentMarkerSubjects(); err != nil {
|
|
|
|
return removed, err
|
|
|
|
} else {
|
|
|
|
removed += r
|
2021-08-23 16:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return removed, nil
|
2021-08-14 20:48:38 +02:00
|
|
|
}
|
2021-08-15 14:14:27 +02:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
// MarkersWithNonExistentReferences finds markers with non-existent face or subject references.
|
|
|
|
func MarkersWithNonExistentReferences() (faces entity.Markers, subjects entity.Markers, err error) {
|
2021-08-28 15:26:26 +02:00
|
|
|
// Find markers with invalid face IDs.
|
|
|
|
if res := Db().
|
|
|
|
Where("marker_type = ?", entity.MarkerFace).
|
|
|
|
Where(fmt.Sprintf("face_id <> '' AND face_id NOT IN (SELECT id FROM %s)", entity.Face{}.TableName())).
|
|
|
|
Find(&faces); res.Error != nil {
|
|
|
|
err = res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find markers with invalid subject UIDs.
|
|
|
|
if res := Db().
|
2021-09-17 14:26:12 +02:00
|
|
|
Where(fmt.Sprintf("subj_uid <> '' AND subj_uid NOT IN (SELECT subj_uid FROM %s)", entity.Subject{}.TableName())).
|
2021-08-28 15:26:26 +02:00
|
|
|
Find(&subjects); res.Error != nil {
|
|
|
|
err = res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return faces, subjects, err
|
|
|
|
}
|
|
|
|
|
2021-08-28 17:27:37 +02:00
|
|
|
// MarkersWithSubjectConflict finds markers with conflicting subjects.
|
|
|
|
func MarkersWithSubjectConflict() (results entity.Markers, err error) {
|
|
|
|
err = Db().
|
2021-09-17 14:26:12 +02:00
|
|
|
Joins(fmt.Sprintf("JOIN %s f ON f.id = face_id AND f.subj_uid <> %s.subj_uid", entity.Face{}.TableName(), entity.Marker{}.TableName())).
|
2021-08-28 17:27:37 +02:00
|
|
|
Order("face_id").
|
|
|
|
Find(&results).Error
|
|
|
|
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
// ResetFaceMarkerMatches removes automatically added subject and face references from the markers table.
|
|
|
|
func ResetFaceMarkerMatches() (removed int64, err error) {
|
|
|
|
res := Db().Model(&entity.Marker{}).
|
2021-09-17 14:26:12 +02:00
|
|
|
Where("subj_src = ? AND marker_type = ?", entity.SrcAuto, entity.MarkerFace).
|
|
|
|
UpdateColumns(entity.Values{"marker_name": "", "subj_uid": "", "subj_src": "", "face_id": "", "face_dist": -1.0, "matched_at": nil})
|
2021-08-15 14:14:27 +02:00
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
return res.RowsAffected, res.Error
|
2021-08-15 14:14:27 +02:00
|
|
|
}
|
2021-08-24 20:15:36 +02:00
|
|
|
|
|
|
|
// CountUnmatchedFaceMarkers counts the number of unmatched face markers in the index.
|
2021-08-29 13:26:05 +02:00
|
|
|
func CountUnmatchedFaceMarkers() (n int) {
|
2021-08-24 20:15:36 +02:00
|
|
|
q := Db().Model(&entity.Markers{}).
|
2021-08-29 13:26:05 +02:00
|
|
|
Where("matched_at IS NULL AND marker_invalid = 0 AND embeddings_json <> ''").
|
|
|
|
Where("marker_type = ?", entity.MarkerFace)
|
2021-08-24 20:15:36 +02:00
|
|
|
|
|
|
|
if err := q.Count(&n).Error; err != nil {
|
|
|
|
log.Errorf("faces: %s (count unmatched markers)", err)
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
return n
|
2021-08-24 20:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CountMarkers counts the number of face markers in the index.
|
|
|
|
func CountMarkers(markerType string) (n int) {
|
|
|
|
q := Db().Model(&entity.Markers{})
|
|
|
|
|
|
|
|
if markerType != "" {
|
|
|
|
q = q.Where("marker_type = ?", markerType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := q.Count(&n).Error; err != nil {
|
|
|
|
log.Errorf("faces: %s (count markers)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
2021-09-24 13:13:59 +02:00
|
|
|
|
|
|
|
// RemoveOrphanMarkers removes markers without an existing file.
|
|
|
|
func RemoveOrphanMarkers() (removed int64, err error) {
|
|
|
|
where := fmt.Sprintf("file_uid NOT IN (SELECT file_uid FROM %s)", entity.File{}.TableName())
|
|
|
|
|
|
|
|
if res := UnscopedDb().
|
|
|
|
Delete(&entity.Marker{}, where); res.Error != nil {
|
|
|
|
return removed, fmt.Errorf("markers: %s (purge orphans)", res.Error)
|
|
|
|
} else {
|
|
|
|
removed += res.RowsAffected
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed, nil
|
|
|
|
}
|