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\InteractsWithStorage; |
10
|
|
|
|
11
|
|
|
class Rekognition implements MediaRecognitionInterface |
12
|
|
|
{ |
13
|
|
|
use InteractsWithStorage; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Client instance of MediaRecognition. |
17
|
|
|
* |
18
|
|
|
* @var \Aws\Rekognition\RekognitionClient |
19
|
|
|
*/ |
20
|
|
|
protected RekognitionClient $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The settings provided to the Rekognition job. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected array $settings; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The input image as base64-encoded bytes. |
31
|
|
|
* |
32
|
|
|
* @var string|null |
33
|
|
|
*/ |
34
|
|
|
protected ?string $blob = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Construct converter. |
38
|
|
|
* |
39
|
|
|
* @param \Aws\Rekognition\RekognitionClient $client |
40
|
|
|
*/ |
41
|
|
|
public function __construct(RekognitionClient $client) |
42
|
|
|
{ |
43
|
|
|
$this->client = $client; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the MediaRecognition Client. |
48
|
|
|
* |
49
|
|
|
* @return \Aws\Rekognition\RekognitionClient |
50
|
|
|
*/ |
51
|
|
|
public function getClient(): RekognitionClient |
52
|
|
|
{ |
53
|
|
|
return $this->client; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the base64 encoded image. |
58
|
|
|
* |
59
|
|
|
* @param string $blob |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function blob(string $blob) |
64
|
|
|
{ |
65
|
|
|
$this->blob = $blob; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sets the image to be analyzed. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
|
|
protected function setImage(): array |
77
|
|
|
{ |
78
|
|
|
if (is_string($this->blob)) { |
79
|
|
|
$settings['Image'] = [ |
|
|
|
|
80
|
|
|
'Bytes' => $this->blob, |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
return $settings; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$disk = $this->disk ?? config('media-recognition.disk'); |
87
|
|
|
$bucketName = config("filesystems.disks.$disk.bucket"); |
88
|
|
|
|
89
|
|
|
if (! $bucketName) { |
90
|
|
|
throw new Exception('Please make sure to set a S3 bucket name.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$settings['Image'] = [ |
94
|
|
|
'S3Object' => [ |
95
|
|
|
'Bucket' => $bucketName, |
96
|
|
|
'Name' => $this->path, |
97
|
|
|
], |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
return $settings; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Detects labels/objects in an image. |
105
|
|
|
* |
106
|
|
|
* @param int|null $mediaId |
107
|
|
|
* @param int|null $minConfidence |
108
|
|
|
* @param int|null $maxLabels |
109
|
|
|
* @return \Aws\Result |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
|
|
public function detectLabels($mediaId = null, $minConfidence = null, $maxLabels = null) |
113
|
|
|
{ |
114
|
|
|
$settings = $this->setImage(); |
115
|
|
|
|
116
|
|
|
$settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
117
|
|
|
|
118
|
|
|
if (is_int($maxLabels)) { |
119
|
|
|
$settings['MaxLabels'] = $maxLabels; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$results = $this->client->detectLabels($settings); |
123
|
|
|
|
124
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
125
|
|
|
return $results; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (is_null($mediaId)) { |
129
|
|
|
throw new Exception('Please make sure to set a $mediaId.'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
MediaRecognition::updateOrCreate([ |
133
|
|
|
'model_id' => $mediaId, |
134
|
|
|
'model_type' => config('media-converter.media_model'), |
135
|
|
|
], ['labels' => $results->toArray()]); |
136
|
|
|
|
137
|
|
|
return $results; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Detects faces & analyzes them. |
142
|
|
|
* |
143
|
|
|
* @param int|null $mediaId |
144
|
|
|
* @param array $attributes |
145
|
|
|
* @return \Aws\Result |
146
|
|
|
* @throws \Exception |
147
|
|
|
*/ |
148
|
|
|
public function detectFaces($mediaId = null, $attributes = ['DEFAULT']) |
149
|
|
|
{ |
150
|
|
|
$settings = $this->setImage(); |
151
|
|
|
|
152
|
|
|
$settings['Attributes'] = $attributes; |
153
|
|
|
|
154
|
|
|
$results = $this->client->detectFaces($settings); |
155
|
|
|
|
156
|
|
|
$this->updateOrCreate('faces', $mediaId, $results); |
157
|
|
|
|
158
|
|
|
return $results; |
159
|
|
|
} |
160
|
|
|
/** |
161
|
|
|
* Detects faces & analyzes them. |
162
|
|
|
* |
163
|
|
|
* @param int|null $mediaId |
164
|
|
|
* @param int|null $minConfidence |
165
|
|
|
* @return \Aws\Result |
166
|
|
|
* @throws \Exception |
167
|
|
|
*/ |
168
|
|
|
public function detectModeration($mediaId = null, $minConfidence = null) |
169
|
|
|
{ |
170
|
|
|
$settings = $this->setImage(); |
171
|
|
|
|
172
|
|
|
$settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
173
|
|
|
|
174
|
|
|
$results = $this->client->detectModerationLabels($settings); |
175
|
|
|
|
176
|
|
|
$this->updateOrCreate('moderation', $mediaId, $results); |
177
|
|
|
|
178
|
|
|
return $results; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Detects text in an image (OCR). |
183
|
|
|
* |
184
|
|
|
* @param int|null $mediaId |
185
|
|
|
* @param int|null $minConfidence |
186
|
|
|
* @return \Aws\Result |
187
|
|
|
* @throws \Exception |
188
|
|
|
*/ |
189
|
|
|
public function detectText($mediaId = null, $minConfidence = null) |
190
|
|
|
{ |
191
|
|
|
$settings = $this->setImage(); |
192
|
|
|
|
193
|
|
|
$results = $this->client->detectText($settings); |
194
|
|
|
|
195
|
|
|
$this->updateOrCreate('ocr', $mediaId, $results); |
196
|
|
|
|
197
|
|
|
return $results; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param $type |
202
|
|
|
* @param $mediaId |
203
|
|
|
* @param $results |
204
|
|
|
* @return mixed |
205
|
|
|
* @throws \Exception |
206
|
|
|
*/ |
207
|
|
|
protected function updateOrCreate($type, $mediaId, $results) |
208
|
|
|
{ |
209
|
|
|
if (! config('media-recognition.track_media_recognitions')) { |
210
|
|
|
return $results; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (is_null($mediaId)) { |
214
|
|
|
throw new Exception('Please make sure to set a $mediaId.'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
MediaRecognition::updateOrCreate([ |
218
|
|
|
'model_id' => $mediaId, |
219
|
|
|
'model_type' => config('media-converter.media_model'), |
220
|
|
|
], [$type => $results->toArray()]); |
221
|
|
|
|
222
|
|
|
return $results; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|