Passed
Push — self-contained-model ( bde79d...83cd40 )
by Matias
03:59
created

ImageProcessingTask::resizeImage()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.9256

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 30
ccs 14
cts 21
cp 0.6667
crap 5.9256
rs 9.2888
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
	public function __construct(string $imagePath, string $tempPath, float $ratio, bool $skipDetection) {
69
		$this->imagePath = $imagePath;
70
		$this->tempPath = $tempPath;
71
		$this->ratio = $ratio;
72
		$this->faces = array();
73
		$this->skipDetection = $skipDetection;
74
	}
75
76
	public function getImagePath(): string {
77
		return $this->imagePath;
78
	}
79
80
	public function getTempPath(): string {
81
		return $this->tempPath;
82
	}
83
84
	public function getRatio(): float {
85
		return $this->ratio;
86
	}
87
88
	public function getSkipDetection(): bool {
89
		return $this->skipDetection;
90
	}
91
92
	/**
93
	 * Gets all faces
94
	 *
95
	 * @return Face[] Array of faces
96
	 */
97
	public function getFaces(): array {
98
		return $this->faces;
99
	}
100
101
	/**
102
	 * @param array<Face> $faces Array of faces to set
103
	 */
104
	public function setFaces($faces) {
105
		$this->faces = $faces;
106
	}
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 1
	public function __construct(ImageMapper     $imageMapper,
140
	                            FileService     $fileService,
141
	                            SettingsService $settingsService,
142
	                            ModelManager    $modelManager)
143
	{
144 1
		parent::__construct();
145
146 1
		$this->imageMapper        = $imageMapper;
147 1
		$this->fileService        = $fileService;
148 1
		$this->settingsService    = $settingsService;
149 1
		$this->modelManager       = $modelManager;
150
151 1
		$this->model              = null;
152 1
		$this->maxImageAreaCached = null;
153 1
	}
154
155
	/**
156
	 * @inheritdoc
157
	 */
158
	public function description() {
159
		return "Process all images to extract faces";
160
	}
161
162
	/**
163
	 * @inheritdoc
164
	 */
165
	public function execute(FaceRecognitionContext $context) {
166
		$this->setContext($context);
167
168
		$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
		$modelVersion = $this->settingsService->getCurrentFaceModel();
172
		$this->model = $this->modelManager->getModel($modelVersion);
173
174
		// Open model.
175
		$this->model->open();
176
177
		$images = $context->propertyBag['images'];
178
		foreach($images as $image) {
179
			yield;
180
181
			$startMillis = round(microtime(true) * 1000);
182
183
			try {
184
				$imageProcessingContext = $this->findFaces($this->model, $image);
185
186
				if (($imageProcessingContext !== null) && ($imageProcessingContext->getSkipDetection() === false)) {
187
					$this->populateDescriptors($this->model, $imageProcessingContext);
188
				}
189
190
				if ($imageProcessingContext === null) {
191
					continue;
192
				}
193
194
				$endMillis = round(microtime(true) * 1000);
195
				$duration = max($endMillis - $startMillis, 0);
196
				$this->imageMapper->imageProcessed($image, $imageProcessingContext->getFaces(), $duration);
197
			} catch (\Exception $e) {
198
				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
				$this->logInfo('Faces found: 0. Image will be skipped because of the following error: ' . $e->getMessage());
202
				$this->logDebug($e);
203
				$this->imageMapper->imageProcessed($image, array(), 0, $e);
204
			} finally {
205
				$this->fileService->clean();
206
			}
207
		}
208
209
		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
	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
		$file = $this->fileService->getFileById($image->getFile(), $image->getUser());
224
225
		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
		$imagePath = $this->fileService->getLocalFile($file);
233
234
		$this->logInfo('Processing image ' . $imagePath);
235
		$imageProcessingContext = $this->prepareImage($imagePath);
236
		if ($imageProcessingContext->getSkipDetection() === true) {
237
			$this->logInfo('Faces found: 0 (image will be skipped because it is too small)');
238
			return $imageProcessingContext;
239
		}
240
241
		// Detect faces from model
242
		$facesFound = $model->detectFaces($imageProcessingContext->getTempPath());
243
244
		// Convert from dictionary of faces to our Face Db Entity
245
		$faces = array();
246
		foreach ($facesFound as $faceFound) {
247
			$face = Face::fromModel($image->getId(), $faceFound);
248
			$face->normalizeSize($imageProcessingContext->getRatio());
249
			$faces[] = $face;
250
		}
251
252
		$imageProcessingContext->setFaces($faces);
253
		$this->logInfo('Faces found: ' . count($faces));
254
255
		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
	private function prepareImage(string $imagePath) {
266
		$image = new OCP_Image(null, $this->context->logger->getLogger(), $this->context->config);
267
		$image->loadFromFile($imagePath);
268
		$image->fixOrientation();
269
270
		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
		$minImageSize = $this->settingsService->getMinimumImageSize();
276
		if ((imagesx($image->resource()) < $minImageSize) || (imagesy($image->resource()) < $minImageSize)) {
277
			return new ImageProcessingContext($imagePath, "", -1, true);
278
		}
279
280
		$maxImageArea = $this->getMaxImageArea($this->model);
281
		$ratio = $this->resizeImage($image, $maxImageArea);
282
283
		$tempfile = $this->fileService->getTemporaryFile(pathinfo($imagePath, PATHINFO_EXTENSION));
284
		$image->save($tempfile);
285
286
		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 1
	public function resizeImage(OCP_Image $image, int $maxImageArea): float {
299 1
		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 1
		$widthOrig = imagesx($image->resource());
306 1
		$heightOrig = imagesy($image->resource());
307 1
		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 1
		$areaRatio = $maxImageArea / ($widthOrig * $heightOrig);
314 1
		$scaleFactor = sqrt($areaRatio);
315
316 1
		$newWidth = intval(round($widthOrig * $scaleFactor));
317 1
		$newHeight = intval(round($heightOrig * $scaleFactor));
318
319 1
		$success = $image->preciseResize($newWidth, $newHeight);
320 1
		if ($success === false) {
321
			throw new \RuntimeException("Error during image resize");
322
		}
323
324 1
		$this->logDebug(sprintf('Image scaled from %dx%d to %dx%d (since max image area is %d pixels^2)',
325 1
			$widthOrig, $heightOrig, $newWidth, $newHeight, $maxImageArea));
326
327 1
		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
	private function populateDescriptors(IModel $model, ImageProcessingContext $imageProcessingContext) {
337
		$faces = $imageProcessingContext->getFaces();
338
339
		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
	}
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
	private function getMaxImageArea(IModel $model): int {
367
		if (!is_null($this->maxImageAreaCached)) {
368
			return $this->maxImageAreaCached;
369
		}
370
371
		$this->maxImageAreaCached = $this->calculateMaxImageArea($model);
372
		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
	private function calculateMaxImageArea(IModel $model): int {
382
		// First check if we are provided value from command line
383
		//
384
		if (
385
			(array_key_exists('max_image_area', $this->context->propertyBag)) &&
386
			(!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
		$maxImageArea = $this->settingsService->getMaximumImageArea();
394
		if ($maxImageArea > 0) {
395
			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
}