-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
face.go
147 lines (117 loc) Β· 3.59 KB
/
face.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Package face provides face recognition.
Copyright (c) 2018 - 2024 PhotoPrism UG. All rights reserved.
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>
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.
The AGPL is supplemented by our Trademark and Brand Guidelines,
which describe how our Brand Assets may be used:
<https://www.photoprism.app/trademark>
Feel free to send an email to hello@photoprism.app if you have questions,
want to support our work, or just want to say hello.
Additional information can be found in our Developer Guide:
<https://docs.photoprism.app/developer-guide/>
*/
package face
import (
"encoding/json"
"github.com/photoprism/photoprism/internal/crop"
"github.com/photoprism/photoprism/internal/event"
)
var log = event.Log
// Face represents a face detected.
type Face struct {
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"`
}
// Size returns the absolute face size in pixels.
func (f *Face) Size() int {
return f.Area.Scale
}
// 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)
}
return float32(1)
}
// CropArea returns the relative image area for cropping.
func (f *Face) CropArea() crop.Area {
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)
return crop.NewArea(
f.Area.Name,
x,
y,
float32(f.Area.Scale)/float32(f.Cols),
float32(f.Area.Scale)/float32(f.Rows),
)
}
// EyesMidpoint returns the point in between the eyes.
func (f *Face) EyesMidpoint() Area {
if len(f.Eyes) != 2 {
return Area{
Name: "midpoint",
Row: f.Area.Row,
Col: f.Area.Col,
Scale: f.Area.Scale,
}
}
return Area{
Name: "midpoint",
Row: (f.Eyes[0].Row + f.Eyes[1].Row) / 2,
Col: (f.Eyes[0].Col + f.Eyes[1].Col) / 2,
Scale: (f.Eyes[0].Scale + f.Eyes[1].Scale) / 2,
}
}
// RelativeLandmarks returns relative face areas.
func (f *Face) RelativeLandmarks() crop.Areas {
p := f.EyesMidpoint()
m := f.Landmarks.Relative(p, float32(f.Rows), float32(f.Cols))
m = append(m, f.Eyes.Relative(p, float32(f.Rows), float32(f.Cols))...)
return m
}
// RelativeLandmarksJSON returns relative face areas as JSON.
func (f *Face) RelativeLandmarksJSON() (b []byte) {
var noResult = []byte("")
l := f.RelativeLandmarks()
if len(l) < 1 {
return noResult
}
if result, err := json.Marshal(l); err != nil {
log.Errorf("faces: %s", err)
return noResult
} else {
return result
}
}
// EmbeddingsJSON returns detected face embeddings as JSON array.
func (f *Face) EmbeddingsJSON() (b []byte) {
return f.Embeddings.JSON()
}
// 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
}
return f.Embeddings.Empty()
}