Passed
Push — self-contained-model ( dd9490...1c4630 )
by Matias
04:08
created

ImageProcessingContext::getSkipDetection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017-2020 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
namespace OCA\FaceRecognition\BackgroundJob\Tasks;
25
26
use OCP\Image as OCP_Image;
27
28
use OCP\Files\File;
29
use OCP\Files\Folder;
30
use OCP\IUser;
31
32
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
33
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext;
34
35
use OCA\FaceRecognition\Db\Face;
36
use OCA\FaceRecognition\Db\Image;
37
use OCA\FaceRecognition\Db\ImageMapper;
38
39
use OCA\FaceRecognition\Model\IModel;
40
use OCA\FaceRecognition\Model\ModelManager;
41
42
use OCA\FaceRecognition\Service\FileService;
43
use OCA\FaceRecognition\Service\SettingsService;
44
45
/**
46
 * Plain old PHP object holding all information
47
 * that are needed to process all faces from one image
48
 */
49
class ImageProcessingContext {
50
	/** @var string Path to the image being processed */
51
	private $imagePath;
52
53
	/** @var string Path to temporary, resized image */
54
	private $tempPath;
55
56
	/** @var float Ratio of resized image, when scaling it */
57
	private $ratio;
58
59
	/** @var array<Face> All found faces in image */
60
	private $faces;
61
62
	/**
63
	 * @var bool True if detection should be skipped, but image should be marked as processed.
64
	 * If this is set, $tempPath and $ratio will be invalid and $faces should be empty array.
65
	 */
66
	private $skipDetection;
67
68 3
	public function __construct(string $imagePath, string $tempPath, float $ratio, bool $skipDetection) {
69 3
		$this->imagePath = $imagePath;
70 3
		$this->tempPath = $tempPath;
71 3
		$this->ratio = $ratio;
72 3
		$this->faces = array();
73 3
		$this->skipDetection = $skipDetection;
74 3
	}
75
76
	public function getImagePath(): string {
77
		return $this->imagePath;
78
	}
79
80 2
	public function getTempPath(): string {
81 2
		return $this->tempPath;
82
	}
83
84
	public function getRatio(): float {
85
		return $this->ratio;
86
	}
87
88 3
	public function getSkipDetection(): bool {
89 3
		return $this->skipDetection;
90
	}
91
92
	/**
93
	 * Gets all faces
94
	 *
95
	 * @return Face[] Array of faces
96
	 */
97 3
	public function getFaces(): array {
98 3
		return $this->faces;
99
	}
100
101
	/**
102
	 * @param array<Face> $faces Array of faces to set
103
	 */
104 2
	public function setFaces($faces) {
105 2
		$this->faces = $faces;
106 2
	}
107
}
108
109
/**
110
 * Taks that get all images that are still not processed and processes them.
111
 * Processing image means that each image is prepared, faces extracted form it,
112
 * and for each found face - face descriptor is extracted.
113
 */
