Passed
Push — self-contained-model ( dd9490...1c4630 )
by Matias
04:08
created

Face::normalizeSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
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 14
	public static function fromModel(int $image, array $faceFromModel): Face {
127 14
		$face = new Face();
128 14
		$face->setImage($image);
129 14
		$face->setPerson(null);
130 14
		$face->setLeft(max($faceFromModel["left"], 0));
131 14
		$face->setRight($faceFromModel["right"]);
132 14
		$face->setTop(max($faceFromModel["top"], 0));
133 14
		$face->setBottom($faceFromModel["bottom"]);
134 14
		$face->setConfidence($faceFromModel["detection_confidence"]);
135 14
		$face->setLandmarks("[]");
136 14
		$face->setDescriptor("[]");
137 14
		$face->setCreationTime(new \DateTime());
138 14
		return $face;
139
	}
140
141
	/**
142
	 * Helper method, to normalize face sizes back to original dimensions, based on ratio
143
	 *
144
	 * @param float $ratio Ratio of image resize
145
	 */
146
	public function normalizeSize($ratio) {
147
		$this->left =   intval(round($this->left * $ratio));
148
		$this->right =  intval(round($this->right * $ratio));
149
		$this->top =    intval(round($this->top * $ratio));
150
		$this->bottom = intval(round($this->bottom * $ratio));
151
	}
152
153
	/**
154
	 * Gets face width
155
	 *
156
	 * @return int Face width
157
	 */
158
	public function width(): int {
159
		return $this->right - $this->left;
160
	}
161
162
	/**
163
	 * Gets face height
164
	 *
165
	 * @return int Face height
166
	 */
167
	public function height(): int {
168
		return $this->bottom - $this->top;
169
	}
170
171
	public function jsonSerialize() {
172
		return [
173
			'id' => $this->id,
174
			'image' => $this->image,
175
			'person' => $this->person,
176
			'left' => $this->left,
177
			'right' => $this->right,
178
			'top' => $this->top,
179
			'bottom' => $this->bottom,
180
			'confidence' => $this->confidence,
181
			'landmarks' => $this->landmarks,
182
			'descriptor' => $this->descriptor,
183
			'creation_time' => $this->creationTime
184
		];
185
	}
186
187
	public function getLandmarks(): string {
188
		return json_encode($this->landmarks);
189
	}
190
191 14
	public function setLandmarks($landmarks) {
192 14
		$this->landmarks = json_decode($landmarks);
193 14
		$this->markFieldUpdated('landmarks');
194 14
	}
195
196
	public function getDescriptor(): string {
197
		return json_encode($this->descriptor);
198
	}
199
200 14
	public function setDescriptor($descriptor) {
201 14
		$this->descriptor = json_decode($descriptor);
202 14
		$this->markFieldUpdated('descriptor');
203 14
	}
204
205 14
	public function setCreationTime($creationTime) {
206 14
		if (is_a($creationTime, 'DateTime')) {
207 14
			$this->creationTime = $creationTime;
208
		} else {
209
			$this->creationTime = new \DateTime($creationTime);
210
		}
211 14
		$this->markFieldUpdated('creationTime');
212
	}
213
}