Passed
Push — new-admin-page ( db7bd8...30dc11 )
by Matias
03:01 queued 01:48
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
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
	public function __construct(IAppManager $appManager, int $model) {
16
		$this->appManager = $appManager;
17
		$this->model = $model;
18
	}
19
20
	public function pdlibLoaded() {
21
		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->getFaceDetectionModelv2();
33
			$landmarkDetection = $this->getLandmarksDetectionModelv2();
34
			$faceRecognition = $this->getFaceRecognitionModelv2();
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
	public function getPythonHelper ()
48
	{
49
		if (file_exists('/bin/nextcloud-face-recognition-cmd') ||
50
		    file_exists('/usr/bin/nextcloud-face-recognition-cmd')) {
51
			return 'nextcloud-face-recognition-cmd';
52
		}
53
		else if (file_exists($this->appManager->getAppPath('facerecognition').'/opt/bin/nextcloud-face-recognition-cmd')) {
54
			return $this->appManager->getAppPath('facerecognition').'/opt/bin/nextcloud-face-recognition-cmd';
55
		}
56
		else {
57
			return NULL;
58
		}
59
	}
60
61
	public function getRecognitionModel ()
62
	{
63
		if (file_exists($this->appManager->getAppPath('facerecognition').'/vendor/models/dlib_face_recognition_resnet_model_v1.dat')) {
64
			return $this->appManager->getAppPath('facerecognition').'/vendor/models/dlib_face_recognition_resnet_model_v1.dat';
65
		}
66
		else {
67
			return NULL;
68
		}
69
	}
70
71
	public function getFaceRecognitionModelv2() {
72
		return $this->getModel1File('dlib_face_recognition_resnet_model_v1.dat');
73
	}
74
75
	public function getLandmarksModel ()
76
	{
77
		if (file_exists($this->appManager->getAppPath('facerecognition').'/vendor/models/shape_predictor_5_face_landmarks.dat')) {
78
			return $this->appManager->getAppPath('facerecognition').'/vendor/models/shape_predictor_5_face_landmarks.dat';
79
		}
80
		else if (file_exists($this->appManager->getAppPath('facerecognition').'/vendor/models/shape_predictor_68_face_landmarks.dat')) {
81
			return $this->appManager->getAppPath('facerecognition').'/vendor/models/shape_predictor_68_face_landmarks.dat';
82
		}
83
		else {
84
			return NULL;
85
		}
86
	}
87
88
	public function getLandmarksDetectionModelv2() {
89
		return $this->getModel1File('shape_predictor_5_face_landmarks.dat');
90
	}
91
92
	public function getFaceDetectionModelv2() {
93
		return $this->getModel1File('mmod_human_face_detector.dat');
94
	}
95
96
	/**
97
	 * Common getter to full path, for all files from model with ID = 1
98
	 *
99
	 * @param string $file File to check
100
	 * @return string|null Full path to file, or NULL if file is not found
101
	 */
102
	private function getModel1File(string $file) {
103
		if ($this->model !== 1) {
104
			return NULL;
105
		}
106
107
		$fullPath = $this->appManager->getAppPath('facerecognition') . '/models/1/' . $file;
108
		if (file_exists($fullPath)) {
109
			return $fullPath;
110
		} else {
111
			return NULL;
112
		}
113
	}
114
115
	/**
116
	 * Determines if FaceRecognition can work with a givem image type. This is determined as
117
	 * intersection of types that are supported in Nextcloud and types that are supported in DLIB.
118
	 *
119
	 * Dlib support can be found here:
120
	 * https://github.com/davisking/dlib/blob/9b82f4b0f65a2152b4a4243c15709e5cb83f7044/dlib/image_loader/load_image.h#L21
121
	 *
122
	 * Note that Dlib supports these if it is compiled with them only! (with libjpeg, libpng...)
123
	 *
124
	 * Based on that and the fact that Nextcloud is superset of these, these are supported image types.
125
	 *
126
	 * @param string $mimeType MIME type to check if it supported
127
	 * @return true if MIME type is supported, false otherwise
128
	 */
129 4
	public static function isImageTypeSupported(string $mimeType): bool {
130
		if (
131 4
				($mimeType === 'image/jpeg') or
132 4
				($mimeType === 'image/png') or
133 4
				($mimeType === 'image/bmp') or
134 4
				($mimeType === 'image/gif')) {
135 1
			return true;
136
		} else {
137 4
			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...
138
		}
139
	}
140
}
141