/
faces_audit.go
199 lines (164 loc) Β· 6.92 KB
/
faces_audit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package photoprism
import (
"github.com/dustin/go-humanize/english"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/face"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/pkg/clean"
)
// Audit face clusters and subjects.
func (w *Faces) Audit(fix bool) (err error) {
invalidFaces, invalidSubj, err := query.MarkersWithNonExistentReferences()
if err != nil {
return err
}
subj, err := query.SubjectMap()
if err != nil {
log.Errorf("faces: %s (find subjects)", err)
}
if n := len(subj); n == 0 {
log.Infof("faces: found no subjects")
} else {
log.Infof("faces: found %s", english.Plural(n, "subject", "subjects"))
}
// Fix non-existent marker subjects references?
if n := len(invalidSubj); n == 0 {
log.Infof("faces: found no invalid marker subjects")
} else if !fix {
log.Infof("faces: %s with non-existent subjects", english.Plural(n, "marker", "markers"))
} else if removed, err := query.RemoveNonExistentMarkerSubjects(); err != nil {
log.Errorf("faces: %s (remove orphan subjects)", err)
} else if removed > 0 {
log.Infof("faces: removed %d / %d markers with non-existent subjects", removed, n)
}
// Fix non-existent marker face references?
if n := len(invalidFaces); n == 0 {
log.Infof("faces: found no invalid marker faces")
} else if !fix {
log.Infof("faces: %s with non-existent faces", english.Plural(n, "marker", "markers"))
} else if removed, err := query.RemoveNonExistentMarkerFaces(); err != nil {
log.Errorf("faces: %s (remove orphan embeddings)", err)
} else if removed > 0 {
log.Infof("faces: removed %d / %d markers with non-existent faces", removed, n)
}
conflicts := 0
resolved := 0
faces, ids, err := query.FacesByID(true, false, false, false)
if err != nil {
return err
}
// 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.
if matched, dist := f1.Match(face.Embeddings{f2.Embedding()}); matched {
if f1.SubjUID == f2.SubjUID {
continue
}
conflicts++
r := f1.SampleRadius + face.MatchDist
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)
if f1.SubjUID != "" {
log.Infof("faces: face %s belongs to subject %s (%s %s)", f1.ID, entity.SubjNames.Log(f1.SubjUID), f1.SubjUID, entity.SrcString(f1.FaceSrc))
} else {
log.Infof("faces: face %s has no subject assigned (%s)", f1.ID, entity.SrcString(f1.FaceSrc))
}
if f2.SubjUID != "" {
log.Infof("faces: face %s belongs to subject %s (%s %s)", f2.ID, entity.SubjNames.Log(f2.SubjUID), f2.SubjUID, entity.SrcString(f2.FaceSrc))
} else {
log.Infof("faces: face %s has no subject assigned (%s)", f2.ID, entity.SrcString(f2.FaceSrc))
}
// Skip fix?
if !fix {
continue
}
// Resolve.
success, failed := f1.ResolveCollision(face.Embeddings{f2.Embedding()})
// Failed?
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)
resolved++
faces, _, err = query.FacesByID(true, false, false, false)
logErr("faces", "refresh", err)
} else {
log.Infof("faces: conflict resolution for %s not successful, face %s still has collisions with other persons", entity.SubjNames.Log(f1.SubjUID), f1.ID)
}
done[matchId] = true
}
}
}
// Show conflict resolution results.
if conflicts == 0 {
log.Infof("faces: found no ambiguous subjects")
} else if !fix {
log.Infof("faces: found %s", english.Plural(conflicts, "ambiguous subject", "ambiguous subjects"))
} else {
log.Infof("faces: found %s, %d resolved", english.Plural(conflicts, "ambiguous subject", "ambiguous subjects"), resolved)
}
// Show remaining issues.
if markers, err := query.MarkersWithSubjectConflict(); err != nil {
logErr("faces", "find marker conflicts", err)
} else {
for _, m := range markers {
if m.FaceID == "" {
log.Warnf("faces: marker %s has an empty face id - you may have found a bug", m.MarkerUID)
} else if f, ok := faces[m.FaceID]; !ok {
log.Warnf("faces: marker %s has invalid face %s of subject %s (%s)", m.MarkerUID, m.FaceID, entity.SubjNames.Log(f.SubjUID), f.SubjUID)
} else if m.SubjUID != "" {
log.Infof("faces: marker %s with %s subject %s (%s) conflicts with face %s (%s) of subject %s (%s)", m.MarkerUID, entity.SrcString(m.SubjSrc), entity.SubjNames.Log(m.SubjUID), m.SubjUID, m.FaceID, entity.SrcString(f.FaceSrc), entity.SubjNames.Log(f.SubjUID), f.SubjUID)
} else if m.MarkerName != "" {
log.Infof("faces: marker %s with %s subject name %s conflicts with face %s (%s) of subject %s (%s)", m.MarkerUID, entity.SrcString(m.SubjSrc), clean.Log(m.MarkerName), m.FaceID, entity.SrcString(f.FaceSrc), entity.SubjNames.Log(f.SubjUID), f.SubjUID)
} else {
log.Infof("faces: marker %s with unknown subject (%s) conflicts with face %s (%s) of subject %s (%s)", m.MarkerUID, entity.SrcString(m.SubjSrc), m.FaceID, entity.SrcString(f.FaceSrc), entity.SubjNames.Log(f.SubjUID), f.SubjUID)
}
}
}
// Find and fix orphan face clusters.
if orphans, err := entity.OrphanFaces(); err != nil {
log.Errorf("faces: %s while finding orphan face clusters", err)
} else if l := len(orphans); l == 0 {
log.Infof("faces: found no orphan face clusters")
} else if !fix {
log.Infof("faces: found %s", english.Plural(l, "orphan face cluster", "orphan face clusters"))
} else if err := orphans.Delete(); err != nil {
log.Errorf("faces: failed removing %s: %s", english.Plural(l, "orphan face cluster", "orphan face clusters"), err)
} else {
log.Infof("faces: removed %s", english.Plural(l, "orphan face cluster", "orphan face clusters"))
}
// Find and fix orphan people.
if orphans, err := entity.OrphanPeople(); err != nil {
log.Errorf("faces: %s while finding orphan people", err)
} else if l := len(orphans); l == 0 {
log.Infof("faces: found no orphan people")
} else if !fix {
log.Infof("faces: found %s", english.Plural(l, "orphan person", "orphan people"))
} else if err := orphans.Delete(); err != nil {
log.Errorf("faces: failed fixing %s: %s", english.Plural(l, "orphan person", "orphan people"), err)
} else {
log.Infof("faces: removed %s", english.Plural(l, "orphan person", "orphan people"))
}
return nil
}