matiasdelellis /
facerecognition
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * @copyright Copyright (c) 2021-2023, Matias De lellis <[email protected]> |
||||
| 4 | * @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]> |
||||
| 5 | * |
||||
| 6 | * @author Branko Kokanovic <[email protected]> |
||||
| 7 | * |
||||
| 8 | * @license GNU AGPL version 3 or any later version |
||||
| 9 | * |
||||
| 10 | * This program is free software: you can redistribute it and/or modify |
||||
| 11 | * it under the terms of the GNU Affero General Public License as |
||||
| 12 | * published by the Free Software Foundation, either version 3 of the |
||||
| 13 | * License, or (at your option) any later version. |
||||
| 14 | * |
||||
| 15 | * This program is distributed in the hope that it will be useful, |
||||
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
| 18 | * GNU Affero General Public License for more details. |
||||
| 19 | * |
||||
| 20 | * You should have received a copy of the GNU Affero General Public License |
||||
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
| 22 | * |
||||
| 23 | */ |
||||
| 24 | |||||
| 25 | namespace OCA\FaceRecognition\Model\DlibHogModel; |
||||
| 26 | |||||
| 27 | use OCA\FaceRecognition\Helper\MemoryLimits; |
||||
| 28 | |||||
| 29 | use OCA\FaceRecognition\Service\CompressionService; |
||||
| 30 | use OCA\FaceRecognition\Service\DownloadService; |
||||
| 31 | use OCA\FaceRecognition\Service\ModelService; |
||||
| 32 | use OCA\FaceRecognition\Service\SettingsService; |
||||
| 33 | |||||
| 34 | use OCA\FaceRecognition\Model\IModel; |
||||
| 35 | |||||
| 36 | class DlibHogModel implements IModel { |
||||
| 37 | |||||
| 38 | /* |
||||
| 39 | * Model files. |
||||
| 40 | */ |
||||
| 41 | const FACE_MODEL_ID = 3; |
||||
| 42 | const FACE_MODEL_NAME = 'DlibHog'; |
||||
| 43 | const FACE_MODEL_DESC = 'Dlib HOG Model which needs lower requirements'; |
||||
| 44 | const FACE_MODEL_DOC = 'https://github.com/matiasdelellis/facerecognition/wiki/Models#model-3'; |
||||
| 45 | |||||
| 46 | /** This model practically does not consume memory. Directly set the limits. */ |
||||
| 47 | const MAXIMUM_IMAGE_AREA = SettingsService::MAXIMUM_ANALYSIS_IMAGE_AREA; |
||||
| 48 | const MINIMUM_MEMORY_REQUIREMENTS = 128 * 1024 * 1024; |
||||
| 49 | |||||
| 50 | const FACE_MODEL_BZ2_URLS = [ |
||||
| 51 | 'https://github.com/davisking/dlib-models/raw/4af9b776281dd7d6e2e30d4a2d40458b1e254e40/shape_predictor_5_face_landmarks.dat.bz2', |
||||
| 52 | 'https://github.com/davisking/dlib-models/raw/2a61575dd45d818271c085ff8cd747613a48f20d/dlib_face_recognition_resnet_model_v1.dat.bz2' |
||||
| 53 | ]; |
||||
| 54 | |||||
| 55 | const FACE_MODEL_FILES = [ |
||||
| 56 | 'shape_predictor_5_face_landmarks.dat', |
||||
| 57 | 'dlib_face_recognition_resnet_model_v1.dat' |
||||
| 58 | ]; |
||||
| 59 | |||||
| 60 | const I_MODEL_PREDICTOR = 0; |
||||
| 61 | const I_MODEL_RESNET = 1; |
||||
| 62 | |||||
| 63 | const PREFERRED_MIMETYPE = 'image/png'; |
||||
| 64 | |||||
| 65 | /** @var \FaceLandmarkDetection */ |
||||
|
0 ignored issues
–
show
|
|||||
| 66 | private $fld; |
||||
| 67 | |||||
| 68 | /** @var \FaceRecognition */ |
||||
|
0 ignored issues
–
show
The type
FaceRecognition was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 69 | private $fr; |
||||
| 70 | |||||
| 71 | /** @var CompressionService */ |
||||
| 72 | private $compressionService; |
||||
| 73 | |||||
| 74 | /** @var DownloadService */ |
||||
| 75 | private $downloadService; |
||||
| 76 | |||||
| 77 | /** @var ModelService */ |
||||
| 78 | private $modelService; |
||||
| 79 | |||||
| 80 | /** @var SettingsService */ |
||||
| 81 | private $settingsService; |
||||
| 82 | |||||
| 83 | |||||
| 84 | /** |
||||
| 85 | * DlibCnnModel __construct. |
||||
| 86 | * |
||||
| 87 | * @param CompressionService $compressionService |
||||
| 88 | * @param DownloadService $downloadService |
||||
| 89 | * @param ModelService $modelService |
||||
| 90 | * @param SettingsService $settingsService |
||||
| 91 | */ |
||||
| 92 | 1 | public function __construct(CompressionService $compressionService, |
|||
| 93 | DownloadService $downloadService, |
||||
| 94 | ModelService $modelService, |
||||
| 95 | SettingsService $settingsService) |
||||
| 96 | { |
||||
| 97 | 1 | $this->compressionService = $compressionService; |
|||
| 98 | 1 | $this->downloadService = $downloadService; |
|||
| 99 | 1 | $this->modelService = $modelService; |
|||
| 100 | 1 | $this->settingsService = $settingsService; |
|||
| 101 | } |
||||
| 102 | |||||
| 103 | public function getId(): int { |
||||
| 104 | return static::FACE_MODEL_ID; |
||||
| 105 | } |
||||
| 106 | |||||
| 107 | public function getName(): string { |
||||
| 108 | return static::FACE_MODEL_NAME; |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | public function getDescription(): string { |
||||
| 112 | return static::FACE_MODEL_DESC; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | public function getDocumentation(): string { |
||||
| 116 | return static::FACE_MODEL_DOC; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | public function isInstalled(): bool { |
||||
| 120 | if (!$this->modelService->modelFileExists($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR])) |
||||
| 121 | return false; |
||||
| 122 | if (!$this->modelService->modelFileExists($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET])) |
||||
| 123 | return false; |
||||
| 124 | return true; |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | public function meetDependencies(string &$error_message): bool { |
||||
| 128 | if (!extension_loaded('pdlib')) { |
||||
| 129 | $error_message = "The PDlib PHP extension is not loaded"; |
||||
| 130 | return false; |
||||
| 131 | } |
||||
| 132 | if (!version_compare(phpversion('pdlib'), '1.0.1', '>=')) { |
||||
| 133 | $error_message = "The PDlib PHP extension version is too old"; |
||||
| 134 | return false; |
||||
| 135 | } |
||||
| 136 | $assignedMemory = $this->settingsService->getAssignedMemory(); |
||||
| 137 | if ($assignedMemory < 0) { |
||||
| 138 | $error_message = "Seems that you still have to configure the assigned memory for image processing."; |
||||
| 139 | return false; |
||||
| 140 | } |
||||
| 141 | if (MemoryLimits::getAvailableMemory() < static::MINIMUM_MEMORY_REQUIREMENTS) { |
||||
| 142 | $error_message = "Your system does not meet the minimum memory requirements"; |
||||
| 143 | return false; |
||||
| 144 | } |
||||
| 145 | return true; |
||||
| 146 | } |
||||
| 147 | |||||
| 148 | public function getMaximumArea(): int { |
||||
| 149 | return intval(self::MAXIMUM_IMAGE_AREA); |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | public function getPreferredMimeType(): string { |
||||
| 153 | return static::PREFERRED_MIMETYPE; |
||||
| 154 | } |
||||
| 155 | |||||
| 156 | /** |
||||
| 157 | * @return void |
||||
| 158 | */ |
||||
| 159 | public function install() { |
||||
| 160 | if ($this->isInstalled()) { |
||||
| 161 | return; |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | // Create main folder where install models. |
||||
| 165 | $this->modelService->prepareModelFolder($this->getId()); |
||||
| 166 | |||||
| 167 | /* Download and install models */ |
||||
| 168 | $predictorModelBz2 = $this->downloadService->downloadFile(static::FACE_MODEL_BZ2_URLS[self::I_MODEL_PREDICTOR]); |
||||
| 169 | $this->compressionService->decompress($predictorModelBz2, $this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR])); |
||||
| 170 | |||||
| 171 | $resnetModelBz2 = $this->downloadService->downloadFile(static::FACE_MODEL_BZ2_URLS[self::I_MODEL_RESNET]); |
||||
| 172 | $this->compressionService->decompress($resnetModelBz2, $this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET])); |
||||
| 173 | |||||
| 174 | /* Clean temporary files */ |
||||
| 175 | $this->downloadService->clean(); |
||||
| 176 | } |
||||
| 177 | |||||
| 178 | /** |
||||
| 179 | * @return void |
||||
| 180 | */ |
||||
| 181 | public function open() { |
||||
| 182 | $this->fld = new \FaceLandmarkDetection($this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_PREDICTOR])); |
||||
| 183 | $this->fr = new \FaceRecognition($this->modelService->getFileModelPath($this->getId(), static::FACE_MODEL_FILES[self::I_MODEL_RESNET])); |
||||
| 184 | } |
||||
| 185 | |||||
| 186 | public function detectFaces(string $imagePath, bool $compute = true): array { |
||||
| 187 | $faces_detected = dlib_face_detection($imagePath); |
||||
|
0 ignored issues
–
show
The function
dlib_face_detection was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 188 | foreach ($faces_detected as &$face) { |
||||
| 189 | // Add and fake higher confidense value sinse this model does not provide it. |
||||
| 190 | $face['detection_confidence'] = 1.1; |
||||
| 191 | |||||
| 192 | if (!$compute) |
||||
| 193 | continue; |
||||
| 194 | |||||
| 195 | $landmarks = $this->fld->detect($imagePath, $face); |
||||
| 196 | $descriptor = $this->fr->computeDescriptor($imagePath, $landmarks); |
||||
| 197 | |||||
| 198 | $face['landmarks'] = $landmarks['parts']; |
||||
| 199 | $face['descriptor'] = $descriptor; |
||||
| 200 | } |
||||
| 201 | return $faces_detected; |
||||
| 202 | } |
||||
| 203 | |||||
| 204 | public function compute(string $imagePath, array $face): array { |
||||
| 205 | $landmarks = $this->fld->detect($imagePath, $face); |
||||
| 206 | $descriptor = $this->fr->computeDescriptor($imagePath, $landmarks); |
||||
| 207 | |||||
| 208 | $face['landmarks'] = $landmarks['parts']; |
||||
| 209 | $face['descriptor'] = $descriptor; |
||||
| 210 | |||||
| 211 | return $face; |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | } |
||||
| 215 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths