Skip to content

Commit

Permalink
fix: video & image response structure is different for faces & face d…
Browse files Browse the repository at this point in the history
…etails
  • Loading branch information
chrisbbreuer committed Mar 26, 2021
1 parent def15a8 commit da00441
Showing 1 changed file with 92 additions and 27 deletions.
119 changes: 92 additions & 27 deletions src/Traits/Recognizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ trait Recognizable
{
/**
* Get all of the media items' conversions.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function recognition()
public function recognition(): \Illuminate\Database\Eloquent\Relations\MorphOne
{
return $this->morphOne(MediaRecognition::class, 'model');
}
Expand All @@ -30,10 +32,11 @@ public function recognize(string $path, string $mimeType = null)
/**
* Return all recognition data.
* The return value "null" indicates that a recognition has been ran, but it just has no results.
* Please note, the Rekognition response is different from a "video" to an "image".
*
* @return array
*/
public function recognitionData()
public function recognitionData(): array
{
$recognition = $this->recognition()->first();

Expand All @@ -55,6 +58,8 @@ public function recognitionData()

if ($recognition->faces && is_array($recognition->faces['FaceDetails'])) {
$faces = $recognition->faces['FaceDetails'];
} elseif ($recognition->faces && is_array($recognition->faces['Faces'])) {
$faces = $recognition->faces['Faces'];
}

if ($recognition->moderation && is_array($recognition->moderation['ModerationLabels'])) {
Expand Down Expand Up @@ -95,44 +100,104 @@ public function minimalRecognitionData(

if ($includeLabels) {
$array['labels'] = collect($data['labels'])->map(function ($label) {
return [
'name' => $label['Label']['Name'],
'confidence' => $label['Label']['Confidence'],
'timestamp' => $label['Timestamp'],
];
})->unique('name')->sortByDesc('confidence')->take($limit)->values();
return $this->generateLabelElement($label);
})->unique('name')->sortByDesc('confidence')->take($limit)->values()->toArray();
}

if ($includeFaces) {
$array['faces'] = collect($data['faces'])->map(function ($face) {
return [
'bounding_box' => $face['Face']['BoundingBox'],
'confidence' => $face['Face']['Confidence'],
'timestamp' => $face['Timestamp'],
];
})->sortByDesc('confidence')->take($limit)->values();
return $this->generateFaceElement($face);
})->sortByDesc('confidence')->take($limit)->values()->toArray();
}

if ($includeModeration) {
$array['moderation'] = collect($data['moderation'])->map(function ($label) {
return [
'name' => $label['ModerationLabel']['Name'],
'confidence' => $label['ModerationLabel']['Confidence'],
'timestamp' => $label['Timestamp'],
];
})->unique('name')->sortByDesc('confidence')->take($limit)->values();
$array['moderation'] = collect($data['moderation'])->map(function ($moderation) {
return $this->generateModerationElement($moderation);
})->unique('name')->sortByDesc('confidence')->take($limit)->values()->toArray();
}

if ($includeTexts) {
$array['texts'] = collect($data['texts'])->map(function ($text) {
return [
'text' => $text['TextDetection']['DetectedText'],
'confidence' => $text['TextDetection']['Confidence'],
'timestamp' => $text['Timestamp'],
];
})->unique('text')->sortByDesc('confidence')->take($limit)->values();
return $this->generateTextElement($text);
})->unique('text')->sortByDesc('confidence')->take($limit)->values()->toArray();
}

return $array;
}

public function generateLabelElement($label): array
{
// image element
if ($label['Name']) {
return [
'name' => $label['Name'],
'confidence' => $label['Confidence'],
'timestamp' => null, // timestamps are only available in videos
];
}

// video element
return [
'name' => $label['Label']['Name'],
'confidence' => $label['Label']['Confidence'],
'timestamp' => $label['Timestamp'],
];
}

public function generateFaceElement($face): array
{
// image element
if ($face['BoundingBox']) {
return [
'bounding_box' => $face['BoundingBox'],
'confidence' => $face['Confidence'],
'timestamp' => null, // timestamps are only available in videos
];
}

// video element
return [
'bounding_box' => $face['Face']['BoundingBox'],
'confidence' => $face['Face']['Confidence'],
'timestamp' => $face['Timestamp'],
];
}

public function generateModerationElement($moderation): array
{
// image element
if ($moderation['Name']) {
return [
'name' => $moderation['Name'],
'confidence' => $moderation['Confidence'],
'timestamp' => null, // timestamps are only available in videos
];
}

// video element
return [
'name' => $moderation['ModerationLabel']['Name'],
'confidence' => $moderation['ModerationLabel']['Confidence'],
'timestamp' => $moderation['Timestamp'],
];
}

public function generateTextElement($text): array
{
// image element
if ($text['DetectedText']) {
return [
'text' => $text['DetectedText'],
'confidence' => $text['Confidence'],
'timestamp' => null, // timestamps are only available in videos
];
}

// video element
return [
'text' => $text['TextDetection']['DetectedText'],
'confidence' => $text['TextDetection']['Confidence'],
'timestamp' => $text['Timestamp'],
];
}
}

0 comments on commit da00441

Please sign in to comment.