Issues (35)

src/Traits/CanRecognizeVideos.php (8 issues)

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.
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
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'] = [
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 $minConfidence
51
     * @param int $maxResults
52
     * @return \Aws\Result
53
     * @throws \Exception
54
     */
55
    public function detectVideoLabels($minConfidence = null, $maxResults = 1000)
56
    {
57
        $this->setVideoSettings('labels');
58
        $this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence');
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
        $this->settings['MaxResults'] = $maxResults;
60
61
        $results = $this->client->startLabelDetection($this->settings);
62
63
        if ($results['JobId']) {
64
            $this->updateJobId($results['JobId'], 'labels');
0 ignored issues
show
It seems like updateJobId() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            $this->/** @scrutinizer ignore-call */ 
65
                   updateJobId($results['JobId'], 'labels');
Loading history...
65
        }
66
67
        return $results;
68
    }
69
70
    /**
71
     * @param array $attributes
72
     * @return mixed
73
     * @throws \Exception
74
     */
75
    public function detectVideoFaces($attributes = ['DEFAULT'])
76
    {
77
        $this->setVideoSettings('faces');
78
79
        $this->settings['FaceAttributes'] = $attributes;
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
81
        $results = $this->client->startFaceDetection($this->settings);
82
83
        if ($results['JobId']) {
84
            $this->updateJobId($results['JobId'], 'faces');
85
        }
86
87
        return $results;
88
    }
89
90
    /**
91
     * Starts asynchronous detection of unsafe content in a stored video.
92
     *
93
     * @param int|null $minConfidence
94
     * @return mixed
95
     * @throws \Exception
96
     */
97
    public function detectVideoModeration($minConfidence = null)
98
    {
99
        $this->setVideoSettings('moderation');
100
101
        $this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence');
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
102
103
        $results = $this->client->startContentModeration($this->settings);
104
105
        if ($results['JobId']) {
106
            $this->updateJobId($results['JobId'], 'faces');
107
        }
108
109
        return $results;
110
    }
111
112
    /**
113
     * Starts asynchronous detection of text in a stored video.
114
     *
115
     * @param array|null $filters
116
     * @return mixed
117
     * @throws \Exception
118
     */
119
    public function detectVideoText(array $filters = null)
120
    {
121
        $this->setVideoSettings('ocr');
122
123
        if (is_array($filters)) {
124
            $this->settings['Filters'] = $filters;
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
125
        }
126
127
        $results = $this->client->startTextDetection($this->settings);
128
129
        if ($results['JobId']) {
130
            $this->updateJobId($results['JobId'], 'ocr');
131
        }
132
133
        return $results;
134
    }
135
136
    /**
137
     * Get the labels from the video analysis.
138
     *
139
     * @param string $jobId
140
     * @param int|null $mediaId
141
     * @return \Aws\Result
142
     * @throws \Exception
143
     */
144
    public function getLabelsByJobId(string $jobId, int $mediaId = null)
145
    {
146
        $results = $this->client->getLabelDetection([
147
            'JobId' => $jobId,
148
        ]);
149
150
        if (is_null($mediaId)) {
151
            return $results;
152
        }
153
154
        $this->updateVideoResults($results->toArray(), 'labels', $mediaId);
0 ignored issues
show
It seems like updateVideoResults() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
        $this->/** @scrutinizer ignore-call */ 
155
               updateVideoResults($results->toArray(), 'labels', $mediaId);
Loading history...
155
156
        return $results;
157
    }
158
159
    /**
160
     * Get the faces from the video analysis.
161
     *
162
     * @param string $jobId
163
     * @param int|null $mediaId
164
     * @return \Aws\Result
165
     * @throws \Exception
166
     */
167
    public function getFacesByJobId(string $jobId, int $mediaId = null)
168
    {
169
        $results = $this->client->getFaceDetection([
170
            'JobId' => $jobId,
171
        ]);
172
173
        if (is_null($mediaId)) {
174
            return $results;
175
        }
176
177
        $this->updateVideoResults($results->toArray(), 'faces', $mediaId);
178
179
        return $results;
180
    }
181
182
    /**
183
     * Get the "content moderation" from the video analysis.
184
     *
185
     * @param string $jobId
186
     * @param int|null $mediaId
187
     * @return \Aws\Result
188
     * @throws \Exception
189
     */
190
    public function getContentModerationByJobId(string $jobId, int $mediaId = null)
191
    {
192
        $results = $this->client->getContentModeration([
193
            'JobId' => $jobId,
194
        ]);
195
196
        if (is_null($mediaId)) {
197
            return $results;
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|null $mediaId
210
     * @return \Aws\Result
211
     * @throws \Exception
212
     */
213
    public function getTextDetectionByJobId(string $jobId, int $mediaId = null)
214
    {
215
        $results = $this->client->getTextDetection([
216
            'JobId' => $jobId,
217
        ]);
218
219
        if (is_null($mediaId)) {
220
            return $results;
221
        }
222
223
        $this->updateVideoResults($results->toArray(), 'ocr', $mediaId);
224
225
        return $results;
226
    }
227
}
228