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 void setImage(int $image) |
39
|
|
|
* @method void setPerson(int $person) |
40
|
|
|
* @method void setLeft(int $left) |
41
|
|
|
* @method void setRight(int $right) |
42
|
|
|
* @method void setTop(int $top) |
43
|
|
|
* @method void setBottom(int $bottom) |
44
|
|
|
*/ |
45
|
|
|
class Face extends Entity implements JsonSerializable { |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Image from this face originated from. |
49
|
|
|
* |
50
|
|
|
* @var int |
51
|
|
|
* */ |
52
|
|
|
public $image; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Person (cluster) that this face belongs to |
56
|
|
|
* |
57
|
|
|
* @var int|null |
58
|
|
|
* */ |
59
|
|
|
public $person; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Left border of bounding rectangle for this face |
63
|
|
|
* |
64
|
|
|
* @var int |
65
|
|
|
* */ |
66
|
|
|
public $left; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Right border of bounding rectangle for this face |
70
|
|
|
* |
71
|
|
|
* @var int |
72
|
|
|
* */ |
73
|
|
|
public $right; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Top border of bounding rectangle for this face |
77
|
|
|
* |
78
|
|
|
* @var int |
79
|
|
|
* */ |
80
|
|
|
public $top; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Bottom border of bounding rectangle for this face |
84
|
|
|
* |
85
|
|
|
* @var int |
86
|
|
|
* */ |
87
|
|
|
public $bottom; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 128D face descriptor for this face. |
91
|
|
|
* |
92
|
|
|
* @var array |
93
|
|
|
* */ |
94
|
|
|
public $descriptor; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Time when this face was found |
98
|
|
|
* |
99
|
|
|
* @var \DateTime |
100
|
|
|
* */ |
101
|
|
|
public $creationTime; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Factory method to create Face from face structure that is returned as output of the model. |
105
|
|
|
* |
106
|
|
|
* @param int $image Image Id |
107
|
|
|
* @param array $faceFromModel Face obtained from DNN model |
108
|
|
|
* @return Face Created face |
109
|
|
|
*/ |
110
|
15 |
|
public static function fromModel(int $image, array $faceFromModel): Face { |
111
|
15 |
|
$face = new Face(); |
112
|
15 |
|
$face->setImage($image); |
113
|
15 |
|
$face->setPerson(null); |
114
|
15 |
|
$face->setLeft(max($faceFromModel["left"], 0)); |
115
|
15 |
|
$face->setRight($faceFromModel["right"]); |
116
|
15 |
|
$face->setTop(max($faceFromModel["top"], 0)); |
117
|
15 |
|
$face->setBottom($faceFromModel["bottom"]); |
118
|
15 |
|
$face->setDescriptor("[]"); |
119
|
15 |
|
$face->setCreationTime(new \DateTime()); |
120
|
15 |
|
return $face; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Helper method, to normalize face sizes back to original dimensions, based on ratio |
125
|
|
|
* |
126
|
|
|
* @param float $ratio Ratio of image resize |
127
|
|
|
*/ |
128
|
1 |
|
public function normalizeSize($ratio) { |
129
|
1 |
|
$this->left = intval(round($this->left * $ratio)); |
130
|
1 |
|
$this->right = intval(round($this->right * $ratio)); |
131
|
1 |
|
$this->top = intval(round($this->top * $ratio)); |
132
|
1 |
|
$this->bottom = intval(round($this->bottom * $ratio)); |
133
|
1 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Gets face width |
137
|
|
|
* |
138
|
|
|
* @return int Face width |
139
|
|
|
*/ |
140
|
|
|
public function width(): int { |
141
|
|
|
return $this->right - $this->left; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Gets face height |
146
|
|
|
* |
147
|
|
|
* @return int Face height |
148
|
|
|
*/ |
149
|
|
|
public function height(): int { |
150
|
|
|
return $this->bottom - $this->top; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function jsonSerialize() { |
154
|
|
|
return [ |
155
|
|
|
'id' => $this->id, |
156
|
|
|
'image' => $this->image, |
157
|
|
|
'person' => $this->person, |
158
|
|
|
'left' => $this->left, |
159
|
|
|
'right' => $this->right, |
160
|
|
|
'top' => $this->top, |
161
|
|
|
'bottom' => $this->bottom, |
162
|
|
|
'descriptor' => $this->descriptor, |
163
|
|
|
'creation_time' => $this->creationTime |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getDescriptor(): string { |
168
|
|
|
return json_encode($this->descriptor); |
169
|
|
|
} |
170
|
|
|
|
171
|
15 |
|
public function setDescriptor($descriptor) { |
172
|
15 |
|
$this->descriptor = json_decode($descriptor); |
173
|
15 |
|
$this->markFieldUpdated('descriptor'); |
174
|
15 |
|
} |
175
|
|
|
|
176
|
15 |
|
public function setCreationTime($creationTime) { |
177
|
15 |
|
if (is_a($creationTime, 'DateTime')) { |
178
|
15 |
|
$this->creationTime = $creationTime; |
179
|
|
|
} else { |
180
|
|
|
$this->creationTime = new \DateTime($creationTime); |
181
|
|
|
} |
182
|
15 |
|
$this->markFieldUpdated('creationTime'); |
183
|
|
|
} |
184
|
|
|
} |