Passed
Push — face-alignment ( 98149b...bc6ca5 )
by Matias
04:31
created

Face::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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