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
|
6 |
|
public function __construct(ModelService $modelService, |
12
|
|
|
int $model) { |
13
|
6 |
|
$this->modelService = $modelService; |
14
|
|
|
|
15
|
6 |
|
$this->modelService->useModelVersion($model); |
16
|
6 |
|
} |
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
|
1 |
|
public function modelFilesPresent(): bool { |
29
|
1 |
|
$faceDetection = $this->getFaceDetectionModel(); |
30
|
1 |
|
$landmarkDetection = $this->getLandmarksDetectionModel(); |
31
|
1 |
|
$faceRecognition = $this->getFaceRecognitionModel(); |
32
|
|
|
|
33
|
1 |
|
if (($faceDetection === NULL) || ($landmarkDetection === NULL) || ($faceRecognition === NULL)) { |
34
|
1 |
|
return false; |
35
|
|
|
} else { |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function getFaceRecognitionModel() { |
41
|
1 |
|
return $this->getModel1File('dlib_face_recognition_resnet_model_v1.dat'); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function getLandmarksDetectionModel() { |
45
|
1 |
|
return $this->getModel1File('shape_predictor_5_face_landmarks.dat'); |
46
|
|
|
} |
47
|
|
|
|
48
|
5 |
|
public function getFaceDetectionModel() { |
49
|
5 |
|
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
|
5 |
|
private function getModel1File(string $file) { |
59
|
5 |
|
$fullPath = $this->modelService->getModelPath($file); |
60
|
|
|
|
61
|
5 |
|
if (file_exists($fullPath)) { |
62
|
|
|
return $fullPath; |
63
|
|
|
} else { |
64
|
5 |
|
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
|
8 |
|
public static function isImageTypeSupported(string $mimeType): bool { |
83
|
|
|
if ( |
84
|
8 |
|
($mimeType === 'image/jpeg') or |
85
|
2 |
|
($mimeType === 'image/png') or |
86
|
2 |
|
($mimeType === 'image/bmp') or |
87
|
8 |
|
($mimeType === 'image/gif')) { |
88
|
7 |
|
return true; |
89
|
|
|
} else { |
90
|
2 |
|
return false; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|