2020-01-09 01:21:09 +01:00
|
|
|
package classify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2020-04-17 21:20:38 +02:00
|
|
|
|
2021-06-02 12:12:30 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/face"
|
|
|
|
|
2020-04-17 21:20:38 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-01-09 01:21:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Label represents a MediaFile label (automatically created).
|
|
|
|
type Label struct {
|
|
|
|
Name string `json:"label"` // Label name
|
|
|
|
Source string `json:"source"` // Where was this label found / detected?
|
|
|
|
Uncertainty int `json:"uncertainty"` // >= 0
|
|
|
|
Priority int `json:"priority"` // >= 0
|
|
|
|
Categories []string `json:"categories"` // List of similar labels
|
|
|
|
}
|
|
|
|
|
2020-07-11 23:43:29 +02:00
|
|
|
// LocationLabel returns a new location label.
|
2021-02-21 17:18:36 +01:00
|
|
|
func LocationLabel(name string, uncertainty int) Label {
|
|
|
|
priority := -1
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
if index := strings.Index(name, " / "); index > 1 {
|
|
|
|
name = name[:index]
|
|
|
|
}
|
|
|
|
|
|
|
|
if index := strings.Index(name, " - "); index > 1 {
|
|
|
|
name = name[:index]
|
|
|
|
}
|
|
|
|
|
2021-02-21 17:18:36 +01:00
|
|
|
var categories []string
|
|
|
|
|
|
|
|
if rule, ok := rules.Find(name); ok {
|
|
|
|
priority = rule.Priority
|
|
|
|
categories = rule.Categories
|
|
|
|
}
|
2020-01-09 01:21:09 +01:00
|
|
|
|
2021-02-21 17:18:36 +01:00
|
|
|
return Label{
|
|
|
|
Name: name,
|
|
|
|
Source: SrcLocation,
|
|
|
|
Uncertainty: uncertainty,
|
|
|
|
Priority: priority,
|
|
|
|
Categories: categories,
|
|
|
|
}
|
2020-01-09 01:21:09 +01:00
|
|
|
}
|
2020-04-17 21:20:38 +02:00
|
|
|
|
|
|
|
// Title returns a formatted label title as string.
|
|
|
|
func (l Label) Title() string {
|
2020-04-26 14:31:33 +02:00
|
|
|
return txt.Title(txt.Clip(l.Name, txt.ClipDefault))
|
2020-04-17 21:20:38 +02:00
|
|
|
}
|
2021-05-25 18:01:21 +02:00
|
|
|
|
|
|
|
// FaceLabels returns matching labels if there are people in the image.
|
2021-05-31 15:40:52 +02:00
|
|
|
func FaceLabels(faces face.Faces, src string) Labels {
|
2021-05-25 18:01:21 +02:00
|
|
|
var r LabelRule
|
|
|
|
|
2021-05-31 15:40:52 +02:00
|
|
|
count := faces.Count()
|
|
|
|
|
2021-05-25 18:01:21 +02:00
|
|
|
if count < 1 {
|
|
|
|
return Labels{}
|
|
|
|
} else if count == 1 {
|
|
|
|
r = rules["portrait"]
|
|
|
|
} else {
|
|
|
|
r = rules["people"]
|
|
|
|
}
|
|
|
|
|
|
|
|
return Labels{Label{
|
|
|
|
Name: r.Label,
|
|
|
|
Source: src,
|
2021-05-31 15:40:52 +02:00
|
|
|
Uncertainty: faces.Uncertainty(),
|
2021-05-25 18:01:21 +02:00
|
|
|
Priority: r.Priority,
|
|
|
|
Categories: r.Categories,
|
|
|
|
}}
|
|
|
|
}
|