Passed
Push — settings-service ( a793f8...527322 )
by Matias
04:01
created

Requirements   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 37.14%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
c 3
b 0
f 0
dl 0
loc 85
ccs 13
cts 35
cp 0.3714
rs 10
wmc 18

9 Methods

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