photoprism/internal/face/area.go

67 lines
1.4 KiB
Go
Raw Normal View History

package face
import (
"fmt"
2021-09-02 23:47:37 +02:00
"github.com/photoprism/photoprism/internal/crop"
)
// Areas is a list of face landmark areas.
type Areas []Area
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) {
for _, p := range pts {
2021-09-02 23:47:37 +02:00
result = append(result, p.Relative(r, rows, cols))
}
2021-09-02 23:47:37 +02:00
return result
}
// Area represents a face landmark position.
type Area struct {
Name string `json:"name,omitempty"`
Row int `json:"x,omitempty"`
Col int `json:"y,omitempty"`
Scale int `json:"size,omitempty"`
}
// String returns the face landmark position as string.
func (a Area) String() string {
return fmt.Sprintf("%d-%d-%d", a.Row, a.Col, a.Scale)
}
// NewArea returns new face landmark coordinates.
func NewArea(name string, row, col, scale int) Area {
return Area{
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 {
if rows < 1 {
rows = 1
}
if cols < 1 {
cols = 1
}
2021-09-02 23:47:37 +02:00
return crop.NewArea(
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-09-20 22:19:54 +02:00
// TopLeft returns the top left position of the area.
func (a Area) TopLeft() (int, int) {
return a.Row - (a.Scale / 2), a.Col - (a.Scale / 2)
}