Skip to content

Commit

Permalink
fix all commands for latest deepface version
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Dec 11, 2024
1 parent bc26991 commit cfa8219
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 70 deletions.
3 changes: 0 additions & 3 deletions pint.json

This file was deleted.

16 changes: 9 additions & 7 deletions scripts/analyze.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import json;
import sys;
import json;
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}},
actions = {{actions}},
enforce_detection = {{enforce_detection}},
detector_backend = "{{detector_backend}}",
align = {{align}},
expand_percentage = {{expand_percentage}},
silent = {{silent}},
anti_spoofing = {{anti_spoofing}},
);

print(json.dumps(result, default=str))
except ValueError as e:
print(json.dumps({"error": str(e)}), file=sys.stderr)
16 changes: 9 additions & 7 deletions scripts/extract_faces.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import json;
import sys;
import json;
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}},
detector_backend = "{{detector_backend}}",
enforce_detection = {{enforce_detection}},
align = {{align}},
expand_percentage = {{expand_percentage}},
grayscale = {{grayscale}},
color_face = "{{color_face}}",
normalize_face = {{normalize_face}},
anti_spoofing = {{anti_spoofing}},
);

print(json.dumps(result, default=str))
Expand Down
11 changes: 8 additions & 3 deletions scripts/find.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from deepface import DeepFace;
import sys;
import json;
from deepface import DeepFace;

try:
result = DeepFace.find(
Expand All @@ -8,11 +9,15 @@
model_name = "{{model_name}}",
distance_metric = "{{distance_metric}}",
enforce_detection = {{enforce_detection}},
anti_spoofing={{anti_spoofing}},
detector_backend = "{{detector_backend}}",
align = {{align}},
expand_percentage = {{expand_percentage}},
threshold = {{threshold}},
normalization = "{{normalization}}",
silent = {{silent}}
silent = {{silent}},
refresh_database = {{refresh_database}},
anti_spoofing = {{anti_spoofing}},
batched = {{batched}},
);

print(result[0].to_json())
Expand Down
8 changes: 5 additions & 3 deletions scripts/represent.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import json;
import sys;
import json;
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}}"
expand_percentage = {{expand_percentage}},
normalization = "{{normalization}}",
anti_spoofing = "{{anti_spoofing}}",
max_faces = {{max_faces}},
);

print(json.dumps(result, default=str))
Expand Down
13 changes: 8 additions & 5 deletions scripts/verify.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import json;
import sys;
import json;
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}}"
enforce_detection = {{enforce_detection}},
align = {{align}},
expand_percentage = {{expand_percentage}},
normalization = "{{normalization}}",
silent = {{silent}},
threshold = {{threshold}},
anti_spoofing = {{anti_spoofing}},
);

