|
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->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
|
|
|
public function getFaceRecognitionModel() { |
|
49
|
|
|
return $this->getModel1File('dlib_face_recognition_resnet_model_v1.dat'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getLandmarksDetectionModel() { |
|
53
|
|
|
return $this->getModel1File('shape_predictor_5_face_landmarks.dat'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getFaceDetectionModel() { |
|
57
|
|
|
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
|
|
|
private function getModel1File(string $file) { |
|
67
|
|
|
if ($this->model !== 1) { |
|
68
|
|
|
return NULL; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$fullPath = $this->appManager->getAppPath('facerecognition') . '/models/1/' . $file; |
|
72
|
|
|
if (file_exists($fullPath)) { |
|
73
|
|
|
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
|
2 |
|
public static function isImageTypeSupported(string $mimeType): bool { |
|
94
|
|
|
if ( |
|
95
|
2 |
|
($mimeType === 'image/jpeg') or |
|
96
|
2 |
|
($mimeType === 'image/png') or |
|
97
|
2 |
|
($mimeType === 'image/bmp') or |
|
98
|
2 |
|
($mimeType === 'image/gif')) { |
|
99
|
1 |
|
return true; |
|
100
|
|
|
} else { |
|
101
|
2 |
|
return false; |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|