Completed
Push — master ( 8cda4e...9c3f97 )
by Matias
05:59 queued 05:56
created

DlibHogModel::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2020, 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
25
namespace OCA\FaceRecognition\Model\DlibHogModel;
26
27
use OCP\IDBConnection;
28
29
use OCA\FaceRecognition\Helper\MemoryLimits;
30
31
use OCA\FaceRecognition\Service\FileService;
32
use OCA\FaceRecognition\Service\ModelService;
33
use OCA\FaceRecognition\Service\SettingsService;
34
35
use OCA\FaceRecognition\Model\IModel;
36
37
class DlibHogModel implements IModel {
38
39
	/*
40
	 * Model files.
41
	 */
42
	const FACE_MODEL_ID = 3;
43
	const FACE_MODEL_NAME = "DlibHog";
44
	const FACE_MODEL_DESC = "Dlib HOG Model which needs lower requirements";
45
46
	/** Relationship between image size and memory consumed */
47
	const MEMORY_AREA_RELATIONSHIP = 1 * 1024;
48
49
	const FACE_MODEL_BZ2_URLS = [
50
		'https://github.com/davisking/dlib-models/raw/4af9b776281dd7d6e2e30d4a2d40458b1e254e40/shape_predictor_5_face_landmarks.dat.bz2',
51
		'https://github.com/davisking/dlib-models/raw/2a61575dd45d818271c085ff8cd747613a48f20d/dlib_face_recognition_resnet_model_v1.dat.bz2'
52
	];
53
54
	const FACE_MODEL_FILES = [
55
		'shape_predictor_5_face_landmarks.dat',
56
		'dlib_face_recognition_resnet_model_v1.dat'
57
	];
58
59
	const I_MODEL_PREDICTOR = 0;
60
	const I_MODEL_RESNET = 1;
61
62
	const PREFERRED_MIMETYPE = 'image/png';
63
64
	/** @var \FaceLandmarkDetection */
0 ignored issues
show
Bug introduced by
The type FaceLandmarkDetection 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...
65
	private $fld;
66
67
	/** @var \FaceRecognition */
0 ignored issues
show
Bug introduced by
The type FaceRecognition 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...
68
	private $fr;
69
70
	/** @var IDBConnection */
71
	private $connection;
72
73
	/** @var FileService */
74
	private $fileService;
75
76
	/** @var ModelService */
77
	private $modelService;
78
79
	/** @var SettingsService */
80
	private $settingsService;
81
82
83
	/**
84
	 * DlibCnnModel __construct.
85
	 *
86
	 * @param IDBConnection $connection
87
	 * @param FileService $fileService
88
	 * @param ModelService $modelService
89
	 * @param SettingsService $settingsService
90
	 */
91 1
	public function __construct(IDBConnection   $connection,
92
	                            FileService     $fileService,
93
	                            ModelService    $modelService,
94
	                            SettingsService $settingsService)
95
	{
96 1
		$this->connection       = $connection;
97 1
		$this->fileService      = $fileService;
98 1
		$this->modelService     = $modelService;
99 1
		$this->settingsService  = $settingsService;
100 1
	}
101
102
	public function getId(): int {
103
		return static::FACE_MODEL_ID;
104
	}
105
106
	public function getName(): string {
107
		return static::FACE_MODEL_NAME;
108
	}
109
110
	public function getDescription(): string {
111
		return static::FACE_MODEL_DESC;
112
	}
113
114
	public function isInstalled(): bool {
115
		if (!$this->modelService->modelFileExists($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR]))
116
			return false;
117
		if (!$this->modelService->modelFileExists($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET]))
118
			return false;
119
		return true;
120
	}
121
122
	public function meetDependencies(): bool {
123
		return extension_loaded('pdlib') &&
124
		       version_compare(phpversion('pdlib'), '1.0.1', '>=');
125
	}
126
127
	public function getMaximumArea(): int {
128
		return intval(MemoryLimits::getAvailableMemory()/self::MEMORY_AREA_RELATIONSHIP);
129
	}
130
131
	public function getPreferredMimeType(): string {
132
		return static::PREFERRED_MIMETYPE;
133
	}
134
135
	public function install() {
136
		if ($this->isInstalled()) {
137
			return;
138
		}
139
140
		// Create main folder where install models.
141
		$this->modelService->prepareModelFolder($this->getId());
142
143
		/* Download and install models */
144
		$predictorModelBz2 = $this->fileService->downloaldFile(static::FACE_MODEL_BZ2_URLS[self::I_MODEL_PREDICTOR]);
145
		$this->fileService->bunzip2($predictorModelBz2, $this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR]));
146
147
		$resnetModelBz2 = $this->fileService->downloaldFile(static::FACE_MODEL_BZ2_URLS[self::I_MODEL_RESNET]);
148
		$this->fileService->bunzip2($resnetModelBz2, $this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET]));
149
150
		/* Clean temporary files */
151
		$this->fileService->clean();
152
153
		// Insert on database and enable it
154
		$qb = $this->connection->getQueryBuilder();
155
		$query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
156
			->from('facerecog_models')
157
			->where($qb->expr()->eq('id', $qb->createParameter('id')))
158
			->setParameter('id', $this->getId());
159
		$resultStatement = $query->execute();
160
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
161
		$resultStatement->closeCursor();
162
163
		if ((int)$data[0] <= 0) {
164
			$query = $this->connection->getQueryBuilder();
165
			$query->insert('facerecog_models')
166
			->values([
167
				'id' => $query->createNamedParameter($this->getId()),
168
				'name' => $query->createNamedParameter($this->getName()),
169
				'description' => $query->createNamedParameter($this->getDescription())
170
			])
171
			->execute();
172
		}
173
	}
174
175
	public function open() {
176
		$this->fld = new \FaceLandmarkDetection($this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR]));
177
		$this->fr = new \FaceRecognition($this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET]));
178
	}
179
180
	public function detectFaces(string $imagePath): array {
181
		$faces_detected = dlib_face_detection($imagePath);
0 ignored issues
show
Bug introduced by
The function dlib_face_detection was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
		$faces_detected = /** @scrutinizer ignore-call */ dlib_face_detection($imagePath);
Loading history...
182
		// To improve clustering a confidence value is needed, which this model does not provide
183
		return array_map (function (array $face) { $face['detection_confidence'] = 1.0; return $face; }, $faces_detected);
184
	}
185
186
	public function detectLandmarks(string $imagePath, array $rect): array {
187
		return $this->fld->detect($imagePath, $rect);
188
	}
189
190
	public function computeDescriptor(string $imagePath, array $landmarks): array {
191
		return $this->fr->computeDescriptor($imagePath, $landmarks);
192
	}
193
194
}
195