Passed
Push — master ( fba003...f4c767 )
by Chris
17:41 queued 02:40
created

CanRecognizeVideos::detectVideoText()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 17
rs 10
c 0
b 0
f 0
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 $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;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
60
        $this->setVideoSettings('labels');
61
        $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...
62
        $this->settings['MaxResults'] = $maxResults;
63
64
        $results = $this->client->startLabelDetection($this->settings);
65
66
        if ($results['JobId']) {
67
            $this->updateJobId($results['JobId'], 'labels');
0 ignored issues
show
Bug introduced by
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

67
            $this->/** @scrutinizer ignore-call */ 
68
                   updateJobId($results['JobId'], 'labels');
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82
83
        $this->setVideoSettings('faces');
84
85
        $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...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
107
108
        $this->setVideoSettings('moderation');
109
110
        $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...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
132
133
        $this->setVideoSettings('ocr');
134
135
        if (is_array($filters)) {
136
            $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...
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);
0 ignored issues
show
Bug introduced by
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

162
        $this->/** @scrutinizer ignore-call */ 
163
               updateVideoResults($results->toArray(), 'labels', $mediaId);
Loading history...
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