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 $mediaId |
65
|
|
|
* @param int|null $minConfidence |
66
|
|
|
* @param null $maxLabels |
|
|
|
|
67
|
|
|
* @return mixed |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
|
|
public function detectImageLabels($mediaId, $minConfidence = null, $maxLabels = null) |
71
|
|
|
{ |
72
|
|
|
$this->setImageSettings(); |
73
|
|
|
|
74
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if (is_int($maxLabels)) { |
|
|
|
|
77
|
|
|
$this->settings['MaxLabels'] = $maxLabels; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$results = $this->client->detectLabels($this->settings); |
81
|
|
|
|
82
|
|
|
$this->updateOrCreate('labels', $mediaId, $results); |
|
|
|
|
83
|
|
|
|
84
|
|
|
return $results; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int|null $mediaId |
89
|
|
|
* @param array $attributes |
90
|
|
|
* @return mixed |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
public function detectImageFaces($mediaId = null, $attributes = ['DEFAULT']) |
94
|
|
|
{ |
95
|
|
|
$this->setImageSettings(); |
96
|
|
|
|
97
|
|
|
$this->settings['Attributes'] = $attributes; |
|
|
|
|
98
|
|
|
|
99
|
|
|
$results = $this->client->detectFaces($this->settings); |
100
|
|
|
|
101
|
|
|
$this->updateOrCreate('faces', $mediaId, $results); |
102
|
|
|
|
103
|
|
|
return $results; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int|null $mediaId |
108
|
|
|
* @param int|null $minConfidence |
109
|
|
|
* @return mixed |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
|
|
public function detectImageModeration($mediaId = null, $minConfidence = null) |
113
|
|
|
{ |
114
|
|
|
$this->setImageSettings(); |
115
|
|
|
|
116
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
117
|
|
|
|
118
|
|
|
$results = $this->client->detectModerationLabels($this->settings); |
119
|
|
|
|
120
|
|
|
$this->updateOrCreate('moderation', $mediaId, $results); |
121
|
|
|
|
122
|
|
|
return $results; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param int|null $mediaId |
127
|
|
|
* @param array|null $filters |
128
|
|
|
* @return mixed |
129
|
|
|
* @throws \Exception |
130
|
|
|
*/ |
131
|
|
|
public function detectImageText($mediaId = null, array $filters = null) |
132
|
|
|
{ |
133
|
|
|
$this->setImageSettings(); |
134
|
|
|
|
135
|
|
|
if (is_array($filters)) { |
136
|
|
|
$this->settings['Filters'] = $filters; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$results = $this->client->detectText($this->settings); |
140
|
|
|
|
141
|
|
|
$this->updateOrCreate('ocr', $mediaId, $results); |
142
|
|
|
|
143
|
|
|
return $results; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|