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->source, |
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|null $mediaId |
51
|
|
|
* @param int|null $minConfidence |
52
|
|
|
* @param int $maxResults |
53
|
|
|
* @return \Aws\Result |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
public function detectVideoLabels($mediaId = null, $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
|
|
|
* @param int|null $mediaId |
75
|
|
|
* @param array $attributes |
76
|
|
|
* @return mixed |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
|
public function detectVideoFaces($mediaId = null, $attributes = ['DEFAULT']) |
80
|
|
|
{ |
81
|
|
|
$this->mediaId = $mediaId; |
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->setVideoSettings('faces'); |
84
|
|
|
|
85
|
|
|
$this->settings['FaceAttributes'] = $attributes; |
|
|
|
|
86
|
|
|
|
87
|
|
|
$results = $this->client->startFaceDetection($this->settings); |
88
|
|
|
|
89
|
|
|
if ($results['JobId']) { |
90
|
|
|
$this->updateJobId($results['JobId'], 'faces'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $results; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Starts asynchronous detection of unsafe content in a stored video. |
98
|
|
|
* |
99
|
|
|
* @param int|null $mediaId |
100
|
|
|
* @param int|null $minConfidence |
101
|
|
|
* @return mixed |
102
|
|
|
* @throws \Exception |
103
|
|
|
*/ |
104
|
|
|
public function detectVideoModeration($mediaId = null, $minConfidence = null) |
105
|
|
|
{ |
106
|
|
|
$this->mediaId = $mediaId; |
|
|
|
|
107
|
|
|
|
108
|
|
|
$this->setVideoSettings('moderation'); |
109
|
|
|
|
110
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$results = $this->client->startContentModeration($this->settings); |
113
|
|
|
|
114
|
|
|
if ($results['JobId']) { |
115
|
|
|
$this->updateJobId($results['JobId'], 'faces'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $results; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Starts asynchronous detection of text in a stored video. |
123
|
|
|
* |
124
|
|
|
* @param int|null $mediaId |
125
|
|
|
* @param array|null $filters |
126
|
|
|
* @return mixed |
127
|
|
|
* @throws \Exception |
128
|
|
|
*/ |
129
|
|
|
public function detectVideoText($mediaId = null, array $filters = null) |
130
|
|
|
{ |
131
|
|
|
$this->mediaId = $mediaId; |
|
|
|
|
132
|
|
|
|
133
|
|
|
$this->setVideoSettings('ocr'); |
134
|
|
|
|
135
|
|
|
if (is_array($filters)) { |
136
|
|
|
$this->settings['Filters'] = $filters; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$results = $this->client->startTextDetection($this->settings); |
140
|
|
|
|
141
|
|
|
if ($results['JobId']) { |
142
|
|
|
$this->updateJobId($results['JobId'], 'ocr'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $results; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the labels from the video analysis. |
150
|
|
|
* |
151
|
|
|
* @param string $jobId |
152
|
|
|
* @param int $mediaId |
153
|
|
|
* @return \Aws\Result |
154
|
|
|
* @throws \Exception |
155
|
|
|
*/ |
156
|
|
|
public function getLabelsByJobId(string $jobId, int $mediaId) |
157
|
|
|
{ |
158
|
|
|
$results = $this->client->getLabelDetection([ |
159
|
|
|
'JobId' => $jobId, |
160
|
|
|
]); |
161
|
|
|
|
162
|
|
|
$this->updateVideoResults($results->toArray(), 'labels', $mediaId); |
|
|
|
|
163
|
|
|
|
164
|
|
|
return $results; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the faces from the video analysis. |
169
|
|
|
* |
170
|
|
|
* @param string $jobId |
171
|
|
|
* @param int $mediaId |
172
|
|
|
* @return \Aws\Result |
173
|
|
|
* @throws \Exception |
174
|
|
|
*/ |
175
|
|
|
public function getFacesByJobId(string $jobId, int $mediaId) |
176
|
|
|
{ |
177
|
|
|
$results = $this->client->getFaceDetection([ |
178
|
|
|
'JobId' => $jobId, |
179
|
|
|
]); |
180
|
|
|
|
181
|
|
|
$this->updateVideoResults($results->toArray(), 'faces', $mediaId); |
182
|
|
|
|
183
|
|
|
return $results; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the "content moderation" from the video analysis. |
188
|
|
|
* |
189
|
|
|
* @param string $jobId |
190
|
|
|
* @param int $mediaId |
191
|
|
|
* @return \Aws\Result |
192
|
|
|
* @throws \Exception |
193
|
|
|
*/ |
194
|
|
|
public function getContentModerationByJobId(string $jobId, int $mediaId) |
195
|
|
|
{ |
196
|
|
|
$results = $this->client->getContentModeration([ |
197
|
|
|
'JobId' => $jobId, |
198
|
|
|
]); |
199
|
|
|
|
200
|
|
|
$this->updateVideoResults($results->toArray(), 'moderation', $mediaId); |
201
|
|
|
|
202
|
|
|
return $results; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the faces from a video analysis. |
207
|
|
|
* |
208
|
|
|
* @param string $jobId |
209
|
|
|
* @param int $mediaId |
210
|
|
|
* @return \Aws\Result |
211
|
|
|
* @throws \Exception |
212
|
|
|
*/ |
213
|
|
|
public function getTextDetectionByJobId(string $jobId, int $mediaId) |
214
|
|
|
{ |
215
|
|
|
$results = $this->client->getTextDetection([ |
216
|
|
|
'JobId' => $jobId, |
217
|
|
|
]); |
218
|
|
|
|
219
|
|
|
$this->updateVideoResults($results->toArray(), 'ocr', $mediaId); |
220
|
|
|
|
221
|
|
|
return $results; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|