diff --git a/src/Traits/Recognizable.php b/src/Traits/Recognizable.php index 42c22be..3516c15 100644 --- a/src/Traits/Recognizable.php +++ b/src/Traits/Recognizable.php @@ -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'); } @@ -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(); @@ -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'])) { @@ -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'], + ]; + } }