114
class ImageProcessingTask extends FaceRecognitionBackgroundTask {
115
	/** @var ImageMapper Image mapper*/
116
	protected $imageMapper;
117
118
	/** @var FileService */
119
	protected $fileService;
120
121
	/** @var SettingsService */
122
	protected $settingsService;
123
124
	/** @var ModelManager */
125
	protected $modelManager;
126
127
	/** @var IModel */
128
	private $model;
129
130
	/** @var int|null Maximum image area (cached, so it is not recalculated for each image) */
131
	private $maxImageAreaCached;
132
133
	/**
134
	 * @param ImageMapper $imageMapper Image mapper
135
	 * @param FileService $fileService
136
	 * @param SettingsService $settingsService
137
	 * @param ModelManager $modelManager Model manager
138
	 */
139 5
	public function __construct(ImageMapper     $imageMapper,
140
	                            FileService     $fileService,
141
	                            SettingsService $settingsService,
142
	                            ModelManager    $modelManager)
143
	{
144 5
		parent::__construct();
145
146 5
		$this->imageMapper        = $imageMapper;
147 5
		$this->fileService        = $fileService;
148 5
		$this->settingsService    = $settingsService;
149 5
		$this->modelManager       = $modelManager;
150
151 5
		$this->model              = null;
152 5
		$this->maxImageAreaCached = null;
153 5
	}
154
155
	/**
156
	 * @inheritdoc
157
	 */
158 4
	public function description() {
159 4
		return "Process all images to extract faces";
160
	}
161
162
	/**
163
	 * @inheritdoc
164
	 */
165 4
	public function execute(FaceRecognitionContext $context) {
166 4
		$this->setContext($context);
167
168 4
		$this->logInfo('NOTE: Starting face recognition. If you experience random crashes after this point, please look FAQ at https://github.com/matiasdelellis/facerecognition/wiki/FAQ');
169
170
		// Get current model.
171 4
		$modelVersion = $this->settingsService->getCurrentFaceModel();
172 4
		$this->model = $this->modelManager->getModel($modelVersion);
173
174
		// Open model.
175 4
		$this->model->open();
176
177 4
		$images = $context->propertyBag['images'];
178 4
		foreach($images as $image) {
179 4
			yield;
180
181 4
			$startMillis = round(microtime(true) * 1000);
182
183
			try {
184 4
				$imageProcessingContext = $this->findFaces($this->model, $image);
185
186 3
				if (($imageProcessingContext !== null) && ($imageProcessingContext->getSkipDetection() === false)) {
187 2
					$this->populateDescriptors($this->model, $imageProcessingContext);
188
				}
189
190 3
				if ($imageProcessingContext === null) {
191
					continue;
192
				}
193
194 3
				$endMillis = round(microtime(true) * 1000);
195 3
				$duration = max($endMillis - $startMillis, 0);
196 3
				$this->imageMapper->imageProcessed($image, $imageProcessingContext->getFaces(), $duration);
197 1
			} catch (\Exception $e) {
198 1
				if ($e->getMessage() === "std::bad_alloc") {
199
					throw new \RuntimeException("Not enough memory to run face recognition! Please look FAQ at https://github.com/matiasdelellis/facerecognition/wiki/FAQ");
200
				}
201 1
				$this->logInfo('Faces found: 0. Image will be skipped because of the following error: ' . $e->getMessage());
202 1
				$this->logDebug($e);
203 1
				$this->imageMapper->imageProcessed($image, array(), 0, $e);
204 4
			} finally {
205 4
				$this->fileService->clean();
206
			}
207
		}
208
209 4
		return true;
210
	}
211
212
	/**
213
	 * Given an image, it finds all faces on it.
214
	 * If image should be skipped, returns null.
215
	 * If there is any error, throws exception
216
	 *
217
	 * @param IModel $model Resnet model
218
	 * @param Image $image Image to find faces on
219
	 * @return ImageProcessingContext|null Generated context that hold all information needed later for this image
220
	 */
221 4
	private function findFaces(IModel $model, Image $image) {
222
		// todo: check if this hits I/O (database, disk...), consider having lazy caching to return user folder from user
223 4
		$file = $this->fileService->getFileById($image->getFile(), $image->getUser());
224
225 4
		if (empty($file)) {
226
			// If we cannot find a file probably it was deleted out of our control and we must clean our tables.
227
			$this->settingsService->setNeedRemoveStaleImages(true, $image->user);
228
			$this->logInfo('File with ID ' . $image->file . ' doesn\'t exist anymore, skipping it');
229
			return null;
230
		}
231
232 4
		$imagePath = $this->fileService->getLocalFile($file);
233
234 4
		$this->logInfo('Processing image ' . $imagePath);
235 4
		$imageProcessingContext = $this->prepareImage($imagePath);
236 3
		if ($imageProcessingContext->getSkipDetection() === true) {
237 1
			$this->logInfo('Faces found: 0 (image will be skipped because it is too small)');
238 1
			return $imageProcessingContext;
239
		}
240
241
		// Detect faces from model
242 2
		$facesFound = $model->detectFaces($imageProcessingContext->getTempPath());
243
244
		// Convert from dictionary of faces to our Face Db Entity
245 2
		$faces = array();
246 2
		foreach ($facesFound as $faceFound) {
247
			$face = Face::fromModel($image->getId(), $faceFound);
248
			$face->normalizeSize($imageProcessingContext->getRatio());
249
			$faces[] = $face;
250
		}
251
252 2
		$imageProcessingContext->setFaces($faces);
253 2
		$this->logInfo('Faces found: ' . count($faces));
254
255 2
		return $imageProcessingContext;
256
	}
257
258
	/**
259
	 * Given an image, it will rotate, scale and save image to temp location, ready to be consumed by pdlib.
260
	 *
261
	 * @param string $imagePath Path to image on disk
262
	 *
263
	 * @return ImageProcessingContext Generated context that hold all information needed later for this image.
264
	 */
265 4
	private function prepareImage(string $imagePath) {
266 4
		$image = new OCP_Image(null, $this->context->logger->getLogger(), $this->context->config);
267 4
		$image->loadFromFile($imagePath);
268 3
		$image->fixOrientation();
269
270 3
		if (!$image->valid()) {
271
			throw new \RuntimeException("Image is not valid, probably cannot be loaded");
272
		}
273
274
		// Ignore processing of images that are not large enough.
275 3
		$minImageSize = $this->settingsService->getMinimumImageSize();
276 3
		if ((imagesx($image->resource()) < $minImageSize) || (imagesy($image->resource()) < $minImageSize)) {
277 1
			return new ImageProcessingContext($imagePath, "", -1, true);
278
		}
279
280 2
		$maxImageArea = $this->getMaxImageArea($this->model);
281 2
		$ratio = $this->resizeImage($image, $maxImageArea);
282
283 2
		$tempfile = $this->fileService->getTemporaryFile(pathinfo($imagePath, PATHINFO_EXTENSION));
284 2
		$image->save($tempfile);
285
286 2
		return new ImageProcessingContext($imagePath, $tempfile, $ratio, false);
287
	}
288
289
	/**
290
	 * Resizes the image to reach max image area, but preserving ratio.
291
	 * Stolen and adopted from OC_Image->resize() (difference is that this returns ratio of resize.)
292
	 *
293
	 * @param Image $image Image to resize
294
	 * @param int $maxImageArea The maximum size of image we can handle (in pixels^2).
295
	 *
296
	 * @return float Ratio of resize. 1 if there was no resize
297
	 */
298 3
	public function resizeImage(OCP_Image $image, int $maxImageArea): float {
299 3
		if (!$image->valid()) {
300
			$message = "Image is not valid, probably cannot be loaded";
301
			$this->logInfo($message);
302
			throw new \RuntimeException($message);
303
		}
304
305 3
		$widthOrig = imagesx($image->resource());
306 3
		$heightOrig = imagesy($image->resource());
307 3
		if (($widthOrig <= 0) || ($heightOrig <= 0)) {
308
			$message = "Image is having non-positive width or height, cannot continue";
309
			$this->logInfo($message);
310
			throw new \RuntimeException($message);
311
		}
312
313 3
		$areaRatio = $maxImageArea / ($widthOrig * $heightOrig);
314 3
		$scaleFactor = sqrt($areaRatio);
315
316 3
		$newWidth = intval(round($widthOrig * $scaleFactor));
317 3
		$newHeight = intval(round($heightOrig * $scaleFactor));
318
319 3
		$success = $image->preciseResize($newWidth, $newHeight);
320 3
		if ($success === false) {
321
			throw new \RuntimeException("Error during image resize");
322
		}
323
324 3
		$this->logDebug(sprintf('Image scaled from %dx%d to %dx%d (since max image area is %d pixels^2)',
325 3
			$widthOrig, $heightOrig, $newWidth, $newHeight, $maxImageArea));
326
327 3
		return 1 / $scaleFactor;
328
	}
329
330
	/**
331
	 * Gets all face descriptors in a given image processing context. Populates "descriptor" in array of faces.
332
	 *
333
	 * @param IModel $model Resnet model
334
	 * @param ImageProcessingContext Image processing context
335
	 */
336 2
	private function populateDescriptors(IModel $model, ImageProcessingContext $imageProcessingContext) {
337 2
		$faces = $imageProcessingContext->getFaces();
338
339 2
		foreach($faces as &$face) {
340
			// For each face, we want to detect landmarks and compute descriptors.
341
			// We use already resized image (from temp, used to detect faces) for this.
342
			// (better would be to work with original image, but that will require
343
			// another orientation fix and another save to the temp)
344
			// But, since our face coordinates are already changed to align to original image,
345
			// we need to fix them up to align them to temp image here.
346
			$normalizedFace = clone $face;
347
			$normalizedFace->normalizeSize(1.0 / $imageProcessingContext->getRatio());
348
349
			// We are getting face landmarks from already prepared (temp) image (resized and with orienation fixed).
350
			$landmarks = $model->detectLandmarks($imageProcessingContext->getTempPath(), array(
351
				"left" => $normalizedFace->left, "top" => $normalizedFace->top,
352
				"bottom" => $normalizedFace->bottom, "right" => $normalizedFace->right));
353
			$face->landmarks = $landmarks['parts'];
354
355
			$descriptor = $model->computeDescriptor($imageProcessingContext->getTempPath(), $landmarks);
356
			$face->descriptor = $descriptor;
357
		}
358 2
	}
359
360
	/**
361
	 * Obtains max image area lazily (from cache, or calculates it and puts it to cache)
362
	 *
363
	 * @param IModel $model Resnet model
364
	 * @return int Max image area (in pixels^2)
365
	 */
366 2
	private function getMaxImageArea(IModel $model): int {
367 2
		if (!is_null($this->maxImageAreaCached)) {
368
			return $this->maxImageAreaCached;
369
		}
370
371 2
		$this->maxImageAreaCached = $this->calculateMaxImageArea($model);
372 2
		return $this->maxImageAreaCached;
373
	}
374
375
	/**
376
	 * Calculates max image area. This is separate function, as there are several levels of user overrides.
377
	 *
378
	 * @param IModel $model Resnet model
379
	 * @return int Max image area (in pixels^2)
380
	 */
381 2
	private function calculateMaxImageArea(IModel $model): int {
382
		// First check if we are provided value from command line
383
		//
384
		if (
385 2
			(array_key_exists('max_image_area', $this->context->propertyBag)) &&
386 2
			(!is_null($this->context->propertyBag['max_image_area']))
387
		) {
388
				return $this->context->propertyBag['max_image_area'];
389
		}
390
391
		// Check if admin persisted this setting in config and it is valid value
392
		//
393 2
		$maxImageArea = $this->settingsService->getMaximumImageArea();
394 2
		if ($maxImageArea > 0) {
395 2
			return $maxImageArea;
396
		}
397
398
		// Calculate it from memory
399
		//
400
		$allowedMemory = $this->context->propertyBag['memory'];
401
402
		// Based on amount on memory PHP have, we will determine maximum amount of image size that we need to scale to.
403
		// This reasoning and calculations are all based on analysis given here:
404
		// https://github.com/matiasdelellis/facerecognition/wiki/Performance-analysis-of-DLib%E2%80%99s-CNN-face-detection
405
		$maxImageArea = intval(($allowedMemory) / $model->getMemoryAreaRelation()); // TODO: Maybe another helper.
406
407
		return $maxImageArea;
408
	}
409
410
}