print(json.dumps(result, default=str))
Expand Down
125 changes: 83 additions & 42 deletions src/DeepFace.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Astrotomic\DeepFace\Data\RepresentResult;
use Astrotomic\DeepFace\Data\VerifyResult;
use Astrotomic\DeepFace\Enums\AnalyzeAction;
use Astrotomic\DeepFace\Enums\ColorFace;
use Astrotomic\DeepFace\Enums\Detector;
use Astrotomic\DeepFace\Enums\DistanceMetric;
use Astrotomic\DeepFace\Enums\Emotion;
Expand All @@ -18,6 +19,7 @@
use Astrotomic\DeepFace\Enums\Normalization;
use Astrotomic\DeepFace\Enums\Race;
use Astrotomic\DeepFace\Exceptions\DeepFaceException;
use BackedEnum;
use BadMethodCallException;
use InvalidArgumentException;
use SplFileInfo;
Expand All @@ -28,9 +30,9 @@ class DeepFace
{
protected ?string $python = null;

public function __construct(string $python = null)
public function __construct(?string $python = null)
{
$this->python = $python ?? (new ExecutableFinder())->find(
$this->python = $python ?? (new ExecutableFinder)->find(
name: 'python3',
default: 'python3',
);
Expand All @@ -54,7 +56,10 @@ public function verify(
DistanceMetric $distance_metric = DistanceMetric::COSINE,
bool $enforce_detection = true,
bool $align = true,
int $expand_percentage = 0,
Normalization $normalization = Normalization::BASE,
bool $silent = false,
?int $threshold = null,
bool $anti_spoofing = false,
): VerifyResult {
$img1 = new SplFileInfo($img1_path);
Expand All @@ -72,13 +77,16 @@ public function verify(
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,
'{{enforce_detection}}' => $enforce_detection,
'{{anti_spoofing}}' => $anti_spoofing,
'{{align}}' => $align,
'{{model_name}}' => $model_name,
'{{detector_backend}}' => $detector_backend,
'{{distance_metric}}' => $distance_metric,
'{{normalization}}' => $normalization,
'{{expand_percentage}}' => $expand_percentage,
'{{silent}}' => $silent,
'{{threshold}}' => $threshold,
],
);

Expand Down Expand Up @@ -106,6 +114,7 @@ public function analyze(
bool $enforce_detection = true,
Detector $detector_backend = Detector::OPENCV,
bool $align = true,
int $expand_percentage = 0,
bool $silent = false,
bool $anti_spoofing = false,
): array {
Expand All @@ -129,11 +138,12 @@ public function analyze(
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',
'{{enforce_detection}}' => $enforce_detection,
'{{anti_spoofing}}' => $anti_spoofing,
'{{detector_backend}}' => $detector_backend,
'{{align}}' => $align,
'{{silent}}' => $silent,
'{{expand_percentage}}' => $expand_percentage,
],
);

Expand All @@ -159,11 +169,13 @@ public function analyze(
*/
public function extractFaces(
string $img_path,
array $target_size = [224, 224],
Detector $detector_backend = Detector::OPENCV,
bool $enforce_detection = true,
bool $align = true,
int $expand_percentage = 0,
bool $grayscale = false,
ColorFace $color_face = ColorFace::RGB,
bool $normalize_face = true,
bool $anti_spoofing = false,
): array {
$img = new SplFileInfo($img_path);
Expand All @@ -176,12 +188,14 @@ public function extractFaces(
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',
'{{enforce_detection}}' => $enforce_detection,
'{{anti_spoofing}}' => $anti_spoofing,
'{{detector_backend}}' => $detector_backend,
'{{align}}' => $align,
'{{grayscale}}' => $grayscale,
'{{expand_percentage}}' => $expand_percentage,
'{{color_face}}' => $color_face,
'{{normalize_face}}' => $normalize_face,
],
);

Expand All @@ -207,9 +221,13 @@ public function find(
bool $enforce_detection = true,
Detector $detector_backend = Detector::OPENCV,
bool $align = true,
int $expand_percentage = 0,
?float $threshold = null,
Normalization $normalization = Normalization::BASE,
bool $silent = false,
bool $refresh_database = true,
bool $anti_spoofing = false,
bool $batched = false,
): array {
$img = new SplFileInfo($img_path);
$db = new SplFileInfo($db_path);
Expand All @@ -227,14 +245,18 @@ public function find(
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',
'{{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,
'{{expand_percentage}}' => $expand_percentage,
'{{threshold}}' => $threshold,
'{{refresh_database}}' => $refresh_database,
'{{batched}}' => $batched,
],
);

Expand Down Expand Up @@ -270,8 +292,10 @@ public function represent(
bool $enforce_detection = true,
Detector $detector_backend = Detector::OPENCV,
bool $align = true,
int $expand_percentage = 0,
Normalization $normalization = Normalization::BASE,
bool $anti_spoofing = false,
?int $max_faces = null,
): array {
$img = new SplFileInfo($img_path);

Expand All @@ -283,12 +307,14 @@ public function represent(
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,
'{{model_name}}' => $model_name,
'{{enforce_detection}}' => $enforce_detection,
'{{anti_spoofing}}' => $anti_spoofing,
'{{detector_backend}}' => $detector_backend,
'{{align}}' => $align,
'{{normalization}}' => $normalization,
'{{expand_percentage}}' => $expand_percentage,
'{{max_faces}}' => $max_faces,
],
);

Expand All @@ -309,21 +335,25 @@ protected function run(string $filepath, array $data): array|bool
$script = $this->script($filepath, $data);
$process = $this->process($script);

$output = $process
->mustRun()
->getOutput();
try {
$output = $process
->mustRun()
->getOutput();
} finally {
unlink($script);
}

$errorOutput = $process->getErrorOutput();

if(!empty($errorOutput)) {
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']);
} else {
throw new DeepFaceException("Failed to parse error message: " . $lastJson);
throw new DeepFaceException('Failed to parse error message: '.$lastJson);
}
}
}
Expand All @@ -347,7 +377,6 @@ protected function process(string $script): Process
{
$process = new Process([
$this->python,
'-c',
$script,
]);

Expand All @@ -358,10 +387,22 @@ protected function process(string $script): Process

protected function script(string $filepath, $data): string
{
$data = array_map(fn (mixed $value): string => match (true) {
$value instanceof BackedEnum => $value->value,
is_bool($value) => $value ? 'True' : 'False',
is_null($value) => 'None',
default => (string) $value,
}, $data);

$template = file_get_contents($filepath);

$script = trim(strtr($template, $data));

return str_replace(PHP_EOL, ' ', $script);
$py = tempnam(sys_get_temp_dir(), 'deepface');

file_put_contents($py, $script);
chmod($py, 0666);

return $py;
}
}
Loading

0 comments on commit cfa8219

Please sign in to comment.