2021-05-24 15:54:22 +02:00
|
|
|
/*
|
2022-07-19 16:58:43 +02:00
|
|
|
Package face provides face recognition.
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2023-01-11 16:43:01 +01:00
|
|
|
Copyright (c) 2018 - 2023 PhotoPrism UG. All rights reserved.
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2022-08-10 16:09:21 +02:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under Version 3 of the GNU Affero General Public License (the "AGPL"):
|
|
|
|
<https://docs.photoprism.app/license/agpl>
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2022-08-10 16:09:21 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2022-08-10 16:09:21 +02:00
|
|
|
The AGPL is supplemented by our Trademark and Brand Guidelines,
|
|
|
|
which describe how our Brand Assets may be used:
|
2023-02-08 09:07:42 +01:00
|
|
|
<https://www.photoprism.app/trademark>
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2022-04-13 22:17:59 +02:00
|
|
|
Feel free to send an email to hello@photoprism.app if you have questions,
|
2021-05-24 15:54:22 +02:00
|
|
|
want to support our work, or just want to say hello.
|
|
|
|
|
|
|
|
Additional information can be found in our Developer Guide:
|
2022-02-27 17:32:54 +01:00
|
|
|
<https://docs.photoprism.app/developer-guide/>
|
2021-05-24 15:54:22 +02:00
|
|
|
*/
|
|
|
|
package face
|
|
|
|
|
|
|
|
import (
|
2021-05-25 14:30:24 +02:00
|
|
|
"encoding/json"
|
2021-05-25 14:55:41 +02:00
|
|
|
|
2021-09-05 11:05:33 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/crop"
|
2021-05-24 15:54:22 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
)
|
|
|
|
|
2021-09-20 22:19:54 +02:00
|
|
|
var log = event.Log
|
|
|
|
|
2021-09-21 09:51:48 +02:00
|
|
|
// Face represents a face detected.
|
2021-05-25 14:30:24 +02:00
|
|
|
type Face struct {
|
2021-09-30 13:44:23 +02:00
|
|
|
Rows int `json:"rows,omitempty"`
|
|
|
|
Cols int `json:"cols,omitempty"`
|
|
|
|
Score int `json:"score,omitempty"`
|
|
|
|
Area Area `json:"face,omitempty"`
|
|
|
|
Eyes Areas `json:"eyes,omitempty"`
|
|
|
|
Landmarks Areas `json:"landmarks,omitempty"`
|
|
|
|
Embeddings Embeddings `json:"embeddings,omitempty"`
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2021-08-29 13:26:05 +02:00
|
|
|
// Size returns the absolute face size in pixels.
|
|
|
|
func (f *Face) Size() int {
|
2021-09-02 23:47:37 +02:00
|
|
|
return f.Area.Scale
|
2021-08-29 13:26:05 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
// Dim returns the max number of rows and cols as float32 to calculate relative coordinates.
|
|
|
|
func (f *Face) Dim() float32 {
|
|
|
|
if f.Cols > 0 {
|
|
|
|
return float32(f.Cols)
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
return float32(1)
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-03 16:14:09 +02:00
|
|
|
// CropArea returns the relative image area for cropping.
|
|
|
|
func (f *Face) CropArea() crop.Area {
|
2021-09-02 23:47:37 +02:00
|
|
|
if f.Rows < 1 {
|
|
|
|
f.Cols = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Cols < 1 {
|
|
|
|
f.Cols = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
x := float32(f.Area.Col-f.Area.Scale/2) / float32(f.Cols)
|
|
|
|
y := float32(f.Area.Row-f.Area.Scale/2) / float32(f.Rows)
|
2021-06-01 17:39:03 +02:00
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
return crop.NewArea(
|
|
|
|
f.Area.Name,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
float32(f.Area.Scale)/float32(f.Cols),
|
|
|
|
float32(f.Area.Scale)/float32(f.Rows),
|
|
|
|
)
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
// EyesMidpoint returns the point in between the eyes.
|
2021-09-01 20:46:15 +02:00
|
|
|
func (f *Face) EyesMidpoint() Area {
|
2021-05-25 14:30:24 +02:00
|
|
|
if len(f.Eyes) != 2 {
|
2021-09-01 20:46:15 +02:00
|
|
|
return Area{
|
2021-05-25 14:30:24 +02:00
|
|
|
Name: "midpoint",
|
2021-09-02 23:47:37 +02:00
|
|
|
Row: f.Area.Row,
|
|
|
|
Col: f.Area.Col,
|
|
|
|
Scale: f.Area.Scale,
|
2021-05-25 14:30:24 +02:00
|
|
|
}
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:46:15 +02:00
|
|
|
return Area{
|
2021-05-25 14:30:24 +02:00
|
|
|
Name: "midpoint",
|
|
|
|
Row: (f.Eyes[0].Row + f.Eyes[1].Row) / 2,
|
|
|
|
Col: (f.Eyes[0].Col + f.Eyes[1].Col) / 2,
|
2021-06-01 17:39:03 +02:00
|
|
|
Scale: (f.Eyes[0].Scale + f.Eyes[1].Scale) / 2,
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
// RelativeLandmarks returns relative face areas.
|
|
|
|
func (f *Face) RelativeLandmarks() crop.Areas {
|
2021-05-25 14:30:24 +02:00
|
|
|
p := f.EyesMidpoint()
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
m := f.Landmarks.Relative(p, float32(f.Rows), float32(f.Cols))
|
|
|
|
m = append(m, f.Eyes.Relative(p, float32(f.Rows), float32(f.Cols))...)
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2021-05-25 14:30:24 +02:00
|
|
|
return m
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 23:47:37 +02:00
|
|
|
// RelativeLandmarksJSON returns relative face areas as JSON.
|
2021-05-25 14:30:24 +02:00
|
|
|
func (f *Face) RelativeLandmarksJSON() (b []byte) {
|
2021-06-01 17:39:03 +02:00
|
|
|
var noResult = []byte("")
|
2021-05-24 15:54:22 +02:00
|
|
|
|
2021-06-01 17:39:03 +02:00
|
|
|
l := f.RelativeLandmarks()
|
|
|
|
|
|
|
|
if len(l) < 1 {
|
|
|
|
return noResult
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 17:39:03 +02:00
|
|
|
if result, err := json.Marshal(l); err != nil {
|
|
|
|
log.Errorf("faces: %s", err)
|
|
|
|
return noResult
|
|
|
|
} else {
|
|
|
|
return result
|
|
|
|
}
|
2021-05-24 15:54:22 +02:00
|
|
|
}
|
2021-07-16 14:34:05 +02:00
|
|
|
|
2021-08-11 13:21:05 +02:00
|
|
|
// EmbeddingsJSON returns detected face embeddings as JSON array.
|
|
|
|
func (f *Face) EmbeddingsJSON() (b []byte) {
|
2021-09-30 13:44:23 +02:00
|
|
|
return f.Embeddings.JSON()
|
|
|
|
}
|
2021-07-16 14:34:05 +02:00
|
|
|
|
2021-09-30 13:44:23 +02:00
|
|
|
// HasEmbedding tests if the face has at least one embedding.
|
|
|
|
func (f *Face) HasEmbedding() bool {
|
|
|
|
return len(f.Embeddings) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoEmbedding tests if the face has no embeddings.
|
|
|
|
func (f *Face) NoEmbedding() bool {
|
|
|
|
if f.Embeddings == nil {
|
|
|
|
return true
|
2021-07-16 14:34:05 +02:00
|
|
|
}
|
2021-09-30 13:44:23 +02:00
|
|
|
|
|
|
|
return f.Embeddings.Empty()
|
2021-07-16 14:34:05 +02:00
|
|
|
}
|