Skip to content

Commit

Permalink
Merge pull request #7 from Astrotomic/deepface-represent
Browse files Browse the repository at this point in the history
add deepface represent logic
  • Loading branch information
Gummibeer authored Oct 10, 2023
2 parents 3343277 + 7ed1ecc commit fc58bf9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ $deepface->find(

### Face Embeddings

Generate vector embeddings for facial images using convolutional neural networks models (To Do).
Generate vector embeddings for facial images using convolutional neural networks models:

```php
$deepface->represent(
img_path: '~/test.png',
);
```

### Facial Attribute Analysis

Expand Down
13 changes: 13 additions & 0 deletions scripts/represent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import json;
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}}"
);

print(json.dumps(result, default=str))
18 changes: 18 additions & 0 deletions src/Data/RepresentResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Astrotomic\DeepFace\Data;

use Astrotomic\DeepFace\Enums\Detector;
use Astrotomic\DeepFace\Enums\FaceRecognitionModel;

class RepresentResult
{
public function __construct(
public readonly string $img_path,
public readonly array $embedding,
public readonly FacialArea $facial_area,
public readonly FaceRecognitionModel $model,
public readonly Detector $detector_backend,
) {
}
}
42 changes: 42 additions & 0 deletions src/DeepFace.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Astrotomic\DeepFace\Data\ExtractFaceResult;
use Astrotomic\DeepFace\Data\FacialArea;
use Astrotomic\DeepFace\Data\FindResult;
use Astrotomic\DeepFace\Data\RepresentResult;
use Astrotomic\DeepFace\Data\VerifyResult;
use Astrotomic\DeepFace\Enums\AnalyzeAction;
use Astrotomic\DeepFace\Enums\Detector;
Expand Down Expand Up @@ -245,6 +246,47 @@ public function find(
return $result;
}

/**
* @return RepresentResult[]
*/
public function represent(
string $img_path,
FaceRecognitionModel $model_name = FaceRecognitionModel::VGGFACE,
bool $enforce_detection = true,
Detector $detector_backend = Detector::OPENCV,
bool $align = true,
Normalization $normalization = Normalization::BASE,
): 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}}' => $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,
],
);

return array_map(
fn (array $result) => new RepresentResult(
img_path: $img->getRealPath(),
embedding: $result['embedding'],
facial_area: new FacialArea(...$result['facial_area']),
model: $model_name,
detector_backend: $detector_backend,
),
$output
);
}

protected function run(string $filepath, array $data): array|bool
{
$script = $this->script($filepath, $data);
Expand Down

0 comments on commit fc58bf9

Please sign in to comment.