Skip to content

Commit

Permalink
Merge pull request #4 from pablo-gbr/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-gbr authored Jun 24, 2024
2 parents 48df309 + 50389b8 commit 48f78ef
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
1 change: 1 addition & 0 deletions scripts/extract_faces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json;
import sys;
from deepface import DeepFace;

try:
Expand Down
2 changes: 2 additions & 0 deletions scripts/represent.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json;
import sys;
from deepface import DeepFace;

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}}"
Expand Down
1 change: 1 addition & 0 deletions scripts/verify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json;
import sys;
from deepface import DeepFace;

try:
Expand Down
46 changes: 26 additions & 20 deletions src/DeepFace.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
use Astrotomic\DeepFace\Enums\Gender;
use Astrotomic\DeepFace\Enums\Normalization;
use Astrotomic\DeepFace\Enums\Race;
use Astrotomic\DeepFace\Exceptions\DeepFaceException;
use BadMethodCallException;
use Exception;
use InvalidArgumentException;
use SplFileInfo;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

class DeepFaceAnalysisException extends Exception {}

class DeepFace
{
protected ?string $python = null;
Expand Down Expand Up @@ -58,7 +57,7 @@ public function verify(
bool $align = true,
Normalization $normalization = Normalization::BASE,
bool $anti_spoofing = false,
): VerifyResult {
): VerifyResult|array {
$img1 = new SplFileInfo($img1_path);
$img2 = new SplFileInfo($img2_path);

Expand All @@ -85,7 +84,8 @@ public function verify(
'{{normalization}}' => $normalization->value,
],
);
} catch(DeepFaceAnalysisException $e){
} 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());
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public function analyze(
'{{silent}}' => $silent ? 'True' : 'False',
],
);
} catch(DeepFaceAnalysisException $e){
} catch(DeepFaceException $e){
return array("error" => $e->getMessage());
}

Expand Down Expand Up @@ -196,7 +196,7 @@ public function extractFaces(
'{{grayscale}}' => $grayscale ? 'True' : 'False',
],
);
} catch(DeepFaceAnalysisException $e){
} catch(DeepFaceException $e){
return array("error" => $e->getMessage());
}

Expand Down Expand Up @@ -253,7 +253,7 @@ public function find(
'{{silent}}' => $silent ? 'True' : 'False',
],
);
} catch(DeepFaceAnalysisException $e){
} catch(DeepFaceException $e){
return array("error" => $e->getMessage());
}

Expand Down Expand Up @@ -290,24 +290,30 @@ public function represent(
Detector $detector_backend = Detector::OPENCV,
bool $align = true,
Normalization $normalization = Normalization::BASE,
bool $anti_spoofing = false,
): array {
$img = new SplFileInfo($img_path);

if (! $img->isFile()) {
throw new InvalidArgumentException("The path [{$img_path}] for image is not a file.");
}

$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',
'{{detector_backend}}' => $detector_backend->value,
'{{align}}' => $align ? 'True' : 'False',
'{{normalization}}' => $normalization->value,
],
);
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());
}

return array_map(
fn (array $result) => new RepresentResult(
Expand Down Expand Up @@ -338,9 +344,9 @@ protected function run(string $filepath, array $data): array|bool
$errorResult = json_decode($lastJson, true);

if ($errorResult !== null && isset($errorResult['error'])) {
throw new DeepFaceAnalysisException($errorResult['error']); // should return 'Spoof detected in the given image'
throw new DeepFaceException($errorResult['error']); // should return 'Spoof detected in the given image'
} else {
throw new DeepFaceAnalysisException("Failed to parse error message: " . $lastJson);
throw new DeepFaceException("Failed to parse error message: " . $lastJson);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/DeepFaceGenericException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Astrotomic\DeepFace\Exceptions;

use Exception;

class DeepFaceException extends Exception
{

}

0 comments on commit 48f78ef

Please sign in to comment.