1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meema\MediaRecognition\Recognizers; |
4
|
|
|
|
5
|
|
|
use Aws\Rekognition\RekognitionClient; |
6
|
|
|
use Exception; |
7
|
|
|
use Meema\MediaRecognition\Contracts\MediaRecognition as MediaRecognitionInterface; |
8
|
|
|
use Meema\MediaRecognition\Models\MediaRecognition; |
9
|
|
|
use Meema\MediaRecognition\Traits\CanRecognizeImages; |
10
|
|
|
use Meema\MediaRecognition\Traits\CanRecognizeVideos; |
11
|
|
|
use Meema\MediaRecognition\Traits\InteractsWithStorage; |
12
|
|
|
|
13
|
|
|
class Rekognition implements MediaRecognitionInterface |
14
|
|
|
{ |
15
|
|
|
use InteractsWithStorage, CanRecognizeImages, CanRecognizeVideos; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Client instance of MediaRecognition. |
19
|
|
|
* |
20
|
|
|
* @var \Aws\Rekognition\RekognitionClient |
21
|
|
|
*/ |
22
|
|
|
protected RekognitionClient $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The settings provided to the Rekognition job. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected array $settings; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The relating media model's id. |
33
|
|
|
* |
34
|
|
|
* @var int|null |
35
|
|
|
*/ |
36
|
|
|
protected ?int $mediaId = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Construct converter. |
40
|
|
|
* |
41
|
|
|
* @param \Aws\Rekognition\RekognitionClient $client |
42
|
|
|
*/ |
43
|
|
|
public function __construct(RekognitionClient $client) |
44
|
|
|
{ |
45
|
|
|
$this->client = $client; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the MediaRecognition Client. |
50
|
|
|
* |
51
|
|
|
* @return \Aws\Rekognition\RekognitionClient |
52
|
|
|
*/ |
53
|
|
|
public function getClient(): RekognitionClient |
54
|
|
|
{ |
55
|
|
|
return $this->client; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the labels from a video analysis whenever. |
60
|
|
|
* |
61
|
|
|
* @param string $jobId |
62
|
|
|
* @param int $mediaId |
63
|
|
|
* @return \Aws\Result |
64
|
|
|
* @throws \Exception |
65
|
|
|
*/ |
66
|
|
|
public function getLabelsByJobId(string $jobId, int $mediaId) |
67
|
|
|
{ |
68
|
|
|
$results = $this->client->getLabelDetection([ |
69
|
|
|
'JobId' => $jobId, |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->updateVideoLabels($results->toArray(), $mediaId); |
73
|
|
|
|
74
|
|
|
return $results; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $type |
79
|
|
|
* @param $mediaId |
80
|
|
|
* @param $results |
81
|
|
|
* @return mixed |
82
|
|
|
* @throws \Exception |
83
|
|
|
*/ |
84
|
|
|
protected function updateOrCreate($type, $mediaId, $results) |
85
|
|
|
{ |
86
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
87
|
|
|
return $results; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (is_null($mediaId)) { |
91
|
|
|
throw new Exception('Please make sure to set a $mediaId.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
MediaRecognition::updateOrCreate([ |
95
|
|
|
'model_id' => $mediaId, |
96
|
|
|
'model_type' => config('media-converter.media_model'), |
97
|
|
|
], [$type => $results->toArray()]); |
98
|
|
|
|
99
|
|
|
return $results; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $jobId |
104
|
|
|
* @param string $type |
105
|
|
|
* @return void |
106
|
|
|
* @throws \Exception |
107
|
|
|
*/ |
108
|
|
|
protected function updateJobId(string $jobId, string $type) |
109
|
|
|
{ |
110
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (is_null($this->mediaId)) { |
115
|
|
|
throw new Exception('Please make sure to set a $mediaId.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
MediaRecognition::updateOrCreate([ |
119
|
|
|
'model_id' => $this->mediaId, |
120
|
|
|
'model_type' => config('media-converter.media_model'), |
121
|
|
|
], [$type.'_job_id' => $jobId]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $results |
126
|
|
|
* @param int $mediaId |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
protected function updateVideoLabels(array $results, int $mediaId) |
130
|
|
|
{ |
131
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$mediaRecognition = MediaRecognition::where('model_id', $mediaId)->firstOrFail(); |
136
|
|
|
$mediaRecognition->labels = $results; |
137
|
|
|
$mediaRecognition->save(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|