Passed
Pull Request — master (#778)
by Matias
08:57 queued 04:20
created

Face::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 7
cts 7
cp 1
crap 1
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 15
		$this->addType('descriptor', 'json');
133 15
		$this->addType('creationTime', 'datetime');
134
	}
135
136
	/**
137
	 * Factory method to create Face from face structure that is returned as output of the model.
138
	 *
139
	 * @param int $image Image Id
140
	 * @param array $faceFromModel Face obtained from DNN model
141
	 * @return Face Created face
142
	 */
143 15
	public static function fromModel(int $imageId, array $faceFromModel): Face {
144 15
		$face = new Face();
145 15
		$face->image      = $imageId;
146 15
		$face->person     = null;
147 15
		$face->x          = $faceFromModel['left'];
148 15
		$face->y          = $faceFromModel['top'];
149 15
		$face->width      = $faceFromModel['right'] - $faceFromModel['left'];
150 15
		$face->height     = $faceFromModel['bottom'] - $faceFromModel['top'];
151 15
		$face->confidence = $faceFromModel['detection_confidence'];
152 15
		$face->landmarks  = isset($faceFromModel['landmarks']) ? $faceFromModel['landmarks'] : [];
153 15
		$face->descriptor = isset($faceFromModel['descriptor']) ? $faceFromModel['descriptor'] : [];
154 15
		$face->setCreationTime(new \DateTime());
155 15
		return $face;
156
	}
157
158
	public function jsonSerialize() {
159
		return [
160
			'id' => $this->id,
161
			'image' => $this->image,
162
			'person' => $this->person,
163
			'x' => $this->x,
164
			'y' => $this->y,
165
			'width' => $this->width,
166
			'height' => $this->height,
167
			'confidence' => $this->confidence,
168
			'is_groupable' => $this->isGroupable,
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
	public function setLandmarks($landmarks): void {
180
		$this->landmarks = json_decode($landmarks);
181
		$this->markFieldUpdated('landmarks');
182
	}
183
184
	public function getDescriptor(): string {
185
		return json_encode($this->descriptor);
186
	}
187
188
	public function setDescriptor($descriptor): void {
189
		$this->descriptor = json_decode($descriptor);
190
		$this->markFieldUpdated('descriptor');
191
	}
192
193 15
	public function setCreationTime($creationTime): void {
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
}