Skip to content

Commit

Permalink
Create another temporary file just with rotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasdelellis committed Jul 21, 2020
1 parent 221cfea commit 8c76771
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/BackgroundJob/Tasks/ImageProcessingTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand Down
37 changes: 28 additions & 9 deletions lib/Helper/TempImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class TempImage extends Image {
private $imagePath;

/** @var string */
private $tempPath;
private $orientedImagePath;

/** @var string */
private $resizedImagePath;

/** @var string */
private $preferredMimeType;
Expand Down Expand Up @@ -72,21 +75,30 @@ 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;
}

/**
* Get the path of orig image
*
* @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;
}

/**
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 8c76771

Please sign in to comment.