From de73e514fd279eb0bfd9066704c3338fcd501cdf Mon Sep 17 00:00:00 2001 From: CheeseSucker Date: Sun, 13 Mar 2016 20:40:33 +0100 Subject: [PATCH 1/7] Actually use $measureImage in CropBalanced $measureImage was created, but never used in CropBalanced. In my tests, this code produces better crops with lower risk chopping heads and splitting interesting objects. --- src/stojg/crop/CropBalanced.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/stojg/crop/CropBalanced.php b/src/stojg/crop/CropBalanced.php index ac2967f..059c01b 100644 --- a/src/stojg/crop/CropBalanced.php +++ b/src/stojg/crop/CropBalanced.php @@ -44,9 +44,9 @@ protected function getRandomEdgeOffset(\Imagick $original, $targetWidth, $target // Turn image into a grayscale $measureImage->modulateImage(100, 0, 100); // Turn everything darker than this to pitch black - $measureImage->blackThresholdImage("#101010"); + $measureImage->blackThresholdImage("#070707"); // Get the calculated offset for cropping - return $this->getOffsetBalanced($targetWidth, $targetHeight); + return $this->getOffsetBalanced2($measureImage, $targetWidth, $targetHeight); } /** @@ -58,6 +58,17 @@ protected function getRandomEdgeOffset(\Imagick $original, $targetWidth, $target */ public function getOffsetBalanced($targetWidth, $targetHeight) { + return $this->getOffsetBalanced2($this->originalImage, $targetWidth, $targetHeight); + } + + /** + * @param \Imagick $image + * @param $targetWidth + * @param $targetHeight + * @return array + * @throws \Exception + */ + protected function getOffsetBalanced2(\Imagick $image, $targetWidth, $targetHeight) { $size = $this->originalImage->getImageGeometry(); $points = array(); @@ -66,25 +77,25 @@ public function getOffsetBalanced($targetWidth, $targetHeight) $halfHeight = ceil($size['height']/2); // First quadrant - $clone = clone($this->originalImage); + $clone = clone($image); $clone->cropimage($halfWidth, $halfHeight, 0, 0); $point = $this->getHighestEnergyPoint($clone); $points[] = array('x' => $point['x'], 'y' => $point['y'], 'sum' => $point['sum']); // Second quadrant - $clone = clone($this->originalImage); + $clone = clone($image); $clone->cropimage($halfWidth, $halfHeight, $halfWidth, 0); $point = $this->getHighestEnergyPoint($clone); $points[] = array('x' => $point['x']+$halfWidth, 'y' => $point['y'], 'sum' => $point['sum']); // Third quadrant - $clone = clone($this->originalImage); + $clone = clone($image); $clone->cropimage($halfWidth, $halfHeight, 0, $halfHeight); $point = $this->getHighestEnergyPoint($clone); $points[] = array('x' => $point['x'], 'y' => $point['y']+$halfHeight, 'sum' => $point['sum']); // Fourth quadrant - $clone = clone($this->originalImage); + $clone = clone($image); $clone->cropimage($halfWidth, $halfHeight, $halfWidth, $halfHeight); $point = $point = $this->getHighestEnergyPoint($clone); $points[] = array('x' => $point['x']+$halfWidth, 'y' => $point['y']+$halfHeight, 'sum' => $point['sum']); From 8fac21b01f086b7bba0d547286914e005e34aae7 Mon Sep 17 00:00:00 2001 From: CheeseSucker Date: Sat, 19 Mar 2016 11:41:27 +0100 Subject: [PATCH 2/7] Get geometry from image passed as parameter --- src/stojg/crop/CropBalanced.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stojg/crop/CropBalanced.php b/src/stojg/crop/CropBalanced.php index 059c01b..0fa1dc9 100644 --- a/src/stojg/crop/CropBalanced.php +++ b/src/stojg/crop/CropBalanced.php @@ -69,7 +69,7 @@ public function getOffsetBalanced($targetWidth, $targetHeight) * @throws \Exception */ protected function getOffsetBalanced2(\Imagick $image, $targetWidth, $targetHeight) { - $size = $this->originalImage->getImageGeometry(); + $size = $image->getImageGeometry(); $points = array(); From 1c4c1e8f40d4ac4a7af911bb5de558daafe5a0eb Mon Sep 17 00:00:00 2001 From: christian Date: Fri, 27 Mar 2020 09:35:57 +0100 Subject: [PATCH 3/7] fixed an error when the source image have a very plain color and it can't found the most energy point. --- src/stojg/crop/CropBalanced.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/stojg/crop/CropBalanced.php b/src/stojg/crop/CropBalanced.php index 0fa1dc9..1dd2072 100644 --- a/src/stojg/crop/CropBalanced.php +++ b/src/stojg/crop/CropBalanced.php @@ -111,11 +111,15 @@ function ($result, $array) { $centerX = 0; $centerY = 0; - // Calulate the mean weighted center x and y - $totalPoints = count($points); - for ($idx=0; $idx < $totalPoints; $idx++) { - $centerX += $points[$idx]['x'] * ($points[$idx]['sum'] / $totalWeight); - $centerY += $points[$idx]['y'] * ($points[$idx]['sum'] / $totalWeight); + // If we found a center point, made the calculations to found the coords + if ($totalWeight) + { + // Calulate the mean weighted center x and y + $totalPoints = count($points); + for ($idx=0; $idx < $totalPoints; $idx++) { + $centerX += $points[$idx]['x'] * ($points[$idx]['sum'] / $totalWeight); + $centerY += $points[$idx]['y'] * ($points[$idx]['sum'] / $totalWeight); + } } // From the weighted center point to the topleft corner of the crop would be From 47fa4cc8f852105964ff8c91ce33d93ed738e24b Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Wed, 15 Mar 2017 16:25:56 +0100 Subject: [PATCH 4/7] fix notice when face_detect return false or null --- src/stojg/crop/CropFace.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/stojg/crop/CropFace.php b/src/stojg/crop/CropFace.php index a0593ed..e055f1b 100644 --- a/src/stojg/crop/CropFace.php +++ b/src/stojg/crop/CropFace.php @@ -75,6 +75,9 @@ protected function getFaceList() protected function getFaceListFromClassifier($classifier) { $faceList = face_detect($this->imagePath, __DIR__ . $classifier); + if (!$faceList) { + $faceList = array(); + } return $faceList; } From 3e05ee342486b92ef7c4e062a674f907c4bbb78b Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Fri, 27 Mar 2020 10:02:52 +0100 Subject: [PATCH 5/7] lint --- src/stojg/crop/CropBalanced.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/stojg/crop/CropBalanced.php b/src/stojg/crop/CropBalanced.php index 1dd2072..ef7c213 100644 --- a/src/stojg/crop/CropBalanced.php +++ b/src/stojg/crop/CropBalanced.php @@ -112,14 +112,13 @@ function ($result, $array) { $centerY = 0; // If we found a center point, made the calculations to found the coords - if ($totalWeight) - { - // Calulate the mean weighted center x and y - $totalPoints = count($points); - for ($idx=0; $idx < $totalPoints; $idx++) { - $centerX += $points[$idx]['x'] * ($points[$idx]['sum'] / $totalWeight); - $centerY += $points[$idx]['y'] * ($points[$idx]['sum'] / $totalWeight); - } + if ($totalWeight) { + // Calulate the mean weighted center x and y + $totalPoints = count($points); + for ($idx=0; $idx < $totalPoints; $idx++) { + $centerX += $points[$idx]['x'] * ($points[$idx]['sum'] / $totalWeight); + $centerY += $points[$idx]['y'] * ($points[$idx]['sum'] / $totalWeight); + } } // From the weighted center point to the topleft corner of the crop would be From e4f616a09fe2d2fde49e852b507057fc90607d88 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Mon, 24 Apr 2017 09:16:51 +0200 Subject: [PATCH 6/7] add a fallback to avoid hitting max execution time --- src/stojg/crop/CropFace.php | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/stojg/crop/CropFace.php b/src/stojg/crop/CropFace.php index e055f1b..9b2e4cf 100644 --- a/src/stojg/crop/CropFace.php +++ b/src/stojg/crop/CropFace.php @@ -32,6 +32,14 @@ class CropFace extends CropEntropy */ protected $safeZoneList; + /** + * max execution time (in seconds) + * + * @var int + * @access protected + */ + protected $maxExecutionTime; + /** * * @param string $imagePath @@ -42,6 +50,18 @@ public function __construct($imagePath) parent::__construct($imagePath); } + /** + * setMaxExecutionTime + * + * @param int $maxExecutionTime max execution time (in sec) + * @access public + * @return void + */ + public function setMaxExecutionTime($maxExecutionTime) + { + $this->maxExecutionTime = $maxExecutionTime; + } + /** * getFaceList get faces positions and sizes * @@ -56,11 +76,18 @@ protected function getFaceList() throw new \Exception($msg); } + if ($this->maxExecutionTime) { + $timeBefore = microtime(true); + } $faceList = $this->getFaceListFromClassifier(self::CLASSIFIER_FACE); + if ($this->maxExecutionTime) { + $timeSpent = microtime(true) - $timeBefore; + } - $profileList = $this->getFaceListFromClassifier(self::CLASSIFIER_PROFILE); - - $faceList = array_merge($faceList, $profileList); + if (!$this->maxExecutionTime || $timeSpent < ($this->maxExecutionTime / 2)) { + $profileList = $this->getFaceListFromClassifier(self::CLASSIFIER_PROFILE); + $faceList = array_merge($faceList, $profileList); + } return $faceList; } From c299a1013ccfb9084ffe76f76d57d013521db2f9 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Fri, 27 Mar 2020 10:46:22 +0100 Subject: [PATCH 7/7] rename function + make it private --- src/stojg/crop/CropBalanced.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/stojg/crop/CropBalanced.php b/src/stojg/crop/CropBalanced.php index ef7c213..892e076 100644 --- a/src/stojg/crop/CropBalanced.php +++ b/src/stojg/crop/CropBalanced.php @@ -46,7 +46,7 @@ protected function getRandomEdgeOffset(\Imagick $original, $targetWidth, $target // Turn everything darker than this to pitch black $measureImage->blackThresholdImage("#070707"); // Get the calculated offset for cropping - return $this->getOffsetBalanced2($measureImage, $targetWidth, $targetHeight); + return $this->getOffsetBalancedForImage($measureImage, $targetWidth, $targetHeight); } /** @@ -58,7 +58,7 @@ protected function getRandomEdgeOffset(\Imagick $original, $targetWidth, $target */ public function getOffsetBalanced($targetWidth, $targetHeight) { - return $this->getOffsetBalanced2($this->originalImage, $targetWidth, $targetHeight); + return $this->getOffsetBalancedForImage($this->originalImage, $targetWidth, $targetHeight); } /** @@ -68,7 +68,8 @@ public function getOffsetBalanced($targetWidth, $targetHeight) * @return array * @throws \Exception */ - protected function getOffsetBalanced2(\Imagick $image, $targetWidth, $targetHeight) { + private function getOffsetBalancedForImage(\Imagick $image, $targetWidth, $targetHeight) + { $size = $image->getImageGeometry(); $points = array();