-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
faces_match.go
189 lines (149 loc) Β· 4.27 KB
/
faces_match.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
package photoprism
import (
"fmt"
"time"
"github.com/dustin/go-humanize/english"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/query"
)
// FacesMatchResult represents the outcome of Faces.Match().
type FacesMatchResult struct {
Updated int64
Recognized int64
Unknown int64
}
// Add adds result counts.
func (r *FacesMatchResult) Add(result FacesMatchResult) {
r.Updated += result.Updated
r.Recognized += result.Recognized
r.Unknown += result.Unknown
}
// Match matches markers with faces and subjects.
func (w *Faces) Match(opt FacesOptions) (result FacesMatchResult, err error) {
if w.Disabled() {
return result, fmt.Errorf("face recognition is disabled")
}
var unmatchedMarkers int
// Skip matching if index contains no new face markers, and force option isn't set.
if opt.Force {
log.Infof("faces: updating all markers")
} else if unmatchedMarkers = query.CountUnmatchedFaceMarkers(); unmatchedMarkers > 0 {
log.Infof("faces: found %s", english.Plural(unmatchedMarkers, "unmatched marker", "unmatched markers"))
} else {
log.Debugf("faces: found no unmatched markers")
}
matchedAt := entity.TimePointer()
if opt.Force || unmatchedMarkers > 0 {
faces, err := query.Faces(false, false, false, false)
if err != nil {
return result, err
}
if r, err := w.MatchFaces(faces, opt.Force, nil); err != nil {
return result, err
} else {
result.Add(r)
}
}
// Find unmatched faces.
if unmatchedFaces, err := query.Faces(false, true, false, false); err != nil {
log.Error(err)
} else if len(unmatchedFaces) > 0 {
if r, err := w.MatchFaces(unmatchedFaces, false, matchedAt); err != nil {
return result, err
} else {
result.Add(r)
}
for _, m := range unmatchedFaces {
if err := m.Matched(); err != nil {
log.Warnf("faces: %s (update match timestamp)", err)
}
}
}
// Update remaining markers based on previous matches.
if m, err := query.MatchFaceMarkers(); err != nil {
return result, err
} else {
result.Recognized += m
}
return result, nil
}
// MatchFaces matches markers against a slice of faces.
func (w *Faces) MatchFaces(faces entity.Faces, force bool, matchedBefore *time.Time) (result FacesMatchResult, err error) {
matched := 0
limit := 500
max := query.CountMarkers(entity.MarkerFace)
for {
var markers entity.Markers
if force {
markers, err = query.FaceMarkers(limit, matched)
} else {
markers, err = query.UnmatchedFaceMarkers(limit, 0, matchedBefore)
}
if err != nil {
return result, err
}
if len(markers) == 0 {
break
}
for _, marker := range markers {
matched++
if w.Canceled() {
return result, fmt.Errorf("worker canceled")
}
// Skip invalid markers.
if marker.MarkerInvalid || marker.MarkerType != entity.MarkerFace || len(marker.EmbeddingsJSON) == 0 {
continue
}
// Pointer to the matching face.
var f *entity.Face
// Dist to the matching face.
var d float64
// Find the closest face match for marker.
for i, m := range faces {
if ok, dist := m.Match(marker.Embeddings()); ok && (f == nil || dist < d) {
f = &faces[i]
d = dist
}
}
// Marker already has the best matching face?
if !marker.HasFace(f, d) {
// Marker needs a (new) face.
} else {
log.Debugf("faces: marker %s already has the best matching face %s with dist %f", marker.MarkerUID, marker.FaceID, marker.FaceDist)
if err := marker.Matched(); err != nil {
log.Warnf("faces: %s while updating marker %s match timestamp", err, marker.MarkerUID)
}
continue
}
// No matching face?
if f == nil {
if updated, err := marker.ClearFace(); err != nil {
log.Warnf("faces: %s (clear marker face)", err)
} else if updated {
result.Updated++
}
continue
}
// Assign matching face to marker.
updated, err := marker.SetFace(f, d)
if err != nil {
log.Warnf("faces: %s while setting a face for marker %s", err, marker.MarkerUID)
continue
}
if updated {
result.Updated++
}
if marker.SubjUID != "" {
result.Recognized++
} else {
result.Unknown++
}
}
log.Debugf("faces: matched %s", english.Plural(matched, "marker", "markers"))
if matched > max {
break
}
time.Sleep(50 * time.Millisecond)
}
return result, err
}