1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017, Matias De lellis <[email protected]> |
4
|
|
|
* @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Branko Kokanovic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
namespace OCA\FaceRecognition\Db; |
25
|
|
|
|
26
|
|
|
use JsonSerializable; |
27
|
|
|
|
28
|
|
|
use OCP\AppFramework\Db\Entity; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Face represents one found face from one image. |
32
|
|
|
* |
33
|
|
|
* @method int getImage() |
34
|
|
|
* @method int getLeft() |
35
|
|
|
* @method int getRight() |
36
|
|
|
* @method int getTop() |
37
|
|
|
* @method int getBottom() |
38
|
|
|
* @method float getConfidence() |
39
|
|
|
* @method void setImage(int $image) |
40
|
|
|
* @method void setPerson(int $person) |
41
|
|
|
* @method void setLeft(int $left) |
42
|
|
|
* @method void setRight(int $right) |
43
|
|
|
* @method void setTop(int $top) |
44
|
|
|
* @method void setBottom(int $bottom) |
45
|
|
|
* @method void setConfidence(float $confidence) |
46
|
|
|
*/ |
47
|
|
|
class Face extends Entity implements JsonSerializable { |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Image from this face originated from. |
51
|
|
|
* |
52
|
|
|
* @var int |
53
|
|
|
* */ |
54
|
|
|
public $image; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Person (cluster) that this face belongs to |
58
|
|
|
* |
59
|
|
|
* @var int|null |
60
|
|
|
* */ |
61
|
|
|
public $person; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Left border of bounding rectangle for this face |
65
|
|
|
* |
66
|
|
|
* @var int |
67
|
|
|
* */ |
68
|
|
|
public $left; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Right border of bounding rectangle for this face |
72
|
|
|
* |
73
|
|
|
* @var int |
74
|
|
|
* */ |
75
|
|
|
public $right; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Top border of bounding rectangle for this face |
79
|
|
|
* |
80
|
|
|
* @var int |
81
|
|
|
* */ |
82
|
|
|
public $top; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Bottom border of bounding rectangle for this face |
86
|
|
|
* |
87
|
|
|
* @var int |
88
|
|
|
* */ |
89
|
|
|
public $bottom; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Confidence of face detection obtained from the model |
93
|
|
|
* |
94
|
|
|
* @var float |
95
|
|
|
* */ |
96
|
|
|
public $confidence; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* landmarks for this face. |
100
|
|
|
* |
101
|
|
|
* @var array |
102
|
|
|
* */ |
103
|
|
|
public $landmarks; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* 128D face descriptor for this face. |
107
|
|
|
* |
108
|
|
|
* @var array |
109
|
|
|
* */ |
110
|
|
|
public $descriptor; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Time when this face was found |
114
|
|
|
* |
115
|
|
|
* @var \DateTime |
116
|
|
|
* */ |
117
|
|
|
public $creationTime; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Factory method to create Face from face structure that is returned as output of the model. |
121
|
|
|
* |
122
|
|
|
* @param int $image Image Id |
123
|
|
|
* @param array $faceFromModel Face obtained from DNN model |
124
|
|
|
* @return Face Created face |
125
|
|
|
*/ |
126
|
15 |
|
public static function fromModel(int $image, array $faceFromModel): Face { |
127
|
15 |
|
$face = new Face(); |
128
|
15 |
|
$face->setImage($image); |
129
|
15 |
|
$face->setPerson(null); |
130
|
15 |
|
$face->setLeft(max($faceFromModel["left"], 0)); |
131
|
15 |
|
$face->setRight($faceFromModel["right"]); |
132
|
15 |
|
$face->setTop(max($faceFromModel["top"], 0)); |
133
|
15 |
|
$face->setBottom($faceFromModel["bottom"]); |
134
|
15 |
|
$face->setConfidence($faceFromModel["detection_confidence"]); |
135
|
15 |
|
$face->setLandmarks("[]"); |
136
|
15 |
|
$face->setDescriptor("[]"); |
137
|
15 |
|
$face->setCreationTime(new \DateTime()); |
138
|
15 |
|
return $face; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Gets face width |
143
|
|
|
* |
144
|
|
|
* @return int Face width |
145
|
|
|
*/ |
146
|
1 |
|
public function width(): int { |
147
|
1 |
|
return $this->right - $this->left; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Gets face height |
152
|
|
|
* |
153
|
|
|
* @return int Face height |
154
|
|
|
*/ |
155
|
1 |
|
public function height(): int { |
156
|
1 |
|
return $this->bottom - $this->top; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function jsonSerialize() { |
160
|
|
|
return [ |
161
|
|
|
'id' => $this->id, |
162
|
|
|
'image' => $this->image, |
163
|
|
|
'person' => $this->person, |
164
|
|
|
'left' => $this->left, |
165
|
|
|
'right' => $this->right, |
166
|
|
|
'top' => $this->top, |
167
|
|
|
'bottom' => $this->bottom, |
168
|
|
|
'confidence' => $this->confidence, |
169
|
|
|
'landmarks' => $this->landmarks, |
170
|
|
|
'descriptor' => $this->descriptor, |
171
|
|
|
'creation_time' => $this->creationTime |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getLandmarks(): string { |
176
|
|
|
return json_encode($this->landmarks); |
177
|
|
|
} |
178
|
|
|
|
179
|
15 |
|
public function setLandmarks($landmarks) { |
180
|
15 |
|
$this->landmarks = json_decode($landmarks); |
181
|
15 |
|
$this->markFieldUpdated('landmarks'); |
182
|
15 |
|
} |
183
|
|
|
|
184
|
|
|
public function getDescriptor(): string { |
185
|
|
|
return json_encode($this->descriptor); |
186
|
|
|
} |
187
|
|
|
|
188
|
15 |
|
public function setDescriptor($descriptor) { |
189
|
15 |
|
$this->descriptor = json_decode($descriptor); |
190
|
15 |
|
$this->markFieldUpdated('descriptor'); |
191
|
15 |
|
} |
192
|
|
|
|
193
|
15 |
|
public function setCreationTime($creationTime) { |
194
|
15 |
|
if (is_a($creationTime, 'DateTime')) { |
195
|
15 |
|
$this->creationTime = $creationTime; |
196
|
|
|
} else { |
197
|
|
|
$this->creationTime = new \DateTime($creationTime); |
198
|
|
|
} |
199
|
15 |
|
$this->markFieldUpdated('creationTime'); |
200
|
|
|
} |
201
|
|
|
} |