Passed
Push — master ( 3a61d1...066a33 )
by Chris
14:55
created

Rekognition::blob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Meema\MediaRecognition\Recognizers;
4
5
use Aws\Rekognition\RekognitionClient;
6
use Exception;
7
use Meema\MediaRecognition\Contracts\MediaRecognition as MediaRecognitionInterface;
8
use Meema\MediaRecognition\Models\MediaRecognition;
9
use Meema\MediaRecognition\Traits\InteractsWithStorage;
10
11
class Rekognition implements MediaRecognitionInterface
12
{
13
    use InteractsWithStorage;
14
15
    /**
16
     * Client instance of MediaRecognition.
17
     *
18
     * @var \Aws\Rekognition\RekognitionClient
19
     */
20
    protected RekognitionClient $client;
21
22
    /**
23
     * The settings provided to the Rekognition job.
24
     *
25
     * @var array
26
     */
27
    protected array $settings;
28
29
    /**
30
     * The input image as base64-encoded bytes.
31
     *
32
     * @var string
33
     */
34
    protected string $blob;
35
36
    /**
37
     * Construct converter.
38
     *
39
     * @param \Aws\Rekognition\RekognitionClient $client
40
     */
41
    public function __construct(RekognitionClient $client)
42
    {
43
        $this->client = $client;
44
    }
45
46
    /**
47
     * Get the MediaRecognition Client.
48
     *
49
     * @return \Aws\Rekognition\RekognitionClient
50
     */
51
    public function getClient(): RekognitionClient
52
    {
53
        return $this->client;
54
    }
55
56
    /**
57
     * Set the base64 encoded image.
58
     *
59
     * @param string $blob
60
     *
61
     * @return $this
62
     */
63
    public function blob(string $blob)
64
    {
65
        $this->blob = $blob;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Detects labels/objects in an image.
72
     *
73
     * @param int|null $mediaId
74
     * @param int|null $minConfidence
75
     * @param int|null $maxLabels
76
     * @return \Aws\Result
77
     * @throws \Exception
78
     */
79
    public function detectLabels($mediaId = null, int $minConfidence = null, int $maxLabels = null)
80
    {
81
        $settings = $this->setImage();
82
83
        $settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence');
84
85
        if (is_int($maxLabels)) {
86
            $settings['MaxLabels'] = $maxLabels;
87
        }
88
89
        $results = $this->client->detectLabels($settings);
90
91
        if (! config('media-recognition.track_media_recognitions')) {
92
            return $results;
93
        }
94
95
        if (is_null($mediaId)) {
96
            throw new Exception('Please make sure to set a $mediaId.');
97
        }
98
99
        MediaRecognition::create($results, $mediaId);
100
101
        return $results;
102
    }
103
104
    /**
105
     * Sets the image to be analyzed.
106
     *
107
     * @return array
108
     * @throws \Exception
109
     */
110
    protected function setImage(): array
111
    {
112
113
        if (is_string($this->blob)) {
0 ignored issues
show
introduced by
The condition is_string($this->blob) is always true.
Loading history...
114
            $settings['Image'] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
$settings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $settings = array(); before regardless.
Loading history...
115
                'Bytes' => $this->blob,
116
            ];
117
118
            return $settings;
119
        }
120
121
        $disk = $this->disk ?? config('media-recognition.disk');
122
        $bucketName = config("filesystems.disks.$disk.bucket");
123
124
        if (! $bucketName) {
125
            throw new Exception('Please make sure to set a S3 bucket name.');
126
        }
127
128
        $settings['Image'] = [
129
            'S3Object' => [
130
                'Bucket' => $bucketName,
131
                'Name' => $this->path,
132
            ],
133
        ];
134
135
        return $settings;
136
    }
137
138
    /**
139
     * Detects labels/objects in an image.
140
     */
141
    public function detectFaces()
142
    {
143
        //return $this->client->detectLabels([
144
        //'NotificationChannel' => [
145
        //    "RoleArn" => config('media-recognition.iam_arn'),
146
        //    "SNSTopicArn" => config('media-recognition.sns_topic_arn'),
147
        //],
148
        //    'Image' => [
149
        //        'S3Object' => [
150
        //            'Bucket' => 'meema-stage',
151
        //            'Name' => 'test-media/people.jpg',
152
        //        ],
153
        //    ],
154
        //]);
155
    }
156
157
    /**
158
     * Detects text in an image (OCR).
159
     */
160
    public function detectText($minConfidence = 50)
161
    {
162
        $bytes = '';
163
164
        $results = $this->client->detectText([
0 ignored issues
show
Unused Code introduced by
The assignment to $results is dead and can be removed.
Loading history...
165
            'Image' => ['Bytes' => $bytes],
166
            'MinConfidence' => $minConfidence,
167
        ])['TextDetections'];
168
    }
169
}
170