Passed
Push — self-contained-model ( 13b347...b458ac )
by Matias
04:07
created

DlibCnnModel::meetDependencies()   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 0
Metric Value
cc 1
eloc 1
c 0
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\DlibCnnModel;
26
27
use OCP\IDBConnection;
28
29
use OCA\FaceRecognition\Service\FileService;
30
use OCA\FaceRecognition\Service\ModelService;
31
use OCA\FaceRecognition\Service\SettingsService;
32
33
use OCA\FaceRecognition\Model\IModel;
34
35
class DlibCnnModel implements IModel {
36
37
	/*
38
	 * Model files.
39
	 */
40
	const FACE_MODEL_ID = -1;
41
	const FACE_MODEL_NAME = "";
42
	const FACE_MODEL_DESC = "";
43
44
	/** Relationship between image size and memory consumed */
45
	const MEMORY_AREA_RELATIONSHIP = -1;
46
47
	const FACE_MODEL_BZ2_URLS = array();
48
	const FACE_MODEL_FILES = array();
49
50
	const I_MODEL_DETECTOR = 0;
51
	const I_MODEL_PREDICTOR = 1;
52
	const I_MODEL_RESNET = 2;
53
54
	/** @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...
55
	private $cfd;
56
57
	/** @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...
58
	private $fld;
59
60
	/** @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...
61
	private $fr;
62
63
	/** @var IDBConnection */
64
	private $connection;
65
66
	/** @var FileService */
67
	private $fileService;
68
69
	/** @var ModelService */
70
	private $modelService;
71
72
	/** @var SettingsService */
73
	private $settingsService;
74
75
76
	/**
77
	 * DlibCnnModel __construct.
78
	 *
79
	 * @param IDBConnection $connection
80
	 * @param FileService $fileService
81
	 * @param ModelService $modelService
82
	 * @param SettingsService $settingsService
83
	 */
84 1
	public function __construct(IDBConnection   $connection,
85
	                            FileService     $fileService,
86
	                            ModelService    $modelService,
87
	                            SettingsService $settingsService)
88
	{
89 1
		$this->connection       = $connection;
90 1
		$this->fileService      = $fileService;
91 1
		$this->modelService     = $modelService;
92 1
		$this->settingsService  = $settingsService;
93 1
	}
94
95 4
	public function getId(): int {
96 4
		return static::FACE_MODEL_ID;
97
	}
98
99
	public function getName(): string {
100
		return static::FACE_MODEL_NAME;
101
	}
102
103
	public function getDescription(): string {
104
		return static::FACE_MODEL_DESC;
105
	}
106
107 4
	public function isInstalled(): bool {
108 4
		if (!$this->modelService->modelFileExists($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_DETECTOR]))
109 1
			return false;
110 3
		if (!$this->modelService->modelFileExists($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR]))
111
			return false;
112 3
		if (!$this->modelService->modelFileExists($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET]))
113
			return false;
114 3
		return true;
115
	}
116
117
	public function meetDependencies(): bool {
118
		return extension_loaded('pdlib');
119
	}
120
121 4
	public function install() {
122 4
		if ($this->isInstalled()) {
123 3
			return;
124
		}
125
126
		// Create main folder where install models.
127 1
		$this->modelService->prepareModelFolder($this->getId());
128
129
		/* Download and install models */
130 1
		$detectorModelBz2 = $this->fileService->downloaldFile(static::FACE_MODEL_BZ2_URLS[self::I_MODEL_DETECTOR]);
131 1
		$this->fileService->bunzip2($detectorModelBz2, $this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_DETECTOR]));
132
133 1
		$predictorModelBz2 = $this->fileService->downloaldFile(static::FACE_MODEL_BZ2_URLS[self::I_MODEL_PREDICTOR]);
134 1
		$this->fileService->bunzip2($predictorModelBz2, $this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR]));
135
136 1
		$resnetModelBz2 = $this->fileService->downloaldFile(static::FACE_MODEL_BZ2_URLS[self::I_MODEL_RESNET]);
137 1
		$this->fileService->bunzip2($resnetModelBz2, $this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET]));
138
139
		/* Clean temporary files */
140 1
		$this->fileService->clean();
141
142
		// Insert on database and enable it
143 1
		$qb = $this->connection->getQueryBuilder();
144 1
		$query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
145 1
			->from('facerecog_models')
146 1
			->where($qb->expr()->eq('id', $qb->createParameter('id')))
147 1
			->setParameter('id', $this->getId());
148 1
		$resultStatement = $query->execute();
149 1
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
150 1
		$resultStatement->closeCursor();
151
152 1
		if ((int)$data[0] <= 0) {
153
			$query = $this->connection->getQueryBuilder();
154
			$query->insert('facerecog_models')
155
			->values([
156
				'id' => $query->createNamedParameter($this->getId()),
157
				'name' => $query->createNamedParameter($this->getName()),
158
				'description' => $query->createNamedParameter($this->getDescription())
159
			])
160
			->execute();
161
		}
162 1
	}
163
164 4
	public function open() {
165 4
		$this->cfd = new \CnnFaceDetection($this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_DETECTOR]));
166 4
		$this->fld = new \FaceLandmarkDetection($this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR]));
167 4
		$this->fr = new \FaceRecognition($this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET]));
168 4
	}
169
170
	public function getMemoryAreaRelation(): int {
171
		return static::MEMORY_AREA_RELATIONSHIP;
172
	}
173
174 2
	public function detectFaces(string $imagePath): array {
175 2
		return $this->cfd->detect($imagePath);
176
	}
177
178 1
	public function detectLandmarks(string $imagePath, array $rect): array {
179 1
		return $this->fld->detect($imagePath, $rect);
180
	}
181
182 1
	public function computeDescriptor(string $imagePath, array $landmarks): array {
183 1
		return $this->fr->computeDescriptor($imagePath, $landmarks);
184
	}
185
186
}
187