1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meema\MediaRecognition\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
trait CanRecognizeImages |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The input image as base64-encoded bytes. |
11
|
|
|
* |
12
|
|
|
* @var string|null |
13
|
|
|
*/ |
14
|
|
|
protected ?string $blob = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Set the base64 encoded image. |
18
|
|
|
* |
19
|
|
|
* @param string $blob |
20
|
|
|
* |
21
|
|
|
* @return $this |
22
|
|
|
*/ |
23
|
|
|
public function blob(string $blob) |
24
|
|
|
{ |
25
|
|
|
$this->blob = $blob; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets the image to be analyzed. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
|
|
protected function setImageSettings(): void |
37
|
|
|
{ |
38
|
|
|
$this->ensureSourceIsNotNull(); |
|
|
|
|
39
|
|
|
|
40
|
|
|
if (is_string($this->blob)) { |
41
|
|
|
$this->settings['Image'] = [ |
|
|
|
|
42
|
|
|
'Bytes' => $this->blob, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$disk = $this->disk ?? config('media-recognition.disk'); |
49
|
|
|
$bucketName = config("filesystems.disks.$disk.bucket"); |
50
|
|
|
|
51
|
|
|
if (! $bucketName) { |
52
|
|
|
throw new Exception('Please make sure to set a S3 bucket name.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->settings['Image'] = [ |
56
|
|
|
'S3Object' => [ |
57
|
|
|
'Bucket' => $bucketName, |
58
|
|
|
'Name' => $this->source, |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int|null $minConfidence |
65
|
|
|
* @param null $maxLabels |
|
|
|
|
66
|
|
|
* @return mixed |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
public function detectImageLabels($minConfidence = null, $maxLabels = null) |
70
|
|
|
{ |
71
|
|
|
$this->setImageSettings(); |
72
|
|
|
|
73
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if (is_int($maxLabels)) { |
|
|
|
|
76
|
|
|
$this->settings['MaxLabels'] = $maxLabels; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$results = $this->client->detectLabels($this->settings); |
80
|
|
|
|
81
|
|
|
if (is_null($this->mediaId)) { |
82
|
|
|
return $results; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->updateOrCreate('labels', $results); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $results; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $attributes |
92
|
|
|
* @return mixed |
93
|
|
|
* @throws \Exception |
94
|
|
|
*/ |
95
|
|
|
public function detectImageFaces($attributes = ['DEFAULT']) |
96
|
|
|
{ |
97
|
|
|
$this->setImageSettings(); |
98
|
|
|
|
99
|
|
|
$this->settings['Attributes'] = $attributes; |
|
|
|
|
100
|
|
|
|
101
|
|
|
$results = $this->client->detectFaces($this->settings); |
102
|
|
|
|
103
|
|
|
if (is_null($this->mediaId)) { |
104
|
|
|
return $results; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->updateOrCreate('faces', $results); |
108
|
|
|
|
109
|
|
|
return $results; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param int|null $minConfidence |
114
|
|
|
* @return mixed |
115
|
|
|
* @throws \Exception |
116
|
|
|
*/ |
117
|
|
|
public function detectImageModeration($minConfidence = null) |
118
|
|
|
{ |
119
|
|
|
$this->setImageSettings(); |
120
|
|
|
|
121
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$results = $this->client->detectModerationLabels($this->settings); |
124
|
|
|
|
125
|
|
|
if (is_null($this->mediaId)) { |
126
|
|
|
return $results; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->updateOrCreate('moderation', $results); |
130
|
|
|
|
131
|
|
|
return $results; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param array|null $filters |
136
|
|
|
* @return mixed |
137
|
|
|
* @throws \Exception |
138
|
|
|
*/ |
139
|
|
|
public function detectImageText(array $filters = null) |
140
|
|
|
{ |
141
|
|
|
$this->setImageSettings(); |
142
|
|
|
|
143
|
|
|
if (is_array($filters)) { |
144
|
|
|
$this->settings['Filters'] = $filters; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$results = $this->client->detectText($this->settings); |
148
|
|
|
|
149
|
|
|
if (is_null($this->mediaId)) { |
150
|
|
|
return $results; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->updateOrCreate('ocr', $results); |
154
|
|
|
|
155
|
|
|
return $results; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|