2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
import (
|
2020-04-16 20:57:00 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/classify"
|
2019-06-04 18:26:35 +02:00
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// PhotoLabel represents the many-to-many relation between Photo and label.
|
|
|
|
// Labels are weighted by uncertainty (100 - confidence)
|
2019-06-04 18:26:35 +02:00
|
|
|
type PhotoLabel struct {
|
2020-04-19 01:13:55 +02: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 18:26:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// TableName returns PhotoLabel table identifier "photos_labels"
|
2019-06-04 18:26:35 +02:00
|
|
|
func (PhotoLabel) TableName() string {
|
2019-06-09 04:37:02 +02:00
|
|
|
return "photos_labels"
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01: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 18:26:35 +02:00
|
|
|
result := &PhotoLabel{
|
2020-04-19 01:13:55 +02:00
|
|
|
PhotoID: photoID,
|
|
|
|
LabelID: labelID,
|
|
|
|
Uncertainty: uncertainty,
|
|
|
|
LabelSrc: source,
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// Updates multiple columns in the database.
|
|
|
|
func (m *PhotoLabel) Updates(values interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumns(values).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates a column in the database.
|
|
|
|
func (m *PhotoLabel) Update(attr string, value interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save saves the entity in the database.
|
|
|
|
func (m *PhotoLabel) Save() error {
|
|
|
|
if m.Photo != nil {
|
|
|
|
m.Photo = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Label != nil {
|
|
|
|
m.Label.SetName(m.Label.LabelName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *PhotoLabel) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreatePhotoLabel returns the existing row, inserts a new row or nil in case of errors.
|
|
|
|
func FirstOrCreatePhotoLabel(m *PhotoLabel) *PhotoLabel {
|
|
|
|
result := PhotoLabel{}
|
|
|
|
|
|
|
|
if err := Db().Where("photo_id = ? AND label_id = ?", m.PhotoID, m.LabelID).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else if err := m.Create(); err != nil {
|
2020-06-29 13:16:55 +02:00
|
|
|
// Already exists.
|
2020-05-26 11:00:39 +02:00
|
|
|
return nil
|
2019-12-19 09:37:10 +01:00
|
|
|
}
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
2020-04-16 20:57:00 +02:00
|
|
|
|
|
|
|
// ClassifyLabel returns the label as classify.Label
|
|
|
|
func (m *PhotoLabel) ClassifyLabel() classify.Label {
|
|
|
|
if m.Label == nil {
|
2020-05-26 11:00:39 +02:00
|
|
|
log.Errorf("photo-label: classify label is nil (photo id %d, label id %d) - bug?", m.PhotoID, m.LabelID)
|
|
|
|
return classify.Label{}
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result := classify.Label{
|
2020-04-16 23:16:55 +02:00
|
|
|
Name: m.Label.LabelName,
|
2020-04-19 01:13:55 +02:00
|
|
|
Source: m.LabelSrc,
|
|
|
|
Uncertainty: m.Uncertainty,
|
2020-04-16 23:16:55 +02:00
|
|
|
Priority: m.Label.LabelPriority,
|
2020-04-16 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|