Skip to content

Commit

Permalink
add missing values to data objects
Browse files Browse the repository at this point in the history
make all data objects json serializable
  • Loading branch information
Gummibeer committed Oct 10, 2023
1 parent 7fbe738 commit 0efc9bb
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
24 changes: 22 additions & 2 deletions src/Data/AnalyzeResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

namespace Astrotomic\DeepFace\Data;

use Astrotomic\DeepFace\Enums\Detector;
use Astrotomic\DeepFace\Enums\Emotion;
use Astrotomic\DeepFace\Enums\Gender;
use Astrotomic\DeepFace\Enums\Race;
use JsonSerializable;

class AnalyzeResult
class AnalyzeResult implements JsonSerializable
{
/**
* @param array<Emotion, float>|null $emotion
* @param array<Gender, float>|null $gender
* @param array<Race, float>|null $race
*/
public function __construct(
public readonly FacialArea $region,
public readonly string $img_path,
public readonly Detector $detector_backend,
public readonly FacialArea $facial_area,
public readonly ?array $emotion,
public readonly ?Emotion $dominant_emotion,
public readonly ?int $age,
Expand All @@ -24,4 +28,20 @@ public function __construct(
public readonly ?Race $dominant_race,
) {
}

public function jsonSerialize(): array
{
return [
'img_path' => $this->img_path,
'detector_backend' => $this->detector_backend->value,
'region' => $this->facial_area->jsonSerialize(),
'emotion' => $this->emotion,
'dominant_emotion' => $this->dominant_emotion?->value,
'age' => $this->age,
'gender' => $this->gender,
'dominant_gender' => $this->dominant_gender?->value,
'race' => $this->race,
'dominant_race' => $this->dominant_race?->value,
];
}
}
17 changes: 16 additions & 1 deletion src/Data/ExtractFaceResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@

namespace Astrotomic\DeepFace\Data;

class ExtractFaceResult
use Astrotomic\DeepFace\Enums\Detector;
use JsonSerializable;

class ExtractFaceResult implements JsonSerializable
{
public function __construct(
public readonly string $img_path,
public readonly FacialArea $facial_area,
public readonly float $confidence,
public readonly Detector $detector_backend,
) {
}

public function jsonSerialize(): array
{
return [
'img_path' => $this->img_path,
'facial_area' => $this->facial_area->jsonSerialize(),
'confidence' => $this->confidence,
'detector_backend' => $this->detector_backend->value,
];
}
}
14 changes: 13 additions & 1 deletion src/Data/FacialArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Astrotomic\DeepFace\Data;

class FacialArea
use JsonSerializable;

class FacialArea implements JsonSerializable
{
public function __construct(
public readonly int $x,
Expand All @@ -11,4 +13,14 @@ public function __construct(
public readonly int $h,
) {
}

public function jsonSerialize(): array
{
return [
'x' => $this->x,
'y' => $this->y,
'w' => $this->w,
'h' => $this->h,
];
}
}
16 changes: 15 additions & 1 deletion src/Data/FindResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Astrotomic\DeepFace\Enums\Detector;
use Astrotomic\DeepFace\Enums\DistanceMetric;
use Astrotomic\DeepFace\Enums\FaceRecognitionModel;
use JsonSerializable;

class FindResult
class FindResult implements JsonSerializable
{
public function __construct(
public readonly string $identity_img_path,
Expand All @@ -18,4 +19,17 @@ public function __construct(
public readonly float $distance,
) {
}

public function jsonSerialize(): array
{
return [
'identity_img_path' => $this->identity_img_path,
'source_img_path' => $this->source_img_path,
'source_facial_area' => $this->source_facial_area->jsonSerialize(),
'model' => $this->model->value,
'detector_backend' => $this->detector_backend->value,
'distance_metric' => $this->distance_metric->value,
'distance' => $this->distance,
];
}
}
14 changes: 13 additions & 1 deletion src/Data/RepresentResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

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

class RepresentResult
class RepresentResult implements JsonSerializable
{
public function __construct(
public readonly string $img_path,
Expand All @@ -15,4 +16,15 @@ public function __construct(
public readonly Detector $detector_backend,
) {
}

public function jsonSerialize(): array
{
return [
'img_path' => $this->img_path,
'embedding' => $this->embedding,
'facial_area' => $this->facial_area->jsonSerialize(),
'model' => $this->model->value,
'detector_backend' => $this->detector_backend->value,
];
}
}
19 changes: 18 additions & 1 deletion src/Data/VerifyResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Astrotomic\DeepFace\Enums\Detector;
use Astrotomic\DeepFace\Enums\DistanceMetric;
use Astrotomic\DeepFace\Enums\FaceRecognitionModel;
use JsonSerializable;

class VerifyResult
class VerifyResult implements JsonSerializable
{
public function __construct(
public readonly bool $verified,
Expand All @@ -22,4 +23,20 @@ public function __construct(
public readonly float $time,
) {
}

public function jsonSerialize(): array
{
return [
'verified' => $this->verified,
'distance' => $this->distance,
'model' => $this->model->value,
'detector_backend' => $this->detector_backend->value,
'distance_metric' => $this->distance_metric->value,
'img1_path' => $this->img1_path,
'img1_facial_area' => $this->img1_facial_area->jsonSerialize(),
'img2_path' => $this->img2_path,
'img2_facial_area' => $this->img2_facial_area->jsonSerialize(),
'time' => $this->time,
];
}
}
8 changes: 6 additions & 2 deletions src/DeepFace.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public function analyze(

return array_map(
fn (array $result) => new AnalyzeResult(
region: new FacialArea(...$result['region']),
img_path: $img->getRealPath(),
detector_backend: $detector_backend,
facial_area: new FacialArea(...$result['region']),
emotion: $result['emotion'] ?? null,
dominant_emotion: isset($result['dominant_emotion']) ? Emotion::from($result['dominant_emotion']) : null,
age: $result['age'] ?? null,
Expand Down Expand Up @@ -178,8 +180,10 @@ public function extractFaces(

return array_map(
fn (array $result) => new ExtractFaceResult(
img_path: $img->getRealPath(),
facial_area: new FacialArea(...$result['facial_area']),
confidence: $result['confidence']
confidence: $result['confidence'],
detector_backend: $detector_backend,
),
$output
);
Expand Down

0 comments on commit 0efc9bb

Please sign in to comment.