|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Meema\MediaRecognition\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Meema\MediaRecognition\Models\MediaRecognition; |
|
7
|
|
|
|
|
8
|
|
|
trait CanRecognizeImages |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The input image as base64-encoded bytes. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string|null |
|
14
|
|
|
*/ |
|
15
|
|
|
protected ?string $blob = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Set the base64 encoded image. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $blob |
|
21
|
|
|
* |
|
22
|
|
|
* @return $this |
|
23
|
|
|
*/ |
|
24
|
|
|
public function blob(string $blob) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->blob = $blob; |
|
27
|
|
|
|
|
28
|
|
|
return $this; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Sets the image to be analyzed. |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
* @throws \Exception |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function setImageSettings(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->ensureSourceIsNotNull(); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if (is_string($this->blob)) { |
|
42
|
|
|
$this->settings['Image'] = [ |
|
|
|
|
|
|
43
|
|
|
'Bytes' => $this->blob, |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$disk = $this->disk ?? config('media-recognition.disk'); |
|
50
|
|
|
$bucketName = config("filesystems.disks.$disk.bucket"); |
|
51
|
|
|
|
|
52
|
|
|
if (! $bucketName) { |
|
53
|
|
|
throw new Exception('Please make sure to set a S3 bucket name.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->settings['Image'] = [ |
|
57
|
|
|
'S3Object' => [ |
|
58
|
|
|
'Bucket' => $bucketName, |
|
59
|
|
|
'Name' => $this->source, |
|
60
|
|
|
], |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $mediaId |
|
66
|
|
|
* @param null $minConfidence |
|
|
|
|
|
|
67
|
|
|
* @param null $maxLabels |
|
|
|
|
|
|
68
|
|
|
* @return mixed |
|
69
|
|
|
* @throws \Exception |
|
70
|
|
|
*/ |
|
71
|
|
|
public function detectImageLabels($mediaId, $minConfidence = null, $maxLabels = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->setImageSettings(); |
|
74
|
|
|
|
|
75
|
|
|
$this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence'); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if (is_int($maxLabels)) { |
|
|
|
|
|
|
78
|
|
|
$this->settings['MaxLabels'] = $maxLabels; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$results = $this->client->detectLabels($this->settings); |
|
82
|
|
|
|
|
83
|
|
|
$this->updateOrCreate('labels', $mediaId, $results); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
return $results; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param int|null $mediaId |
|
90
|
|
|
* @param array $attributes |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
* @throws \Exception |
|
93
|
|
|
*/ |
|
94
|
|
|
public function detectImageFaces($mediaId = null, $attributes = ['DEFAULT']) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->setImageSettings(); |
|
97
|
|
|
|
|
98
|
|
|
$this->settings['Attributes'] = $attributes; |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$results = $this->client->detectFaces($this->settings); |
|
101
|
|
|
|
|
102
|
|
|
$this->updateOrCreate('faces', $mediaId, $results); |
|
103
|
|
|
|
|
104
|
|
|
return $results; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|