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
|
|
|
* @throws \Exception |
43
|
|
|
*/ |
44
|
|
|
public function __construct(RekognitionClient $client) |
45
|
|
|
{ |
46
|
|
|
$this->client = $client; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the MediaRecognition Client. |
51
|
|
|
* |
52
|
|
|
* @return \Aws\Rekognition\RekognitionClient |
53
|
|
|
*/ |
54
|
|
|
public function getClient(): RekognitionClient |
55
|
|
|
{ |
56
|
|
|
return $this->client; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Detects labels/objects in an image. |
61
|
|
|
* |
62
|
|
|
* @param int|null $mediaId |
63
|
|
|
* @param int|null $minConfidence |
64
|
|
|
* @param int|null $maxLabels |
65
|
|
|
* @return \Aws\Result |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
public function detectLabels($mediaId = null, $minConfidence = null, $maxLabels = null) |
69
|
|
|
{ |
70
|
|
|
$this->setImageSettings(); |
71
|
|
|
|
72
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
73
|
|
|
|
74
|
|
|
if (is_int($maxLabels)) { |
75
|
|
|
$this->settings['MaxLabels'] = $maxLabels; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$results = $this->client->detectLabels($this->settings); |
79
|
|
|
|
80
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
81
|
|
|
return $results; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (is_null($mediaId)) { |
85
|
|
|
throw new Exception('Please make sure to set a $mediaId.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
MediaRecognition::updateOrCreate([ |
89
|
|
|
'model_id' => $mediaId, |
90
|
|
|
'model_type' => config('media-converter.media_model'), |
91
|
|
|
], ['labels' => $results->toArray()]); |
92
|
|
|
|
93
|
|
|
return $results; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Detects faces in an image & analyzes them. |
98
|
|
|
* |
99
|
|
|
* @param int|null $mediaId |
100
|
|
|
* @param array $attributes |
101
|
|
|
* @return \Aws\Result |
102
|
|
|
* @throws \Exception |
103
|
|
|
*/ |
104
|
|
|
public function detectFaces($mediaId = null, $attributes = ['DEFAULT']) |
105
|
|
|
{ |
106
|
|
|
$this->setImageSettings(); |
107
|
|
|
|
108
|
|
|
$this->settings['Attributes'] = $attributes; |
109
|
|
|
|
110
|
|
|
$results = $this->client->detectFaces($this->settings); |
111
|
|
|
|
112
|
|
|
$this->updateOrCreate('faces', $mediaId, $results); |
113
|
|
|
|
114
|
|
|
return $results; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Detects moderation labels in an image. |
119
|
|
|
* This can be useful for children-friendly images or NSFW images. |
120
|
|
|
* |
121
|
|
|
* @param int|null $mediaId |
122
|
|
|
* @param int|null $minConfidence |
123
|
|
|
* @return \Aws\Result |
124
|
|
|
* @throws \Exception |
125
|
|
|
*/ |
126
|
|
|
public function detectModeration($mediaId = null, $minConfidence = null) |
127
|
|
|
{ |
128
|
|
|
$this->setImageSettings(); |
129
|
|
|
|
130
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
131
|
|
|
|
132
|
|
|
$results = $this->client->detectModerationLabels($this->settings); |
133
|
|
|
|
134
|
|
|
$this->updateOrCreate('moderation', $mediaId, $results); |
135
|
|
|
|
136
|
|
|
return $results; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Detects text in an image (OCR). |
141
|
|
|
* |
142
|
|
|
* @param int|null $mediaId |
143
|
|
|
* @return \Aws\Result |
144
|
|
|
* @throws \Exception |
145
|
|
|
*/ |
146
|
|
|
public function detectText($mediaId = null) |
147
|
|
|
{ |
148
|
|
|
$this->setImageSettings(); |
149
|
|
|
|
150
|
|
|
$results = $this->client->detectText($this->settings); |
151
|
|
|
|
152
|
|
|
$this->updateOrCreate('ocr', $mediaId, $results); |
153
|
|
|
|
154
|
|
|
return $results; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $type |
159
|
|
|
* @param $mediaId |
160
|
|
|
* @param $results |
161
|
|
|
* @return mixed |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
|
|
protected function updateOrCreate($type, $mediaId, $results) |
165
|
|
|
{ |
166
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
167
|
|
|
return $results; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (is_null($mediaId)) { |
171
|
|
|
throw new Exception('Please make sure to set a $mediaId.'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
MediaRecognition::updateOrCreate([ |
175
|
|
|
'model_id' => $mediaId, |
176
|
|
|
'model_type' => config('media-converter.media_model'), |
177
|
|
|
], [$type => $results->toArray()]); |
178
|
|
|
|
179
|
|
|
return $results; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $jobId |
184
|
|
|
* @param string $type |
185
|
|
|
* @return void |
186
|
|
|
* @throws \Exception |
187
|
|
|
*/ |
188
|
|
|
protected function updateJobId(string $jobId, string $type) |
189
|
|
|
{ |
190
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
191
|
|
|
return; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (is_null($this->mediaId)) { |
195
|
|
|
throw new Exception('Please make sure to set a $mediaId.'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
MediaRecognition::updateOrCreate([ |
199
|
|
|
'model_id' => $this->mediaId, |
200
|
|
|
'model_type' => config('media-converter.media_model'), |
201
|
|
|
], [$type.'_job_id' => $jobId]); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array $results |
206
|
|
|
* @param string $type |
207
|
|
|
* @param int $mediaId |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
protected function updateVideoResults(array $results, string $type, int $mediaId) |
211
|
|
|
{ |
212
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
213
|
|
|
return; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$mediaRecognition = MediaRecognition::where('model_id', $mediaId)->firstOrFail(); |
217
|
|
|
$mediaRecognition->$type = $results; |
218
|
|
|
$mediaRecognition->save(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|