Completed
Push — master ( dce9c2...90c6b3 )
by Branko
10s
created

Requirements::getModel1File()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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 modelFilesPresent(): bool {
25
		if ($this->model === 1) {
26
			$faceDetection = $this->getFaceDetectionModelv2();
27
			$landmarkDetection = $this->getLandmarksDetectionModelv2();
28
			$faceRecognition = $this->getFaceRecognitionModelv2();
29
30
			if (($faceDetection === NULL) || ($landmarkDetection === NULL) || ($faceRecognition === NULL)) {
31
				return false;
32
			} else {
33
				return true;
34
			}
35
		} else {
36
			// Since current app version only can handle model with ID=1,
37
			// we surely cannot check if files from other model exist
38
			return false;
39
		}
40
	}
41
	public function getPythonHelper ()
42
	{
43
		if (file_exists('/bin/nextcloud-face-recognition-cmd') ||
44
		    file_exists('/usr/bin/nextcloud-face-recognition-cmd')) {
45
			return 'nextcloud-face-recognition-cmd';
46
		}
47
		else if (file_exists($this->appManager->getAppPath('facerecognition').'/opt/bin/nextcloud-face-recognition-cmd')) {
48
			return $this->appManager->getAppPath('facerecognition').'/opt/bin/nextcloud-face-recognition-cmd';
49
		}
50
		else {
51
			return NULL;
52
		}
53
	}
54
55
	public function getRecognitionModel ()
56
	{
57
		if (file_exists($this->appManager->getAppPath('facerecognition').'/vendor/models/dlib_face_recognition_resnet_model_v1.dat')) {
58
			return $this->appManager->getAppPath('facerecognition').'/vendor/models/dlib_face_recognition_resnet_model_v1.dat';
59
		}
60
		else {
61
			return NULL;
62
		}
63
	}
64
65
	public function getFaceRecognitionModelv2() {
66
		return $this->getModel1File('dlib_face_recognition_resnet_model_v1.dat');
67
	}
68
69
	public function getLandmarksModel ()
70
	{
71
		if (file_exists($this->appManager->getAppPath('facerecognition').'/vendor/models/shape_predictor_5_face_landmarks.dat')) {
72
			return $this->appManager->getAppPath('facerecognition').'/vendor/models/shape_predictor_5_face_landmarks.dat';
73
		}
74
		else if (file_exists($this->appManager->getAppPath('facerecognition').'/vendor/models/shape_predictor_68_face_landmarks.dat')) {
75
			return $this->appManager->getAppPath('facerecognition').'/vendor/models/shape_predictor_68_face_landmarks.dat';
76
		}
77
		else {
78
			return NULL;
79
		}
80
	}
81
82
	public function getLandmarksDetectionModelv2() {
83
		return $this->getModel1File('shape_predictor_5_face_landmarks.dat');
84
	}
85
86
	public function getFaceDetectionModelv2() {
87
		return $this->getModel1File('mmod_human_face_detector.dat');
88
	}
89
90
	/**
91
	 * Common getter to full path, for all files from model with ID = 1
92
	 *
93
	 * @param string $file File to check
94
	 * @return string|null Full path to file, or NULL if file is not found
95
	 */
96
	private function getModel1File(string $file) {
97
		if ($this->model !== 1) {
98
			return NULL;
99
		}
100
101
		$fullPath = $this->appManager->getAppPath('facerecognition') . '/models/1/' . $file;
102
		if (file_exists($fullPath)) {
103
			return $fullPath;
104
		} else {
105
			return NULL;
106
		}
107
	}
108
109
	/**
110
	 * Determines if FaceRecognition can work with a givem image type. This is determined as
111
	 * intersection of types that are supported in Nextcloud and types that are supported in DLIB.
112
	 *
113
	 * Dlib support can be found here:
114
	 * https://github.com/davisking/dlib/blob/9b82f4b0f65a2152b4a4243c15709e5cb83f7044/dlib/image_loader/load_image.h#L21
115
	 *
116
	 * Note that Dlib supports these if it is compiled with them only! (with libjpeg, libpng...)
117
	 *
118
	 * Based on that and the fact that Nextcloud is superset of these, these are supported image types.
119
	 *
120
	 * @param string $mimeType MIME type to check if it supported
121
	 * @return true if MIME type is supported, false otherwise
122
	 */
123
	public static function isImageTypeSupported(string $mimeType): bool {
124
		if (
125
				($mimeType === 'image/jpeg') or
126
				($mimeType === 'image/png') or
127
				($mimeType === 'image/bmp') or
128
				($mimeType === 'image/gif')) {
129
			return true;
130
		} else {
131
			return false;
132
		}
133
	}
134
}
135