Passed
Pull Request — master (#778)
by Matias
05:58
created

Face::setLandmarks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017-2021 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 getPerson()
35
 * @method int getX()
36
 * @method int getY()
37
 * @method int getWidth()
38
 * @method int getHeight()
39
 * @method float getConfidence()
40
 * @method void setImage(int $image)
41
 * @method void setPerson(int $person)
42
 * @method void setX(int $x)
43
 * @method void setY(int $y)
44
 * @method void setWidth(int $width)
45
 * @method void setHeight(int $height)
46
 * @method void setConfidence(float $confidence)
47
 */
48
class Face extends Entity implements JsonSerializable {
49
50
	/**
51
	 * Image from this face originated from.
52
	 *
53
	 * @var int
54
	 * */
55
	public $image;
56
57
	/**
58
	 * Person (cluster) that this face belongs to
59
	 *
60
	 * @var int|null
61
	 * */
62
	public $person;
63
64
	/**
65
	 * Left border of bounding rectangle for this face
66
	 *
67
	 * @var int
68
	 * */
69
	public $x;
70
71
	/**
72
	 * Top border of bounding rectangle for this face
73
	 *
74
	 * @var int
75
	 * */
76
	public $y;
77
78
	/**
79
	 * Width of this face from the left border
80
	 *
81
	 * @var int
82
	 * */
83
	public $width;
84
85
	/**
86
	 * Height of this face from top border
87
	 *
88
	 * @var int
89
	 * */
90
	public $height;
91
92
	/**
93
	 * Confidence of face detection obtained from the model
94
	 *
95
	 * @var float
96
	 * */
97
	public $confidence;
98
99
	/**
100
	 * If it can be grouped according to the configurations
101
	 *
102
	 * @var bool
103
	 **/
104
	public $isGroupable;
105
106
	/**
107
	 * landmarks for this face.
108
	 *
109
	 * @var array
110
	 * */
111
	public $landmarks;
112
113
	/**
114
	 * 128D face descriptor for this face.
115
	 *
116
	 * @var array
117
	 * */
118
	public $descriptor;
119
120
	/**
121
	 * Time when this face was found
122
	 *
123
	 * @var \DateTime
124
	 * */
125
	public $creationTime;
126
127 15
	public function __construct() {
128 15
		$this->addType('id', 'integer');
129 15
		$this->addType('image', 'integer');
130 15
		$this->addType('person', 'integer');
131 15
		$this->addType('isGroupable', 'bool');
132
	}
133
134
	/**
135
	 * Factory method to create Face from face structure that is returned as output of the model.
136
	 *
137
	 * @param int $image Image Id
138
	 * @param array $faceFromModel Face obtained from DNN model
139
	 * @return Face Created face
140
	 */
141 15
	public static function fromModel(int $imageId, array $faceFromModel): Face {
142 15
		$face = new Face();
143 15
		$face->image      = $imageId;
144 15
		$face->person     = null;
145 15
		$face->x          = $faceFromModel['left'];
146 15
		$face->y          = $faceFromModel['top'];
147 15
		$face->width      = $faceFromModel['right'] - $faceFromModel['left'];
148 15
		$face->height     = $faceFromModel['bottom'] - $faceFromModel['top'];
149 15
		$face->confidence = $faceFromModel['detection_confidence'];
150 15
		$face->landmarks  = isset($faceFromModel['landmarks']) ? $faceFromModel['landmarks'] : [];
151 15
		$face->descriptor = isset($faceFromModel['descriptor']) ? $faceFromModel['descriptor'] : [];
152 15
		$face->setCreationTime(new \DateTime());
153 15
		return $face;
154
	}
155
156
	public function jsonSerialize() {
157
		return [
158
			'id' => $this->id,
159
			'image' => $this->image,
160
			'person' => $this->person,
161
			'x' => $this->x,
162
			'y' => $this->y,
163
			'width' => $this->width,
164
			'height' => $this->height,
165
			'confidence' => $this->confidence,
166
			'is_groupable' => $this->isGroupable,
167
			'landmarks' => $this->landmarks,
168
			'descriptor' => $this->descriptor,
169
			'creation_time' => $this->creationTime
170
		];
171
	}
172
173
	public function getLandmarks(): string {
174
		return json_encode($this->landmarks);
175
	}
176
177
	public function setLandmarks($landmarks): void {
178
		$this->landmarks = json_decode($landmarks);
179
		$this->markFieldUpdated('landmarks');
180
	}
181
182
	public function getDescriptor(): string {
183
		return json_encode($this->descriptor);
184
	}
185
186
	public function setDescriptor($descriptor): void {
187
		$this->descriptor = json_decode($descriptor);
188
		$this->markFieldUpdated('descriptor');
189
	}
190
191 15
	public function setCreationTime($creationTime): void {
192 15
		if (is_a($creationTime, 'DateTime')) {
193 15
			$this->creationTime = $creationTime;
194
		} else {
195
			$this->creationTime = new \DateTime($creationTime);
196
		}
197 15
		$this->markFieldUpdated('creationTime');
198
	}
199
}