diff --git a/lib/BackgroundJob/Tasks/ImageProcessingTask.php b/lib/BackgroundJob/Tasks/ImageProcessingTask.php index b527875b..ef9295c9 100644 --- a/lib/BackgroundJob/Tasks/ImageProcessingTask.php +++ b/lib/BackgroundJob/Tasks/ImageProcessingTask.php @@ -136,7 +136,7 @@ public function execute(FaceRecognitionContext $context) { } // Get faces in the temporary image - $tempImagePath = $tempImage->getTempPath(); + $tempImagePath = $tempImage->getResizedImagePath(); $rawFaces = $this->model->detectFaces($tempImagePath); $this->logInfo('Faces found: ' . count($rawFaces)); @@ -145,10 +145,10 @@ public function execute(FaceRecognitionContext $context) { foreach ($rawFaces as $rawFace) { // Normalize face and get landmarks of face from model to original size $normFace = $this->getNormalizedFace($rawFace, $tempImage->getRatio()); - $landmarks = $this->model->detectLandmarks($tempImage->getImagePath(), $normFace); + $landmarks = $this->model->detectLandmarks($tempImage->getOrientedImagePath(), $normFace); // Get descriptor of face from model - $descriptor = $this->model->computeDescriptor($tempImage->getImagePath(), $landmarks); + $descriptor = $this->model->computeDescriptor($tempImage->getOrientedImagePath(), $landmarks); // Convert from dictionary of faces to our Face Db Entity and put Landmarks and descriptor $face = Face::fromModel($image->getId(), $normFace); diff --git a/lib/Helper/TempImage.php b/lib/Helper/TempImage.php index 06dad717..cb8a1d44 100644 --- a/lib/Helper/TempImage.php +++ b/lib/Helper/TempImage.php @@ -34,7 +34,10 @@ class TempImage extends Image { private $imagePath; /** @var string */ - private $tempPath; + private $orientedImagePath; + + /** @var string */ + private $resizedImagePath; /** @var string */ private $preferredMimeType; @@ -72,12 +75,12 @@ public function __construct(string $imagePath, } /** - * Get the path of temporary image + * Get the path of orig image * * @return string */ - public function getTempPath(): string { - return $this->tempPath; + public function getImagePath(): string { + return $this->imagePath; } /** @@ -85,8 +88,17 @@ public function getTempPath(): string { * * @return string */ - public function getImagePath(): string { - return $this->imagePath; + public function getOrientedImagePath(): string { + return $this->orientedImagePath; + } + + /** + * Get the path of temporary image + * + * @return string + */ + public function getResizedImagePath(): string { + return $this->resizedImagePath; } /** @@ -119,7 +131,6 @@ public function clean() { */ private function prepareImage() { $this->loadFromFile($this->imagePath); - $this->fixOrientation(); if (!$this->valid()) { throw new \RuntimeException("Image is not valid, probably cannot be loaded"); @@ -131,10 +142,18 @@ private function prepareImage() { return; } + if ($this->getOrientation() > 1) { + $this->fixOrientation(); + $this->orientedImagePath = $this->tempManager->getTemporaryFile(); + $this->save($this->orientedImagePath, $this->preferredMimeType); + } else { + $this->orientedImagePath = $this->imagePath; + } + $this->ratio = $this->resizeImage(); - $this->tempPath = $this->tempManager->getTemporaryFile(); + $this->resizedImagePath = $this->tempManager->getTemporaryFile(); - $this->save($this->tempPath, $this->preferredMimeType); + $this->save($this->resizedImagePath, $this->preferredMimeType); } /**