2019-12-11 15:55:18 +00:00
|
|
|
package entity
|
2019-06-04 16:26:35 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-16 18:57:00 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
2019-06-04 16:26:35 +00:00
|
|
|
)
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// PhotoLabel represents the many-to-many relation between Photo and label.
|
|
|
|
// Labels are weighted by uncertainty (100 - confidence)
|
2019-06-04 16:26:35 +00:00
|
|
|
type PhotoLabel struct {
|
2020-04-18 23:13:55 +00:00
|
|
|
PhotoID uint `gorm:"primary_key;auto_increment:false"`
|
|
|
|
LabelID uint `gorm:"primary_key;auto_increment:false;index"`
|
|
|
|
LabelSrc string `gorm:"type:varbinary(8);"`
|
|
|
|
Uncertainty int `gorm:"type:SMALLINT"`
|
|
|
|
Photo *Photo `gorm:"PRELOAD:false"`
|
|
|
|
Label *Label `gorm:"PRELOAD:true"`
|
2019-06-04 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// TableName returns PhotoLabel table identifier "photos_labels"
|
2019-06-04 16:26:35 +00:00
|
|
|
func (PhotoLabel) TableName() string {
|
2019-06-09 02:37:02 +00:00
|
|
|
return "photos_labels"
|
2019-06-04 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// NewPhotoLabel registers a new PhotoLabel relation with an uncertainty and a source of label
|
|
|
|
func NewPhotoLabel(photoID, labelID uint, uncertainty int, source string) *PhotoLabel {
|
2019-06-04 16:26:35 +00:00
|
|
|
result := &PhotoLabel{
|
2020-04-18 23:13:55 +00:00
|
|
|
PhotoID: photoID,
|
|
|
|
LabelID: labelID,
|
|
|
|
Uncertainty: uncertainty,
|
|
|
|
LabelSrc: source,
|
2019-06-04 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:20:38 +00:00
|
|
|
// FirstOrCreate checks if the PhotoLabel relation already exist in the database before the creation
|
2020-04-30 18:07:03 +00:00
|
|
|
func (m *PhotoLabel) FirstOrCreate() *PhotoLabel {
|
|
|
|
if err := Db().FirstOrCreate(m, "photo_id = ? AND label_id = ?", m.PhotoID, m.LabelID).Error; err != nil {
|
2019-12-19 08:37:10 +00:00
|
|
|
log.Errorf("photo label: %s", err)
|
|
|
|
}
|
2019-06-04 16:26:35 +00:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
2020-04-16 18:57:00 +00:00
|
|
|
|
|
|
|
// ClassifyLabel returns the label as classify.Label
|
|
|
|
func (m *PhotoLabel) ClassifyLabel() classify.Label {
|
|
|
|
if m.Label == nil {
|
|
|
|
panic("photo label: label is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
result := classify.Label{
|
2020-04-16 21:16:55 +00:00
|
|
|
Name: m.Label.LabelName,
|
2020-04-18 23:13:55 +00:00
|
|
|
Source: m.LabelSrc,
|
|
|
|
Uncertainty: m.Uncertainty,
|
2020-04-16 21:16:55 +00:00
|
|
|
Priority: m.Label.LabelPriority,
|
2020-04-16 18:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2020-04-17 19:20:38 +00:00
|
|
|
|
|
|
|
// Save saves the entity in the database and returns an error.
|
2020-04-30 18:07:03 +00:00
|
|
|
func (m *PhotoLabel) Save() error {
|
2020-04-17 19:20:38 +00:00
|
|
|
if m.Photo != nil {
|
|
|
|
m.Photo = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Label != nil {
|
2020-04-26 12:31:33 +00:00
|
|
|
m.Label.SetName(m.Label.LabelName)
|
2020-04-17 19:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 18:07:03 +00:00
|
|
|
return Db().Save(m).Error
|
2020-04-17 19:20:38 +00:00
|
|
|
}
|