1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meema\MediaRecognition\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
trait CanRecognizeVideos |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Sets the video to be analyzed. |
11
|
|
|
* |
12
|
|
|
* @param $type - used to create a readable identifier. |
|
|
|
|
13
|
|
|
* @return void |
14
|
|
|
* @throws \Exception |
15
|
|
|
*/ |
16
|
|
|
protected function setVideoSettings($type): void |
17
|
|
|
{ |
18
|
|
|
$disk = $this->disk ?? config('media-recognition.disk'); |
19
|
|
|
$bucketName = config("filesystems.disks.$disk.bucket"); |
20
|
|
|
|
21
|
|
|
if (! $bucketName) { |
22
|
|
|
throw new Exception('Please make sure to set a S3 bucket name.'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$this->settings['Video'] = [ |
|
|
|
|
26
|
|
|
'S3Object' => [ |
27
|
|
|
'Bucket' => $bucketName, |
28
|
|
|
'Name' => $this->path, |
29
|
|
|
], |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
$this->settings['NotificationChannel'] = [ |
33
|
|
|
'RoleArn' => config('media-recognition.iam_arn'), |
34
|
|
|
'SNSTopicArn' => config('media-recognition.sns_topic_arn'), |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
$uniqueId = $type.'_'.$this->mediaId; |
38
|
|
|
// Idempotent token used to identify the start request. |
39
|
|
|
// If you use the same token with multiple StartCelebrityRecognition requests, the same JobId is returned. |
40
|
|
|
// Use ClientRequestToken to prevent the same job from being accidentally started more than once. |
41
|
|
|
$this->settings['ClientRequestToken'] = $uniqueId; |
42
|
|
|
|
43
|
|
|
// the JobTag is set to be the media id, so we can adjust the media record with the results once the webhook comes in |
44
|
|
|
$this->settings['JobTag'] = $uniqueId; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Starts asynchronous detection of labels/objects in a stored video. |
49
|
|
|
* |
50
|
|
|
* @param int $mediaId |
51
|
|
|
* @param int|null $minConfidence |
52
|
|
|
* @param int $maxResults |
53
|
|
|
* @return \Aws\Result |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
public function startLabelDetection(int $mediaId, $minConfidence = null, $maxResults = 1000) |
57
|
|
|
{ |
58
|
|
|
$this->mediaId = $mediaId; |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->setVideoSettings('labels'); |
61
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
62
|
|
|
$this->settings['MaxResults'] = $maxResults; |
63
|
|
|
|
64
|
|
|
$results = $this->client->startLabelDetection($this->settings); |
65
|
|
|
|
66
|
|
|
if ($results['JobId']) { |
67
|
|
|
$this->updateJobId($results['JobId'], 'labels'); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $results; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Starts the detection of faces in a video. |
75
|
|
|
* |
76
|
|
|
* @param null $mediaId |
|
|
|
|
77
|
|
|
* @param string $faceAttribute |
78
|
|
|
* @return \Aws\Result |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
|
|
public function startFaceDetection($mediaId = null, string $faceAttribute = 'DEFAULT') |
82
|
|
|
{ |
83
|
|
|
$this->mediaId = $mediaId; |
|
|
|
|
84
|
|
|
|
85
|
|
|
$this->setVideoSettings('faces'); |
86
|
|
|
$this->settings['FaceAttributes'] = $faceAttribute; |
|
|
|
|
87
|
|
|
|
88
|
|
|
$results = $this->client->startFaceDetection($this->settings); |
89
|
|
|
|
90
|
|
|
if ($results['JobId']) { |
91
|
|
|
$this->updateJobId($results['JobId'], 'faces'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $results; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Starts asynchronous detection of unsafe content in a stored video. |
99
|
|
|
* |
100
|
|
|
* @param null $mediaId |
|
|
|
|
101
|
|
|
* @param int|null $minConfidence |
102
|
|
|
* @return \Aws\Result |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
|
|
public function startContentModeration($mediaId = null, $minConfidence = null) |
106
|
|
|
{ |
107
|
|
|
$this->mediaId = $mediaId; |
|
|
|
|
108
|
|
|
|
109
|
|
|
$this->setVideoSettings('moderation'); |
110
|
|
|
|
111
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$results = $this->client->startContentModeration($this->settings); |
114
|
|
|
|
115
|
|
|
if ($results['JobId']) { |
116
|
|
|
$this->updateJobId($results['JobId'], 'faces'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $results; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Starts asynchronous detection of text in a stored video. |
124
|
|
|
* |
125
|
|
|
* @param null $mediaId |
|
|
|
|
126
|
|
|
* @param array|null $filters |
127
|
|
|
* @return \Aws\Result |
128
|
|
|
* @throws \Exception |
129
|
|
|
*/ |
130
|
|
|
public function startTextDetection($mediaId = null, array $filters = null) |
131
|
|
|
{ |
132
|
|
|
$this->mediaId = $mediaId; |
|
|
|
|
133
|
|
|
|
134
|
|
|
$this->setVideoSettings('ocr'); |
135
|
|
|
|
136
|
|
|
if (is_array($filters)) { |
137
|
|
|
$this->settings['Filters'] = $filters; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$results = $this->client->startTextDetection($this->settings); |
141
|
|
|
|
142
|
|
|
if ($results['JobId']) { |
143
|
|
|
$this->updateJobId($results['JobId'], 'ocr'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $results; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the labels from the video analysis. |
151
|
|
|
* |
152
|
|
|
* @param string $jobId |
153
|
|
|
* @param int $mediaId |
154
|
|
|
* @return \Aws\Result |
155
|
|
|
* @throws \Exception |
156
|
|
|
*/ |
157
|
|
|
public function getLabelsByJobId(string $jobId, int $mediaId) |
158
|
|
|
{ |
159
|
|
|
$results = $this->client->getLabelDetection([ |
160
|
|
|
'JobId' => $jobId, |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
$this->updateVideoResults($results->toArray(),'labels', $mediaId); |
|
|
|
|
164
|
|
|
|
165
|
|
|
return $results; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the faces from the video analysis. |
170
|
|
|
* |
171
|
|
|
* @param string $jobId |
172
|
|
|
* @param int $mediaId |
173
|
|
|
* @return \Aws\Result |
174
|
|
|
* @throws \Exception |
175
|
|
|
*/ |
176
|
|
|
public function getFacesByJobId(string $jobId, int $mediaId) |
177
|
|
|
{ |
178
|
|
|
$results = $this->client->getFaceDetection([ |
179
|
|
|
'JobId' => $jobId, |
180
|
|
|
]); |
181
|
|
|
|
182
|
|
|
$this->updateVideoResults($results->toArray(),'faces', $mediaId); |
183
|
|
|
|
184
|
|
|
return $results; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the "content moderation" from the video analysis. |
189
|
|
|
* |
190
|
|
|
* @param string $jobId |
191
|
|
|
* @param int $mediaId |
192
|
|
|
* @return \Aws\Result |
193
|
|
|
* @throws \Exception |
194
|
|
|
*/ |
195
|
|
|
public function getContentModerationByJobId(string $jobId, int $mediaId) |
196
|
|
|
{ |
197
|
|
|
$results = $this->client->getContentModeration([ |
198
|
|
|
'JobId' => $jobId, |
199
|
|
|
]); |
200
|
|
|
|
201
|
|
|
$this->updateVideoResults($results->toArray(),'moderation', $mediaId); |
202
|
|
|
|
203
|
|
|
return $results; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the faces from a video analysis. |
208
|
|
|
* |
209
|
|
|
* @param string $jobId |
210
|
|
|
* @param int $mediaId |
211
|
|
|
* @return \Aws\Result |
212
|
|
|
* @throws \Exception |
213
|
|
|
*/ |
214
|
|
|
public function getTextDetectionByJobId(string $jobId, int $mediaId) |
215
|
|
|
{ |
216
|
|
|
$results = $this->client->getTextDetection([ |
217
|
|
|
'JobId' => $jobId, |
218
|
|
|
]); |
219
|
|
|
|
220
|
|
|
$this->updateVideoResults($results->toArray(),'ocr', $mediaId); |
221
|
|
|
|
222
|
|
|
return $results; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|