Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Features #14

Merged
merged 19 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions scripts/analyze.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import json;
import sys;
from deepface import DeepFace;

result = DeepFace.analyze(
img_path = "{{img_path}}",
actions={{actions}},
enforce_detection={{enforce_detection}},
detector_backend="{{detector_backend}}",
align={{align}},
silent={{silent}},
);

print(json.dumps(result, default=str))
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)
23 changes: 14 additions & 9 deletions scripts/extract_faces.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import json;
import sys;
from deepface import DeepFace;

result = DeepFace.extract_faces(
img_path = "{{img_path}}",
target_size={{target_size}},
enforce_detection={{enforce_detection}},
detector_backend="{{detector_backend}}",
align={{align}},
grayscale={{grayscale}},
);
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}},
);

print(json.dumps(result, default=str))
print(json.dumps(result, default=str))
except ValueError as e:
print(json.dumps({"error": str(e)}), file=sys.stderr)
29 changes: 17 additions & 12 deletions scripts/find.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from deepface import DeepFace;
import sys;

result = DeepFace.find(
img_path = "{{img_path}}",
db_path = "{{db_path}}",
model_name = "{{model_name}}",
distance_metric = "{{distance_metric}}",
enforce_detection = {{enforce_detection}},
detector_backend = "{{detector_backend}}",
align = {{align}},
normalization = "{{normalization}}",
silent = {{silent}}
);
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}}
);

print(result[0].to_json())
print(result[0].to_json())
except ValueError as e:
print(json.dumps({"error": str(e)}), file=sys.stderr)
24 changes: 15 additions & 9 deletions scripts/represent.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import json;
import sys;
Gummibeer marked this conversation as resolved.
Show resolved Hide resolved
from deepface import DeepFace;

result = DeepFace.represent(
img_path = "{{img_path}}",
model_name = "{{model_name}}",
enforce_detection = {{enforce_detection}},
detector_backend = "{{detector_backend}}",
align = {{align}},
normalization = "{{normalization}}"
);
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)

print(json.dumps(result, default=str))
27 changes: 16 additions & 11 deletions scripts/verify.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import json;
import sys;
from deepface import DeepFace;

result = DeepFace.verify(
img1_path = "{{img1_path}}",
img2_path = "{{img2_path}}",
enforce_detection = {{enforce_detection}},
align = {{align}},
model_name = "{{model_name}}",
detector_backend = "{{detector_backend}}",
distance_metric = "{{distance_metric}}",
normalization = "{{normalization}}"
);
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}}"
);

print(json.dumps(result, default=str))
print(json.dumps(result, default=str))
except ValueError as e:
print(json.dumps({"error": str(e)}), file=sys.stderr)
4 changes: 2 additions & 2 deletions src/Data/FacialArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function __construct(
public readonly int $y,
public readonly int $w,
public readonly int $h,
public readonly ?int $left_eye,
public readonly ?int $right_eye,
public readonly int|array|null $left_eye,
public readonly int|array|null $right_eye,
) {
}

Expand Down
42 changes: 35 additions & 7 deletions src/DeepFace.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Astrotomic\DeepFace\Enums\Gender;
use Astrotomic\DeepFace\Enums\Normalization;
use Astrotomic\DeepFace\Enums\Race;
use Astrotomic\DeepFace\Exceptions\DeepFaceException;
use BadMethodCallException;
use InvalidArgumentException;
use SplFileInfo;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function verify(
bool $enforce_detection = true,
bool $align = true,
Normalization $normalization = Normalization::BASE,
bool $anti_spoofing = false,
): VerifyResult {
$img1 = new SplFileInfo($img1_path);
$img2 = new SplFileInfo($img2_path);
Expand All @@ -68,9 +70,10 @@ public function verify(
$output = $this->run(
filepath: __DIR__.'/../scripts/verify.py',
data: [
'{{img1_path}}' => $img1->getRealPath(),
'{{img2_path}}' => $img2->getRealPath(),
'{{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,
Expand All @@ -80,7 +83,7 @@ public function verify(
);

return new VerifyResult(
verified: $output['verified'] === 'True',
verified: $output['verified'],
distance: $output['distance'],
threshold: $output['threshold'],
model: FaceRecognitionModel::from($output['model']),
Expand All @@ -104,6 +107,7 @@ public function analyze(
Detector $detector_backend = Detector::OPENCV,
bool $align = true,
bool $silent = false,
bool $anti_spoofing = false,
): array {
$img = new SplFileInfo($img_path);

Expand All @@ -123,9 +127,10 @@ public function analyze(
$output = $this->run(
filepath: __DIR__.'/../scripts/analyze.py',
data: [
'{{img_path}}' => $img->getRealPath(),
'{{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',
Expand Down Expand Up @@ -159,6 +164,7 @@ public function extractFaces(
bool $enforce_detection = true,
bool $align = true,
bool $grayscale = false,
bool $anti_spoofing = false,
): array {
$img = new SplFileInfo($img_path);

Expand All @@ -169,9 +175,10 @@ public function extractFaces(
$output = $this->run(
filepath: __DIR__.'/../scripts/extract_faces.py',
data: [
'{{img_path}}' => $img->getRealPath(),
'{{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',
Expand Down Expand Up @@ -202,6 +209,7 @@ public function find(
bool $align = true,
Normalization $normalization = Normalization::BASE,
bool $silent = false,
bool $anti_spoofing = false,
): array {
$img = new SplFileInfo($img_path);
$db = new SplFileInfo($db_path);
Expand All @@ -217,11 +225,12 @@ public function find(
$output = $this->run(
filepath: __DIR__.'/../scripts/find.py',
data: [
'{{img_path}}' => $img->getRealPath(),
'{{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,
Expand All @@ -239,6 +248,8 @@ public function find(
y: $output['source_y'][$i],
w: $output['source_w'][$i],
h: $output['source_h'][$i],
left_eye: null,
right_eye: null
Gummibeer marked this conversation as resolved.
Show resolved Hide resolved
),
model: $model_name,
detector_backend: $detector_backend,
Expand All @@ -260,6 +271,7 @@ 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);

Expand All @@ -270,9 +282,10 @@ public function represent(
$output = $this->run(
filepath: __DIR__.'/../scripts/represent.py',
data: [
'{{img_path}}' => $img->getRealPath(),
'{{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,
Expand Down Expand Up @@ -300,6 +313,21 @@ protected function run(string $filepath, array $data): array|bool
->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']);
} else {
throw new DeepFaceException("Failed to parse error message: " . $lastJson);
}
}
}

$lines = array_values(array_filter(explode(PHP_EOL, $output), function (string $line): bool {
json_decode($line, true);

Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/DeepFaceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Astrotomic\DeepFace\Exceptions;

use Exception;

class DeepFaceException extends Exception
{
}