Passed
Push — dependabot/npm_and_yarn/minimi... ( f33d6d )
by
unknown
11:45
created

Face   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Test Coverage

Coverage 64%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 54
c 2
b 0
f 0
dl 0
loc 168
ccs 32
cts 50
cp 0.64
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getLandmarks() 0 2 1
A width() 0 2 1
A setDescriptor() 0 3 1
A jsonSerialize() 0 14 1
A fromModel() 0 13 3
A setCreationTime() 0 7 2
A height() 0 2 1
A setLandmarks() 0 3 1
A __construct() 0 5 1
A getDescriptor() 0 2 1
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 getLeft()
36
 * @method int getRight()
37
 * @method int getTop()
38
 * @method int getBottom()
39
 * @method float getConfidence()
40
 * @method void setImage(int $image)
41
 * @method void setPerson(int $person)
42
 * @method void setLeft(int $left)
43
 * @method void setRight(int $right)
44
 * @method void setTop(int $top)
45
 * @method void setBottom(int $bottom)
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 $left;
70
71
	/**
72
	 * Right border of bounding rectangle for this face
73
	 *
74
	 * @var int
75
	 * */
76
	public $right;
77
78
	/**
79
	 * Top border of bounding rectangle for this face
80
	 *
81
	 * @var int
82
	 * */
83
	public $top;
84
85
	/**
86
	 * Bottom border of bounding rectangle for this face
87
	 *
88
	 * @var int
89
	 * */
90
	public $bottom;
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->left       = $faceFromModel['left'];
146 15
		$face->right      = $faceFromModel['right'];
147 15
		$face->top        = $faceFromModel['top'];
148 15
		$face->bottom     = $faceFromModel['bottom'];
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
	/**
157
	 * Gets face width
158
	 *
159
	 * @return int Face width
160
	 */
161 1
	public function width(): int {
162 1
		return $this->right - $this->left;
163
	}
164
165
	/**
166
	 * Gets face height
167
	 *
168
	 * @return int Face height
169
	 */
170 1
	public function height(): int {
171 1
		return $this->bottom - $this->top;
172
	}
173
174
	public function jsonSerialize() {
175
		return [
176
			'id' => $this->id,
177
			'image' => $this->image,
178
			'person' => $this->person,
179
			'left' => $this->left,
180
			'right' => $this->right,
181
			'top' => $this->top,
182
			'bottom' => $this->bottom,
183
			'confidence' => $this->confidence,
184
			'is_groupable' => $this->isGroupable,
185
			'landmarks' => $this->landmarks,
186
			'descriptor' => $this->descriptor,
187
			'creation_time' => $this->creationTime
188
		];
189
	}
190
191
	public function getLandmarks(): string {
192
		return json_encode($this->landmarks);
193
	}
194
195 1
	public function setLandmarks($landmarks): void {
196 1
		$this->landmarks = json_decode($landmarks);
197 1
		$this->markFieldUpdated('landmarks');
198
	}
199
200
	public function getDescriptor(): string {
201
		return json_encode($this->descriptor);
202
	}
203
204 14
	public function setDescriptor($descriptor): void {
205 14
		$this->descriptor = json_decode($descriptor);
206 14
		$this->markFieldUpdated('descriptor');
207
	}
208
209 15
	public function setCreationTime($creationTime): void {
210 15
		if (is_a($creationTime, 'DateTime')) {
211 15
			$this->creationTime = $creationTime;
212
		} else {
213
			$this->creationTime = new \DateTime($creationTime);
214
		}
215 15
		$this->markFieldUpdated('creationTime');
216
	}
217
}