Passed
Push — memory-logic ( 482239 )
by Matias
04:06
created

Requirements::pdlibVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace OCA\FaceRecognition\Helper;
3
4
use OCA\FaceRecognition\Helper\MemoryLimits;
5
6
use OCA\FaceRecognition\Service\ModelService;
7
use OCA\FaceRecognition\Service\SettingsService;
8
9
class Requirements
10
{
11
	/** @var ModelService */
12
	protected $modelService;
13
14 5
	public function __construct(ModelService $modelService,
15
	                            int          $model) {
16 5
		$this->modelService = $modelService;
17
18 5
		$this->modelService->useModelVersion($model);
19 5
	}
20
21
	public function hasEnoughMemory() {
22
		$memory = MemoryLimits::getSystemMemory();
23
		return ($memory > SettingsService::MINIMUM_SYSTEM_MEMORY_REQUERIMENTS);
24
	}
25
26 1
	public function pdlibLoaded() {
27 1
		return extension_loaded('pdlib');
28
	}
29
30
	public function pdlibVersion() {
31
		if (!$this->pdlibLoaded())
32
			return '0.0';
33
		return phpversion ('pdlib');
34
	}
35
36
	public function modelFilesPresent(): bool {
37
		$faceDetection = $this->getFaceDetectionModel();
38
		$landmarkDetection = $this->getLandmarksDetectionModel();
39
		$faceRecognition = $this->getFaceRecognitionModel();
40
41
		if (($faceDetection === NULL) || ($landmarkDetection === NULL) || ($faceRecognition === NULL)) {
42
			return false;
43
		} else {
44
			return true;
45
		}
46
	}
47
48 4
	public function getFaceRecognitionModel() {
49 4
		return $this->getModel1File('dlib_face_recognition_resnet_model_v1.dat');
50
	}
51
52 4
	public function getLandmarksDetectionModel() {
53 4
		return $this->getModel1File('shape_predictor_5_face_landmarks.dat');
54
	}
55
56 4
	public function getFaceDetectionModel() {
57 4
		return $this->getModel1File('mmod_human_face_detector.dat');
58
	}
59
60
	/**
61
	 * Common getter to full path, for all files from model with ID = 1
62
	 *
63
	 * @param string $file File to check
64
	 * @return string|null Full path to file, or NULL if file is not found
65
	 */
66 4
	private function getModel1File(string $file) {
67 4
		$fullPath = $this->modelService->getModelPath($file);
68
69 4
		if (file_exists($fullPath)) {
70 4
			return $fullPath;
71
		} else {
72
			return NULL;
73
		}
74
	}
75
76
	/**
77
	 * Determines if FaceRecognition can work with a givem image type. This is determined as
78
	 * intersection of types that are supported in Nextcloud and types that are supported in DLIB.
79
	 *
80
	 * Dlib support can be found here:
81
	 * https://github.com/davisking/dlib/blob/9b82f4b0f65a2152b4a4243c15709e5cb83f7044/dlib/image_loader/load_image.h#L21
82
	 *
83
	 * Note that Dlib supports these if it is compiled with them only! (with libjpeg, libpng...)
84
	 *
85
	 * Based on that and the fact that Nextcloud is superset of these, these are supported image types.
86
	 *
87
	 * @param string $mimeType MIME type to check if it supported
88
	 * @return true if MIME type is supported, false otherwise
89
	 */
90 8
	public static function isImageTypeSupported(string $mimeType): bool {
91
		if (
92 8
				($mimeType === 'image/jpeg') or
93 2
				($mimeType === 'image/png') or
94 2
				($mimeType === 'image/bmp') or
95 8
				($mimeType === 'image/gif')) {
96 7
			return true;
97
		} else {
98 2
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
99
		}
100
	}
101
}
102