2021-06-02 17:25:04 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2021-08-19 21:12:38 +02:00
|
|
|
"fmt"
|
|
|
|
|
2021-06-02 17:25:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2021-08-14 20:48:38 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2021-06-02 17:25:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// MarkerByID returns a Marker based on the ID.
|
|
|
|
func MarkerByID(id uint) (marker entity.Marker, err error) {
|
|
|
|
if err := UnscopedDb().Where("id = ?", id).
|
|
|
|
First(&marker).Error; err != nil {
|
|
|
|
return marker, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return marker, nil
|
|
|
|
}
|
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-19 21:12:38 +02:00
|
|
|
func Markers(limit, offset int, markerType string, embeddings, subjects bool) (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 {
|
|
|
|
db = db.Where("subject_uid <> ''")
|
2021-08-12 04:54:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 20:48:38 +02:00
|
|
|
db = db.Order("id").Limit(limit).Offset(offset)
|
|
|
|
|
|
|
|
err = db.Find(&result).Error
|
2021-08-12 04:54:20 +02:00
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
// Embeddings returns existing face embeddings.
|
|
|
|
func Embeddings(single bool) (result entity.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-08-12 04:54:20 +02:00
|
|
|
Order("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 {
|
|
|
|
if embeddings := entity.UnmarshalEmbeddings(embeddingsJson); len(embeddings) > 0 {
|
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-19 23:12:51 +02:00
|
|
|
// AddMarkerSubjects adds and references known marker subjects.
|
|
|
|
func AddMarkerSubjects() (affected int64, err error) {
|
2021-08-14 20:48:38 +02:00
|
|
|
var markers entity.Markers
|
|
|
|
|
|
|
|
if err := Db().
|
2021-08-19 21:12:38 +02:00
|
|
|
Where("face_id <> '' AND subject_uid = '' AND subject_src = ?", entity.SrcAuto).
|
2021-08-14 20:48:38 +02:00
|
|
|
Where("marker_invalid = 0 AND marker_type = ?", entity.MarkerFace).
|
2021-08-16 00:29:36 +02:00
|
|
|
Where("marker_name <> ''").
|
|
|
|
Order("marker_name").
|
2021-08-14 20:48:38 +02:00
|
|
|
Find(&markers).Error; err != nil {
|
|
|
|
return affected, err
|
|
|
|
} else if len(markers) == 0 {
|
|
|
|
return affected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range markers {
|
2021-08-19 21:12:38 +02:00
|
|
|
if faceId := m.FaceID; faceId == "" {
|
|
|
|
// Do nothing.
|
|
|
|
} else if subj := entity.NewSubject(m.MarkerName, entity.SubjectPerson, entity.SrcMarker); subj == nil {
|
2021-08-16 00:29:36 +02:00
|
|
|
log.Errorf("faces: subject should not be nil - bug?")
|
|
|
|
} else if subj = entity.FirstOrCreateSubject(subj); subj == nil {
|
|
|
|
log.Errorf("faces: failed adding subject %s for marker %d", txt.Quote(m.MarkerName), m.ID)
|
2021-08-19 21:12:38 +02:00
|
|
|
} else if err := m.Updates(entity.Values{"SubjectUID": subj.SubjectUID, "SubjectSrc": entity.SrcAuto}); err != nil {
|
2021-08-14 20:48:38 +02:00
|
|
|
return affected, err
|
2021-08-16 00:29:36 +02:00
|
|
|
} else if err := Db().Model(&entity.Face{}).Where("id = ? AND subject_uid = ''", faceId).Update("SubjectUID", subj.SubjectUID).Error; err != nil {
|
2021-08-14 20:48:38 +02:00
|
|
|
return affected, err
|
|
|
|
} else {
|
|
|
|
affected++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
return affected, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TidyMarkers resets invalid marker data as needed.
|
|
|
|
func TidyMarkers() (err error) {
|
|
|
|
// Reset subject and face relationships for invalid markers.
|
|
|
|
err = Db().
|
|
|
|
Model(&entity.Marker{}).
|
|
|
|
Where("marker_invalid = 1").
|
|
|
|
UpdateColumns(entity.Values{"subject_uid": "", "subject_src": "", "face_id": ""}).
|
|
|
|
Error
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset invalid face IDs.
|
|
|
|
return Db().
|
|
|
|
Model(&entity.Marker{}).
|
|
|
|
Where(fmt.Sprintf("face_id NOT IN (SELECT id FROM %s)", entity.Face{}.TableName())).
|
|
|
|
UpdateColumns(entity.Values{"face_id": ""}).
|
|
|
|
Error
|
2021-08-14 20:48:38 +02:00
|
|
|
}
|
2021-08-15 14:14:27 +02:00
|
|
|
|
|
|
|
// ResetFaceMarkerMatches removes people and face matches from face markers.
|
|
|
|
func ResetFaceMarkerMatches() error {
|
2021-08-16 00:29:36 +02:00
|
|
|
v := entity.Values{"subject_uid": "", "subject_src": "", "face_id": ""}
|
2021-08-15 14:14:27 +02:00
|
|
|
|
|
|
|
return Db().Model(&entity.Marker{}).Where("marker_type = ?", entity.MarkerFace).UpdateColumns(v).Error
|
|
|
|
}
|