Completed
Push — master ( bd4a48...a13ca6 )
by Branko
12s queued 10s
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, 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 void setImage(int $image)
39
 * @method void setPerson(int $person)
40
 * @method void setLeft(int $left)
41
 * @method void setRight(int $right)
42
 * @method void setTop(int $top)
43
 * @method void setBottom(int $bottom)
44
 */
45
class Face extends Entity implements JsonSerializable {
46
47
	/**
48
	 * Image from this face originated from.
49
	 *
50
	 * @var int
51
	 * */
52
	public $image;
53
54
	/**
55
	 * Person (cluster) that this face belongs to
56
	 *
57
	 * @var int|null
58
	 * */
59
	public $person;
60
61
	/**
62
	 * Left border of bounding rectangle for this face
63
	 *
64
	 * @var int
65
	 * */
66
	public $left;
67
68
	/**
69
	 * Right border of bounding rectangle for this face
70
	 *
71
	 * @var int
72
	 * */
73
	public $right;
74
75
	/**
76
	 * Top border of bounding rectangle for this face
77
	 *
78
	 * @var int
79
	 * */
80
	public $top;
81
82
	/**
83
	 * Bottom border of bounding rectangle for this face
84
	 *
85
	 * @var int
86
	 * */
87
	public $bottom;
88
89
	/**
90
	 * 128D face descriptor for this face.
91
	 *
92
	 * @var array
93
	 * */
94
	public $descriptor;
95
96
	/**
97
	 * Time when this face was found
98
	 *
99
	 * @var \DateTime
100
	 * */
101
	public $creationTime;
102
103
	/**
104
	 * Factory method to create Face from face structure that is returned as output of the model.
105
	 *
106
	 * @param int $image Image Id
107
	 * @param array $faceFromModel Face obtained from DNN model
108
	 * @return Face Created face
109
	 */
110 15
	public static function fromModel(int $image, array $faceFromModel): Face {
111 15
		$face = new Face();
112 15
		$face->setImage($image);
113 15
		$face->setPerson(null);
114 15
		$face->setLeft(max($faceFromModel["left"], 0));
115 15
		$face->setRight($faceFromModel["right"]);
116 15
		$face->setTop(max($faceFromModel["top"], 0));
117 15
		$face->setBottom($faceFromModel["bottom"]);
118 15
		$face->setDescriptor("[]");
119 15
		$face->setCreationTime(new \DateTime());
120 15
		return $face;
121
	}
122
123
	/**
124
	 * Helper method, to normalize face sizes back to original dimensions, based on ratio
125
	 *
126
	 * @param float $ratio Ratio of image resize
127
	 */
128 1
	public function normalizeSize($ratio) {
129 1
		$this->left =   intval(round($this->left * $ratio));
130 1
		$this->right =  intval(round($this->right * $ratio));
131 1
		$this->top =    intval(round($this->top * $ratio));
132 1
		$this->bottom = intval(round($this->bottom * $ratio));
133 1
	}
134
135
	/**
136
	 * Gets face width
137
	 *
138
	 * @return int Face width
139
	 */
140
	public function width(): int {
141
		return $this->right - $this->left;
142
	}
143
144
	/**
145
	 * Gets face height
146
	 *
147
	 * @return int Face height
148
	 */
149
	public function height(): int {
150
		return $this->bottom - $this->top;
151
	}
152
153
	public function jsonSerialize() {
154
		return [
155
			'id' => $this->id,
156
			'image' => $this->image,
157
			'person' => $this->person,
158
			'left' => $this->left,
159
			'right' => $this->right,
160
			'top' => $this->top,
161
			'bottom' => $this->bottom,
162
			'descriptor' => $this->descriptor,
163
			'creation_time' => $this->creationTime
164
		];
165
	}
166
167
	public function getDescriptor(): string {
168
		return json_encode($this->descriptor);
169
	}
170
171 15
	public function setDescriptor($descriptor) {
172 15
		$this->descriptor = json_decode($descriptor);
173 15
		$this->markFieldUpdated('descriptor');
174 15
	}
175
176 15
	public function setCreationTime($creationTime) {
177 15
		if (is_a($creationTime, 'DateTime')) {
178 15
			$this->creationTime = $creationTime;
179
		} else {
180
			$this->creationTime = new \DateTime($creationTime);
181
		}
182 15
		$this->markFieldUpdated('creationTime');
183
	}
184
}