|
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 the detection of labels/objects in a 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 startDetectingLabels(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 int $mediaId |
|
77
|
|
|
* @param array $attributes |
|
78
|
|
|
* @return \Aws\Result |
|
79
|
|
|
* @throws \Exception |
|
80
|
|
|
*/ |
|
81
|
|
|
public function startDetectingFaces($mediaId = null, $attributes = ['DEFAULT']) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$this->mediaId = $mediaId; |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$this->setVideoSettings('faces'); |
|
86
|
|
|
|
|
87
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$results = $this->client->startFaceDetection($this->settings); |
|
90
|
|
|
|
|
91
|
|
|
if ($results['JobId']) { |
|
92
|
|
|
$this->updateJobId($results['JobId'], 'faces'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $results; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|