2021-05-25 14:30:24 +02:00
|
|
|
package face
|
|
|
|
|
2021-08-11 13:21:05 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-02 23:47:37 +02:00
|
|
|
|
2021-09-05 11:05:33 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/crop"
|
2021-08-11 13:21:05 +02:00
|
|
|
)
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// Areas is a list of face landmark areas.
|
|
|
|
type Areas []Area
|
2021-05-25 14:30:24 +02:00
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
// Relative returns all areas with relative coordinates.
|
|
|
|
func (pts Areas) Relative(r Area, rows, cols float32) (result crop.Areas) {
|
2021-05-25 14:30:24 +02:00
|
|
|
for _, p := range pts {
|
2021-09-02 23:47:37 +02:00
|
|
|
result = append(result, p.Relative(r, rows, cols))
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
return result
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// Area represents a face landmark position.
|
|
|
|
type Area struct {
|
2021-05-25 14:30:24 +02:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Row int `json:"x,omitempty"`
|
|
|
|
Col int `json:"y,omitempty"`
|
|
|
|
Scale int `json:"size,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-08-11 13:21:05 +02:00
|
|
|
// String returns the face landmark position as string.
|
2021-09-01 20:46:15 +02:00
|
|
|
func (a Area) String() string {
|
|
|
|
return fmt.Sprintf("%d-%d-%d", a.Row, a.Col, a.Scale)
|
2021-08-11 13:21:05 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
// NewArea returns new face landmark coordinates.
|
|
|
|
func NewArea(name string, row, col, scale int) Area {
|
|
|
|
return Area{
|
2021-05-25 14:30:24 +02:00
|
|
|
Name: name,
|
|
|
|
Row: row,
|
|
|
|
Col: col,
|
|
|
|
Scale: scale,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
// Relative returns the area with relative coordinates.
|
|
|
|
func (a Area) Relative(r Area, rows, cols float32) crop.Area {
|
2021-06-01 17:39:03 +02:00
|
|
|
if rows < 1 {
|
|
|
|
rows = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if cols < 1 {
|
|
|
|
cols = 1
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
return crop.NewArea(
|
2021-09-01 20:46:15 +02:00
|
|
|
a.Name,
|
|
|
|
float32(a.Col-r.Col)/cols,
|
|
|
|
float32(a.Row-r.Row)/rows,
|
|
|
|
float32(a.Scale)/cols,
|
2021-09-02 23:47:37 +02:00
|
|
|
float32(a.Scale)/rows,
|
2021-05-25 14:30:24 +02:00
|
|
|
)
|
|
|
|
}
|
2021-07-16 14:34:05 +02:00
|
|
|
|
2021-09-20 22:19:54 +02:00
|
|
|
// TopLeft returns the top left position of the area.
|
2021-09-01 20:46:15 +02:00
|
|
|
func (a Area) TopLeft() (int, int) {
|
|
|
|
return a.Row - (a.Scale / 2), a.Col - (a.Scale / 2)
|
2021-07-16 14:34:05 +02:00
|
|
|
}
|