Passed
Push — self-contained-model ( d8299d...34afa8 )
by Matias
03:49
created

DlibCnnModel::isInstalled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
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;
26
27
use OCP\IDBConnection;
28
29
use OCA\FaceRecognition\Helper\Requirements;
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 DlibCnnModel implements IModel {
38
39
	/*
40
	 * Model files.
41
	 */
42
	const MODEL_DETECTOR = 0;
43
	const MODEL_PREDICTOR = 1;
44
	const MODEL_RESNET = 2;
45
46
	/** @var \CnnFaceDetection */
0 ignored issues
show
Bug introduced by
The type CnnFaceDetection 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...
47
	private $cfd;
48
49
	/** @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...
50
	private $fld;
51
52
	/** @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...
53
	private $fr;
54
55
	/** @var IDBConnection */
56
	private $connection;
57
58
	/** @var FileService */
59
	private $fileService;
60
61
	/** @var ModelService */
62
	private $modelService;
63
64
	/** @var SettingsService */
65
	private $settingsService;
66
67
68
	/**
69
	 * DlibCnn5Model __construct.
70
	 *
71
	 * @param IDBConnection $connection
72
	 * @param FileService $fileService
73
	 * @param ModelService $modelService
74
	 * @param SettingsService $settingsService
75
	 */
76 1
	public function __construct(IDBConnection   $connection,
77
	                            FileService     $fileService,
78
	                            ModelService    $modelService,
79
	                            SettingsService $settingsService)
80
	{
81 1
		$this->connection       = $connection;
82 1
		$this->fileService      = $fileService;
83 1
		$this->modelService     = $modelService;
84 1
		$this->settingsService  = $settingsService;
85 1
	}
86
87 4
	public function getId(): int {
88 4
		return static::FACE_MODEL_ID;
0 ignored issues
show
Bug introduced by
The constant OCA\FaceRecognition\Mode...CnnModel::FACE_MODEL_ID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
89
	}
90
91
	public function getName(): string {
92
		return static::FACE_MODEL_NAME;
0 ignored issues
show
Bug introduced by
The constant OCA\FaceRecognition\Mode...nModel::FACE_MODEL_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
93
	}
94
95
	public function getDescription(): string {
96
		return static::FACE_MODEL_DESC;
0 ignored issues
show
Bug introduced by
The constant OCA\FaceRecognition\Mode...nModel::FACE_MODEL_DESC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
97
	}
98
99
	public function isInstalled(): bool {
100
		$requirements = new Requirements($this->modelService, $this->getId());
101
		return $requirements->modelFilesPresent();
102
	}
103
104
	public function meetDependencies(): bool {
105
		$model = $this->settingsService->getCurrentFaceModel();
106
		$requirements = new Requirements($this->modelService, $model);
107
		return extension_loaded('pdlib') && $requirements->modelFilesPresent();
108
	}
109
110
	public function install() {
111
		if ($this->isInstalled()) {
112
			return;
113
		}
114
115
		/* Still not installed but it is necessary to get the model folders */
116
		$this->modelService->useModelVersion($this->getId());
117
118
		/* Download and install models */
119
		$detectorModelBz2 = $this->fileService->downloaldFile(static::FACE_MODEL_BZ2_URLS[self::MODEL_DETECTOR]);
0 ignored issues
show
Bug introduced by
The constant OCA\FaceRecognition\Mode...el::FACE_MODEL_BZ2_URLS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
120
		$this->fileService->bunzip2($detectorModelBz2, $this->modelService->getModelPath(static::FACE_MODEL_FILES[self::MODEL_DETECTOR]));
0 ignored issues
show
Bug introduced by
The constant OCA\FaceRecognition\Mode...Model::FACE_MODEL_FILES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121
122
		$predictorModelBz2 = $this->fileService->downloaldFile(static::FACE_MODEL_BZ2_URLS[self::MODEL_PREDICTOR]);
123
		$this->fileService->bunzip2($predictorModelBz2, $this->modelService->getModelPath(static::FACE_MODEL_FILES[self::MODEL_PREDICTOR]));
124
125
		$resnetModelBz2 = $this->fileService->downloaldFile(static::FACE_MODEL_BZ2_URLS[self::MODEL_RESNET]);
126
		$this->fileService->bunzip2($resnetModelBz2, $this->modelService->getModelPath(static::FACE_MODEL_FILES[self::MODEL_RESNET]));
127
128
		/* Clean temporary files */
129
		$this->fileService->clean();
130
131
		// Insert on database and enable it
132
		$qb = $this->connection->getQueryBuilder();
133
		$query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
134
			->from('facerecog_models')
135
			->where($qb->expr()->eq('id', $qb->createParameter('id')))
136
			->setParameter('id', $this->getId());
137
		$resultStatement = $query->execute();
138
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
139
		$resultStatement->closeCursor();
140
141
		if ((int)$data[0] <= 0) {
142
			$query = $this->connection->getQueryBuilder();
143
			$query->insert('facerecog_models')
144
			->values([
145
				'id' => $query->createNamedParameter($this->getId()),
146
				'name' => $query->createNamedParameter($this->getName()),
147
				'description' => $query->createNamedParameter($this->getDescription())
148
			])
149
			->execute();
150
		}
151
	}
152
153
	public function setDefault() {
154
		// Use default model, if it is not set already.
155
		if ($this->settingsService->getCurrentFaceModel() !== $this->getId()) {
156
			$this->settingsService->setCurrentFaceModel($this->getId());
157
		}
158
	}
159
160 4
	public function open() {
161 4
		$this->modelService->useModelVersion($this->getId());
162
163 4
		$this->cfd = new \CnnFaceDetection($this->modelService->getModelPath(static::FACE_MODEL_FILES[self::MODEL_DETECTOR]));
0 ignored issues
show
Bug introduced by
The constant OCA\FaceRecognition\Mode...Model::FACE_MODEL_FILES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
164 4
		$this->fld = new \FaceLandmarkDetection($this->modelService->getModelPath(static::FACE_MODEL_FILES[self::MODEL_PREDICTOR]));
165 4
		$this->fr = new \FaceRecognition($this->modelService->getModelPath(static::FACE_MODEL_FILES[self::MODEL_RESNET]));
166 4
	}
167
168 2
	public function detectFaces(string $imagePath): array {
169 2
		return $this->cfd->detect($imagePath);
170
	}
171
172 1
	public function detectLandmarks(string $imagePath, array $rect): array {
173 1
		return $this->fld->detect($imagePath, $rect);
174
	}
175
176 1
	public function computeDescriptor(string $imagePath, array $landmarks): array {
177 1
		return $this->fr->computeDescriptor($imagePath, $landmarks);
178
	}
179
180
}
181