-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
index_labels.go
66 lines (49 loc) Β· 1.45 KB
/
index_labels.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
package photoprism
import (
"sort"
"time"
"github.com/photoprism/photoprism/internal/classify"
"github.com/photoprism/photoprism/internal/thumb"
"github.com/photoprism/photoprism/pkg/clean"
)
// Labels classifies a JPEG image and returns matching labels.
func (ind *Index) Labels(jpeg *MediaFile) (results classify.Labels) {
start := time.Now()
var sizes []thumb.Name
if jpeg.Square() {
sizes = []thumb.Name{thumb.Tile224}
} else {
sizes = []thumb.Name{thumb.Tile224, thumb.Left224, thumb.Right224}
}
var labels classify.Labels
for _, size := range sizes {
filename, err := jpeg.Thumbnail(Config().ThumbCachePath(), size)
if err != nil {
log.Debugf("%s in %s", err, clean.Log(jpeg.BaseName()))
continue
}
imageLabels, err := ind.tensorFlow.File(filename)
if err != nil {
log.Debugf("%s in %s", err, clean.Log(jpeg.BaseName()))
continue
}
labels = append(labels, imageLabels...)
}
// Sort by priority and uncertainty
sort.Sort(labels)
var confidence int
for _, label := range labels {
if confidence == 0 {
confidence = 100 - label.Uncertainty
}
if (100 - label.Uncertainty) > (confidence / 3) {
results = append(results, label)
}
}
if l := len(labels); l == 1 {
log.Infof("index: matched %d label with %s [%s]", l, clean.Log(jpeg.BaseName()), time.Since(start))
} else if l > 1 {
log.Infof("index: matched %d labels with %s [%s]", l, clean.Log(jpeg.BaseName()), time.Since(start))
}
return results
}