Passed
Pull Request — master (#136)
by Branko
01:00
created

Requirements   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 61.53%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 95
ccs 24
cts 39
cp 0.6153
rs 10
c 0
b 0
f 0
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A pdlibVersion() 0 4 2
A modelFilesPresent() 0 15 5
A isImageTypeSupported() 0 9 5
A pdlibLoaded() 0 2 1
A __construct() 0 3 1
A getFaceRecognitionModel() 0 2 1
A getModel1File() 0 10 3
A getFaceDetectionModel() 0 2 1
A getLandmarksDetectionModel() 0 2 1
1
<?php
2
3
namespace OCA\FaceRecognition\Helper;
4
5
use OCP\App\IAppManager;
6
7
class Requirements
8
{
9
	/** @var \OCP\App\IAppManager **/
10
	protected $appManager;
11
12
	/** @var int ID of used model */
13
	private $model;
14
15 5
	public function __construct(IAppManager $appManager, int $model) {
16 5
		$this->appManager = $appManager;
17 5
		$this->model = $model;
18 5
	}
19
20 1
	public function pdlibLoaded() {
21 1
		return extension_loaded('pdlib');
22
	}
23
24
	public function pdlibVersion() {
25
		if (!$this->pdlibLoaded())
26
			return '0.0';
27
		return phpversion ('pdlib');
28
	}
29
30
	public function modelFilesPresent(): bool {
31
		if ($this->model === 1) {
32
			$faceDetection = $this->getFaceDetectionModel();
33
			$landmarkDetection = $this->getLandmarksDetectionModel();
34
			$faceRecognition = $this->getFaceRecognitionModel();
35
36
			if (($faceDetection === NULL) || ($landmarkDetection === NULL) || ($faceRecognition === NULL)) {
37
				return false;
38
			} else {
39
				return true;
40
			}
41
		} else {
42
			// Since current app version only can handle model with ID=1,
43
			// we surely cannot check if files from other model exist
44
			return false;
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
		if ($this->model !== 1) {
68
			return NULL;
69
		}
70
71 4
		$fullPath = $this->appManager->getAppPath('facerecognition') . '/vendor/models/1/' . $file;
72 4
		if (file_exists($fullPath)) {
73 4
			return $fullPath;
74
		} else {
75
			return NULL;
76
		}
77
	}
78
79
	/**
80
	 * Determines if FaceRecognition can work with a givem image type. This is determined as
81
	 * intersection of types that are supported in Nextcloud and types that are supported in DLIB.
82
	 *
83
	 * Dlib support can be found here:
84
	 * https://github.com/davisking/dlib/blob/9b82f4b0f65a2152b4a4243c15709e5cb83f7044/dlib/image_loader/load_image.h#L21
85
	 *
86
	 * Note that Dlib supports these if it is compiled with them only! (with libjpeg, libpng...)
87
	 *
88
	 * Based on that and the fact that Nextcloud is superset of these, these are supported image types.
89
	 *
90
	 * @param string $mimeType MIME type to check if it supported
91
	 * @return true if MIME type is supported, false otherwise
92
	 */
93 7
	public static function isImageTypeSupported(string $mimeType): bool {
94
		if (
95 7
				($mimeType === 'image/jpeg') or
96 2
				($mimeType === 'image/png') or
97 2
				($mimeType === 'image/bmp') or
98 7
				($mimeType === 'image/gif')) {
99 6
			return true;
100
		} else {
101 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...
102
		}
103
	}
104
}
105