Passed
Push — recreate-cluster-logic ( 25ad8b )
by Branko
01:54
created

FaceNew::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 2
rs 9.9332
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
 * todo: currently named facev2 as it should not interfere with existing face. Once we switch, it should be renamed to "Face".
34
 */
35
class FaceNew extends Entity implements JsonSerializable {
36
37
	/**
38
	 * Image from this face originated from.
39
	 *
40
	 * @var int
41
	 * */
42
	public $image;
43
44
	/**
45
	 * Person (cluster) that this face belongs to
46
	 *
47
	 * @var int|null
48
	 * */
49
	public $person;
50
51
	/**
52
	 * Left border of bounding rectangle for this face
53
	 *
54
	 * @var int
55
	 * */
56
	public $left;
57
58
	/**
59
	 * Right border of bounding rectangle for this face
60
	 *
61
	 * @var int
62
	 * */
63
	public $right;
64
65
	/**
66
	 * Top border of bounding rectangle for this face
67
	 *
68
	 * @var int
69
	 * */
70
	public $top;
71
72
	/**
73
	 * Bottom border of bounding rectangle for this face
74
	 *
75
	 * @var int
76
	 * */
77
	public $bottom;
78
79
	/**
80
	 * 128D face descriptor for this face.
81
	 *
82
	 * @var array
83
	 * */
84
	public $descriptor;
85
86
	/**
87
	 * Time when this face was found
88
	 *
89
	 * @var \DateTime
90
	 * */
91
	public $creationTime;
92
93
	/**
94
	 * Factory method to create Face from face structure that is returned as output of the model.
95
	 *
96
	 * @param int $image Image Id
97
	 * @param dict $faceFromModel Face obtained from DNN model
0 ignored issues
show
Bug introduced by
The type OCA\FaceRecognition\Db\dict was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
	 * @return FaceNew Created face
99
	 */
100
	public static function fromModel($image, $faceFromModel): FaceNew {
101
		$face = new FaceNew();
102
		$face->image = $image;
103
		$face->person = null;
104
		$face->left = max($faceFromModel["left"], 0);
105
		$face->right = $faceFromModel["right"];
106
		$face->top = max($faceFromModel["top"], 0);
107
		$face->bottom = $faceFromModel["bottom"];
108
		$face->descriptor = array();
109
		$face->creationTime = new \DateTime();
110
		return $face;
111
	}
112
113
	/**
114
	 * Helper method, to normalize face sizes back to original dimensions, based on ratio
115
	 *
116
	 * @param float $ratio Ratio of image resize
117
	 */
118
	public function normalizeSize($ratio) {
119
		$this->left =   intval(round($this->left * $ratio));
120
		$this->right =  intval(round($this->right * $ratio));
121
		$this->top =    intval(round($this->top * $ratio));
122
		$this->bottom = intval(round($this->bottom * $ratio));
123
	}
124
125
	/**
126
	 * Gets face width
127
	 *
128
	 * @return int Face width
129
	 */
130
	public function width(): int {
131
		return $this->right - $this->left;
132
	}
133
134
	/**
135
	 * Gets face height
136
	 *
137
	 * @return int Face height
138
	 */
139
	public function height(): int {
140
		return $this->bottom - $this->top;
141
	}
142
143
	public function jsonSerialize() {
144
		return [
145
			'id' => $this->id,
146
			'image' => $this->image,
147
			'person' => $this->person,
148
			'left' => $this->left,
149
			'right' => $this->right,
150
			'top' => $this->top,
151
			'bottom' => $this->bottom,
152
			'descriptor' => $this->descriptor,
153
			'creation_time' => $this->creationTime
154
		];
155
	}
156
157
	public function setDescriptor($descriptor) {
158
		$this->descriptor = json_decode($descriptor);
159
	}
160
161
	public function setCreationTime($creationTime) {
162
		$this->creationTime = new \DateTime($creationTime);
163
	}
164
}