From 74ac105feb5bb21bc5d19e19dab5191320ad7dce Mon Sep 17 00:00:00 2001 From: "pablo.gbr" Date: Thu, 4 Jul 2024 08:32:31 -0300 Subject: [PATCH 1/3] Removed try-except from scripts, handle ProcessFailed Exception --- scripts/analyze.py | 24 +++++++-------- scripts/extract_faces.py | 24 +++++++-------- scripts/find.py | 30 ++++++++----------- scripts/represent.py | 25 +++++++--------- scripts/verify.py | 28 ++++++++---------- src/DeepFace.php | 64 +++++++++++++++++++++------------------- 6 files changed, 88 insertions(+), 107 deletions(-) diff --git a/scripts/analyze.py b/scripts/analyze.py index 02d3964..7f09920 100644 --- a/scripts/analyze.py +++ b/scripts/analyze.py @@ -1,17 +1,13 @@ import json; -import sys; from deepface import DeepFace; -try: - result = DeepFace.analyze( - img_path = "{{img_path}}", - actions={{actions}}, - enforce_detection={{enforce_detection}}, - anti_spoofing={{anti_spoofing}}, - detector_backend="{{detector_backend}}", - align={{align}}, - silent={{silent}}, - ); - print(json.dumps(result, default=str)) -except ValueError as e: - print(json.dumps({"error": str(e)}), file=sys.stderr) +result = DeepFace.analyze( + img_path = "{{img_path}}", + actions={{actions}}, + enforce_detection={{enforce_detection}}, + anti_spoofing={{anti_spoofing}}, + detector_backend="{{detector_backend}}", + align={{align}}, + silent={{silent}}, +); +print(json.dumps(result, default=str)) \ No newline at end of file diff --git a/scripts/extract_faces.py b/scripts/extract_faces.py index e4fad15..cb8e306 100644 --- a/scripts/extract_faces.py +++ b/scripts/extract_faces.py @@ -1,18 +1,14 @@ import json; -import sys; from deepface import DeepFace; -try: - result = DeepFace.extract_faces( - img_path = "{{img_path}}", - target_size={{target_size}}, - enforce_detection={{enforce_detection}}, - anti_spoofing={{anti_spoofing}}, - detector_backend="{{detector_backend}}", - align={{align}}, - grayscale={{grayscale}}, - ); +result = DeepFace.extract_faces( + img_path = "{{img_path}}", + target_size={{target_size}}, + enforce_detection={{enforce_detection}}, + anti_spoofing={{anti_spoofing}}, + detector_backend="{{detector_backend}}", + align={{align}}, + grayscale={{grayscale}}, +); - print(json.dumps(result, default=str)) -except ValueError as e: - print(json.dumps({"error": str(e)}), file=sys.stderr) +print(json.dumps(result, default=str)) \ No newline at end of file diff --git a/scripts/find.py b/scripts/find.py index 1dbf89d..9c2dc53 100644 --- a/scripts/find.py +++ b/scripts/find.py @@ -1,20 +1,16 @@ from deepface import DeepFace; -import sys; -try: - result = DeepFace.find( - img_path = "{{img_path}}", - db_path = "{{db_path}}", - model_name = "{{model_name}}", - distance_metric = "{{distance_metric}}", - enforce_detection = {{enforce_detection}}, - anti_spoofing={{anti_spoofing}}, - detector_backend = "{{detector_backend}}", - align = {{align}}, - normalization = "{{normalization}}", - silent = {{silent}} - ); +result = DeepFace.find( + img_path = "{{img_path}}", + db_path = "{{db_path}}", + model_name = "{{model_name}}", + distance_metric = "{{distance_metric}}", + enforce_detection = {{enforce_detection}}, + anti_spoofing={{anti_spoofing}}, + detector_backend = "{{detector_backend}}", + align = {{align}}, + normalization = "{{normalization}}", + silent = {{silent}} +); - print(result[0].to_json()) -except ValueError as e: - print(json.dumps({"error": str(e)}), file=sys.stderr) +print(result[0].to_json()) diff --git a/scripts/represent.py b/scripts/represent.py index 18855dc..a7c675f 100644 --- a/scripts/represent.py +++ b/scripts/represent.py @@ -1,19 +1,14 @@ import json; -import sys; from deepface import DeepFace; -try: - result = DeepFace.represent( - img_path = "{{img_path}}", - model_name = "{{model_name}}", - enforce_detection = {{enforce_detection}}, - anti_spoofing = "{{anti_spoofing}}", - detector_backend = "{{detector_backend}}", - align = {{align}}, - normalization = "{{normalization}}" - ); - - print(json.dumps(result, default=str)) -except ValueError as e: - print(json.dumps({"error": str(e)}), file=sys.stderr) +result = DeepFace.represent( + img_path = "{{img_path}}", + model_name = "{{model_name}}", + enforce_detection = {{enforce_detection}}, + anti_spoofing = "{{anti_spoofing}}", + detector_backend = "{{detector_backend}}", + align = {{align}}, + normalization = "{{normalization}}" +); +print(json.dumps(result, default=str)) diff --git a/scripts/verify.py b/scripts/verify.py index cd6d9ec..3086bff 100644 --- a/scripts/verify.py +++ b/scripts/verify.py @@ -1,20 +1,16 @@ import json; -import sys; from deepface import DeepFace; -try: - result = DeepFace.verify( - img1_path = "{{img1_path}}", - img2_path = "{{img2_path}}", - enforce_detection = {{enforce_detection}}, - anti_spoofing={{anti_spoofing}}, - align = {{align}}, - model_name = "{{model_name}}", - detector_backend = "{{detector_backend}}", - distance_metric = "{{distance_metric}}", - normalization = "{{normalization}}" - ); +result = DeepFace.verify( + img1_path = "{{img1_path}}", + img2_path = "{{img2_path}}", + enforce_detection = {{enforce_detection}}, + anti_spoofing={{anti_spoofing}}, + align = {{align}}, + model_name = "{{model_name}}", + detector_backend = "{{detector_backend}}", + distance_metric = "{{distance_metric}}", + normalization = "{{normalization}}" +); - print(json.dumps(result, default=str)) -except ValueError as e: - print(json.dumps({"error": str(e)}), file=sys.stderr) +print(json.dumps(result, default=str)) diff --git a/src/DeepFace.php b/src/DeepFace.php index 265ada3..3402729 100644 --- a/src/DeepFace.php +++ b/src/DeepFace.php @@ -19,8 +19,10 @@ use Astrotomic\DeepFace\Enums\Race; use Astrotomic\DeepFace\Exceptions\DeepFaceException; use BadMethodCallException; +use Exception; use InvalidArgumentException; use SplFileInfo; +use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\ExecutableFinder; use Symfony\Component\Process\Process; @@ -306,41 +308,41 @@ public function represent( protected function run(string $filepath, array $data): array|bool { - $script = $this->script($filepath, $data); - $process = $this->process($script); - - $output = $process - ->mustRun() - ->getOutput(); - - $errorOutput = $process->getErrorOutput(); - - if(!empty($errorOutput)) { - if (preg_match_all('/\{(?:[^{}]|(?R))*\}/', $errorOutput, $matches)) { - $lastJson = end($matches[0]); - $errorResult = json_decode($lastJson, true); - - if ($errorResult !== null && isset($errorResult['error'])) { - throw new DeepFaceException($errorResult['error']); + try{ + $script = $this->script($filepath, $data); + $process = $this->process($script); + + $output = $process + ->mustRun() + ->getOutput(); + + $lines = array_values(array_filter(explode(PHP_EOL, $output), function (string $line): bool { + json_decode($line, true); + + return json_last_error() === JSON_ERROR_NONE; + })); + + if (empty($lines)) { + throw new BadMethodCallException('Python deepface script has not returned with any JSON.'); + } + + $json = $lines[0]; + + return json_decode($json, true, 512, JSON_THROW_ON_ERROR); + } catch(Exception $e){ + if($e instanceof ProcessFailedException){ + $fullMessage = $e->getMessage(); + + if (preg_match('/ValueError: (.*)/', $fullMessage, $matches)) { + $errorMessage = $matches[1]; + throw new DeepFaceException(trim(strval($errorMessage))); } else { - throw new DeepFaceException("Failed to parse error message: " . $lastJson); + throw new BadMethodCallException("Couldn't get the error message."); } + } else { + throw new BadMethodCallException("Something went wrong."); } } - - $lines = array_values(array_filter(explode(PHP_EOL, $output), function (string $line): bool { - json_decode($line, true); - - return json_last_error() === JSON_ERROR_NONE; - })); - - if (empty($lines)) { - throw new BadMethodCallException('Python deepface script has not returned with any JSON.'); - } - - $json = $lines[0]; - - return json_decode($json, true, 512, JSON_THROW_ON_ERROR); } protected function process(string $script): Process From b03bfa743507f3a821cda34b21e34e6a72823808 Mon Sep 17 00:00:00 2001 From: "pablo.gbr" Date: Thu, 4 Jul 2024 09:36:34 -0300 Subject: [PATCH 2/3] Composer fix --- src/DeepFace.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/DeepFace.php b/src/DeepFace.php index 3402729..e90bb7e 100644 --- a/src/DeepFace.php +++ b/src/DeepFace.php @@ -308,31 +308,31 @@ public function represent( protected function run(string $filepath, array $data): array|bool { - try{ + try { $script = $this->script($filepath, $data); $process = $this->process($script); - + $output = $process ->mustRun() ->getOutput(); - + $lines = array_values(array_filter(explode(PHP_EOL, $output), function (string $line): bool { json_decode($line, true); - + return json_last_error() === JSON_ERROR_NONE; })); - + if (empty($lines)) { throw new BadMethodCallException('Python deepface script has not returned with any JSON.'); } - + $json = $lines[0]; - + return json_decode($json, true, 512, JSON_THROW_ON_ERROR); - } catch(Exception $e){ - if($e instanceof ProcessFailedException){ + } catch(Exception $e) { + if($e instanceof ProcessFailedException) { $fullMessage = $e->getMessage(); - + if (preg_match('/ValueError: (.*)/', $fullMessage, $matches)) { $errorMessage = $matches[1]; throw new DeepFaceException(trim(strval($errorMessage))); From 753c1049c35dae9c9dafa8bc374c158e01def24a Mon Sep 17 00:00:00 2001 From: "pablo.gbr" Date: Mon, 15 Jul 2024 11:31:59 -0300 Subject: [PATCH 3/3] Update newline replace --- src/DeepFace.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/DeepFace.php b/src/DeepFace.php index e90bb7e..deab31d 100644 --- a/src/DeepFace.php +++ b/src/DeepFace.php @@ -337,9 +337,11 @@ protected function run(string $filepath, array $data): array|bool $errorMessage = $matches[1]; throw new DeepFaceException(trim(strval($errorMessage))); } else { + // Should return this or $fullMessage? throw new BadMethodCallException("Couldn't get the error message."); } } else { + // Should return the exception message? throw new BadMethodCallException("Something went wrong."); } } @@ -364,6 +366,8 @@ protected function script(string $filepath, $data): string $script = trim(strtr($template, $data)); - return str_replace(PHP_EOL, ' ', $script); + $newline_regex = '/(\r\n)|\r|\n/'; + + return preg_replace($newline_regex, ' ', $script); } }