-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
markers.go
160 lines (128 loc) Β· 3.06 KB
/
markers.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
package entity
import (
"fmt"
"github.com/photoprism/photoprism/internal/classify"
"github.com/photoprism/photoprism/internal/face"
"github.com/photoprism/photoprism/pkg/txt"
)
// Markers represents a list of markers.
type Markers []Marker
// Save stores the markers in the database.
func (m Markers) Save(file *File) (count int, err error) {
if file == nil {
return 0, fmt.Errorf("file required for saving markers")
}
for i := range m {
if m[i].UpdateFile(file) {
continue
}
if created, err := CreateMarkerIfNotExists(&m[i]); err != nil {
log.Errorf("markers: %s (save)", err)
} else {
m[i] = *created
}
}
return file.UpdatePhotoFaceCount()
}
// Unsaved tests if any marker hasn't been saved yet.
func (m Markers) Unsaved() bool {
for i := range m {
if m[i].Unsaved() {
return true
}
}
return false
}
// Contains returns true if a marker at the same position already exists.
func (m Markers) Contains(other Marker) bool {
for i := range m {
if m[i].OverlapPercent(other) > face.OverlapThreshold {
return true
}
}
return false
}
// DetectedFaceCount returns the number of automatically detected face markers.
func (m Markers) DetectedFaceCount() (count int) {
for i := range m {
if m[i].DetectedFace() {
count++
}
}
return count
}
// ValidFaceCount returns the number of valid face markers.
func (m Markers) ValidFaceCount() (count int) {
for i := range m {
if m[i].ValidFace() {
count++
}
}
return count
}
// SubjectNames returns known subject names.
func (m Markers) SubjectNames() (names []string) {
for i := range m {
if m[i].MarkerInvalid || m[i].MarkerType != MarkerFace {
continue
} else if n := m[i].SubjectName(); n != "" {
names = append(names, n)
}
}
return txt.UniqueNames(names)
}
// Labels returns matching labels.
func (m Markers) Labels() (result classify.Labels) {
faceCount := 0
labelSrc := SrcImage
labelUncertainty := 100
for i := range m {
if m[i].ValidFace() {
faceCount++
if u := m[i].Uncertainty(); u < labelUncertainty {
labelUncertainty = u
}
if m[i].MarkerSrc != "" {
labelSrc = m[i].MarkerSrc
}
}
}
if faceCount < 1 {
return classify.Labels{}
}
var rule classify.LabelRule
if faceCount == 1 {
rule = classify.Rules["portrait"]
} else {
rule = classify.Rules["people"]
}
return classify.Labels{classify.Label{
Name: rule.Label,
Source: labelSrc,
Uncertainty: labelUncertainty,
Priority: rule.Priority,
Categories: rule.Categories,
}}
}
// Append adds a marker.
func (m *Markers) Append(marker Marker) {
*m = append(*m, marker)
}
// AppendWithEmbedding adds a marker with face embedding.
func (m *Markers) AppendWithEmbedding(marker Marker) {
if !marker.Embeddings().One() {
// Ignore markers that don't have exactly one embedding.
return
}
m.Append(marker)
}
// FindMarkers returns up to 1000 markers for a given file uid.
func FindMarkers(fileUid string) (Markers, error) {
m := Markers{}
err := Db().
Where("file_uid = ?", fileUid).
Order("x").
Offset(0).Limit(1000).
Find(&m).Error
return m, err
}