Passed
Push — php8 ( 3e2147...190cd5 )
by Matias
10:58 queued 08:41
created

Face::setCreationTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
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
	 * landmarks for this face.
101
	 *
102
	 * @var array
103
	 * */
104
	public $landmarks;
105
106
	/**
107
	 * 128D face descriptor for this face.
108
	 *
109
	 * @var array
110
	 * */
111
	public $descriptor;
112
113
	/**
114
	 * Time when this face was found
115
	 *
116
	 * @var \DateTime
117
	 * */
118
	public $creationTime;
119
120 15
	public function __construct() {
121 15
		$this->addType('id', 'integer');
122 15
		$this->addType('image', 'integer');
123 15
		$this->addType('person', 'integer');
124 15
	}
125
126
	/**
127
	 * Factory method to create Face from face structure that is returned as output of the model.
128
	 *
129
	 * @param int $image Image Id
130
	 * @param array $faceFromModel Face obtained from DNN model
131
	 * @return Face Created face
132
	 */
133 15
	public static function fromModel(int $imageId, array $faceFromModel): Face {
134 15
		$face = new Face();
135 15
		$face->image      = $imageId;
136 15
		$face->person     = null;
137 15
		$face->left       = $faceFromModel['left'];
138 15
		$face->right      = $faceFromModel['right'];
139 15
		$face->top        = $faceFromModel['top'];
140 15
		$face->bottom     = $faceFromModel['bottom'];
141 15
		$face->confidence = $faceFromModel['detection_confidence'];
142 15
		$face->landmarks  = isset($faceFromModel['landmarks']) ? $faceFromModel['landmarks'] : [];
143 15
		$face->descriptor = isset($faceFromModel['descriptor']) ? $faceFromModel['descriptor'] : [];
144 15
		$face->setCreationTime(new \DateTime());
145 15
		return $face;
146
	}
147
148
	/**
149
	 * Gets face width
150
	 *
151
	 * @return int Face width
152
	 */
153 1
	public function width(): int {
154 1
		return $this->right - $this->left;
155
	}
156
157
	/**
158
	 * Gets face height
159
	 *
160
	 * @return int Face height
161
	 */
162 1
	public function height(): int {
163 1
		return $this->bottom - $this->top;
164
	}
165
166
	public function jsonSerialize() {
167
		return [
168
			'id' => $this->id,
169
			'image' => $this->image,
170
			'person' => $this->person,
171
			'left' => $this->left,
172
			'right' => $this->right,
173
			'top' => $this->top,
174
			'bottom' => $this->bottom,
175
			'confidence' => $this->confidence,
176
			'landmarks' => $this->landmarks,
177
			'descriptor' => $this->descriptor,
178
			'creation_time' => $this->creationTime
179
		];
180
	}
181
182
	public function getLandmarks(): string {
183
		return json_encode($this->landmarks);
184
	}
185
186 1
	public function setLandmarks($landmarks) {
187 1
		$this->landmarks = json_decode($landmarks);
188 1
		$this->markFieldUpdated('landmarks');
189 1
	}
190
191
	public function getDescriptor(): string {
192
		return json_encode($this->descriptor);
193
	}
194
195 14
	public function setDescriptor($descriptor) {
196 14
		$this->descriptor = json_decode($descriptor);
197 14
		$this->markFieldUpdated('descriptor');
198 14
	}
199
200 15
	public function setCreationTime($creationTime) {
201 15
		if (is_a($creationTime, 'DateTime')) {
202 15
			$this->creationTime = $creationTime;
203
		} else {
204
			$this->creationTime = new \DateTime($creationTime);
205
		}
206 15
		$this->markFieldUpdated('creationTime');
207
	}
208
}