From 56e47e7c30e84b0ff640af7a002a7790fd80e33a Mon Sep 17 00:00:00 2001 From: Pablo Gabarro Date: Mon, 24 Jun 2024 09:42:07 -0300 Subject: [PATCH] Removed try-catch from methods --- src/DeepFace.php | 154 ++++++++++++++++++++--------------------------- 1 file changed, 66 insertions(+), 88 deletions(-) diff --git a/src/DeepFace.php b/src/DeepFace.php index 8c8fa2d..265ada3 100644 --- a/src/DeepFace.php +++ b/src/DeepFace.php @@ -19,7 +19,6 @@ use Astrotomic\DeepFace\Enums\Race; use Astrotomic\DeepFace\Exceptions\DeepFaceException; use BadMethodCallException; -use Exception; use InvalidArgumentException; use SplFileInfo; use Symfony\Component\Process\ExecutableFinder; @@ -57,7 +56,7 @@ public function verify( bool $align = true, Normalization $normalization = Normalization::BASE, bool $anti_spoofing = false, - ): VerifyResult|array { + ): VerifyResult { $img1 = new SplFileInfo($img1_path); $img2 = new SplFileInfo($img2_path); @@ -68,25 +67,20 @@ public function verify( throw new InvalidArgumentException("The path [{$img2_path}] for image#2 is not a file."); } - try { - $output = $this->run( - filepath: __DIR__.'/../scripts/verify.py', - data: [ - '{{img1_path}}' => str_replace('\\', '/', $img1->getRealPath()), - '{{img2_path}}' => str_replace('\\', '/', $img2->getRealPath()), - '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', - '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', - '{{align}}' => $align ? 'True' : 'False', - '{{model_name}}' => $model_name->value, - '{{detector_backend}}' => $detector_backend->value, - '{{distance_metric}}' => $distance_metric->value, - '{{normalization}}' => $normalization->value, - ], - ); - } catch(DeepFaceException $e) { - // if any of these images fails the spoof detection, it will throw 'Exception while processing imgX_path' - return array("error" => $e->getMessage()); - } + $output = $this->run( + filepath: __DIR__.'/../scripts/verify.py', + data: [ + '{{img1_path}}' => str_replace('\\', '/', $img1->getRealPath()), + '{{img2_path}}' => str_replace('\\', '/', $img2->getRealPath()), + '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', + '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', + '{{align}}' => $align ? 'True' : 'False', + '{{model_name}}' => $model_name->value, + '{{detector_backend}}' => $detector_backend->value, + '{{distance_metric}}' => $distance_metric->value, + '{{normalization}}' => $normalization->value, + ], + ); return new VerifyResult( verified: $output['verified'], @@ -130,22 +124,18 @@ public function analyze( $actions ); - try { - $output = $this->run( - filepath: __DIR__.'/../scripts/analyze.py', - data: [ - '{{img_path}}' => str_replace('\\', '/', $img->getRealPath()), - '{{actions}}' => '['.implode(',', array_map(fn (AnalyzeAction $action) => "'{$action->value}'", $actions)).']', - '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', - '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', - '{{detector_backend}}' => $detector_backend->value, - '{{align}}' => $align ? 'True' : 'False', - '{{silent}}' => $silent ? 'True' : 'False', - ], - ); - } catch(DeepFaceException $e) { - return array("error" => $e->getMessage()); - } + $output = $this->run( + filepath: __DIR__.'/../scripts/analyze.py', + data: [ + '{{img_path}}' => str_replace('\\', '/', $img->getRealPath()), + '{{actions}}' => '['.implode(',', array_map(fn (AnalyzeAction $action) => "'{$action->value}'", $actions)).']', + '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', + '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', + '{{detector_backend}}' => $detector_backend->value, + '{{align}}' => $align ? 'True' : 'False', + '{{silent}}' => $silent ? 'True' : 'False', + ], + ); return array_map( fn (array $result) => new AnalyzeResult( @@ -182,22 +172,18 @@ public function extractFaces( throw new InvalidArgumentException("The path [{$img_path}] for image is not a file."); } - try { - $output = $this->run( - filepath: __DIR__.'/../scripts/extract_faces.py', - data: [ - '{{img_path}}' => str_replace('\\', '/', $img->getRealPath()), - '{{target_size}}' => '['.implode(',', $target_size).']', - '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', - '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', - '{{detector_backend}}' => $detector_backend->value, - '{{align}}' => $align ? 'True' : 'False', - '{{grayscale}}' => $grayscale ? 'True' : 'False', - ], - ); - } catch(DeepFaceException $e) { - return array("error" => $e->getMessage()); - } + $output = $this->run( + filepath: __DIR__.'/../scripts/extract_faces.py', + data: [ + '{{img_path}}' => str_replace('\\', '/', $img->getRealPath()), + '{{target_size}}' => '['.implode(',', $target_size).']', + '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', + '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', + '{{detector_backend}}' => $detector_backend->value, + '{{align}}' => $align ? 'True' : 'False', + '{{grayscale}}' => $grayscale ? 'True' : 'False', + ], + ); return array_map( fn (array $result) => new ExtractFaceResult( @@ -236,25 +222,21 @@ public function find( throw new InvalidArgumentException("The path [{$db_path}] for database is not a directory."); } - try { - $output = $this->run( - filepath: __DIR__.'/../scripts/find.py', - data: [ - '{{img_path}}' => str_replace('\\', '/', $img->getRealPath()), - '{{db_path}}' => $db->getRealPath(), - '{{model_name}}' => $model_name->value, - '{{distance_metric}}' => $distance_metric->value, - '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', - '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', - '{{detector_backend}}' => $detector_backend->value, - '{{align}}' => $align ? 'True' : 'False', - '{{normalization}}' => $normalization->value, - '{{silent}}' => $silent ? 'True' : 'False', - ], - ); - } catch(DeepFaceException $e) { - return array("error" => $e->getMessage()); - } + $output = $this->run( + filepath: __DIR__.'/../scripts/find.py', + data: [ + '{{img_path}}' => str_replace('\\', '/', $img->getRealPath()), + '{{db_path}}' => $db->getRealPath(), + '{{model_name}}' => $model_name->value, + '{{distance_metric}}' => $distance_metric->value, + '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', + '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', + '{{detector_backend}}' => $detector_backend->value, + '{{align}}' => $align ? 'True' : 'False', + '{{normalization}}' => $normalization->value, + '{{silent}}' => $silent ? 'True' : 'False', + ], + ); $result = []; foreach ($output['identity'] as $i => $identity) { @@ -297,22 +279,18 @@ public function represent( throw new InvalidArgumentException("The path [{$img_path}] for image is not a file."); } - try { - $output = $this->run( - filepath: __DIR__.'/../scripts/represent.py', - data: [ - '{{img_path}}' => str_replace('\\', '/', $img->getRealPath()), - '{{model_name}}' => $model_name->value, - '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', - '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', - '{{detector_backend}}' => $detector_backend->value, - '{{align}}' => $align ? 'True' : 'False', - '{{normalization}}' => $normalization->value, - ], - ); - } catch(DeepFaceException $e) { - return array("error" => $e->getMessage()); - } + $output = $this->run( + filepath: __DIR__.'/../scripts/represent.py', + data: [ + '{{img_path}}' => str_replace('\\', '/', $img->getRealPath()), + '{{model_name}}' => $model_name->value, + '{{enforce_detection}}' => $enforce_detection ? 'True' : 'False', + '{{anti_spoofing}}' => $anti_spoofing ? 'True' : 'False', + '{{detector_backend}}' => $detector_backend->value, + '{{align}}' => $align ? 'True' : 'False', + '{{normalization}}' => $normalization->value, + ], + ); return array_map( fn (array $result) => new RepresentResult(