1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meema\MediaRecognition\Jobs; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
7
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
8
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
9
|
|
|
use Illuminate\Queue\SerializesModels; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use Meema\MediaRecognition\Events\LabelAnalysisCompleted; |
13
|
|
|
use Meema\MediaRecognition\Facades\Recognize; |
14
|
|
|
|
15
|
|
|
class StartLabelDetection implements ShouldQueue |
16
|
|
|
{ |
17
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
|
|
|
|
18
|
|
|
|
19
|
|
|
private string $path; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* $mimeType may be 'image', 'video' or the actual mime type of the file. |
23
|
|
|
* It allows us to reduce an HTTP request to check for the mime type. |
24
|
|
|
* If not assigned, it will check the mime type for whether it is an image or a video source. |
25
|
|
|
* |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
private ?string $mimeType = null; |
29
|
|
|
|
30
|
|
|
private ?int $mediaId = null; |
31
|
|
|
|
32
|
|
|
private ?int $minConfidence; |
33
|
|
|
|
34
|
|
|
private int $maxResults; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new job instance. |
38
|
|
|
* |
39
|
|
|
* @param string $path |
40
|
|
|
* @param string|null $mimeType |
41
|
|
|
* @param int|null $mediaId |
42
|
|
|
* @param int|null $minConfidence |
43
|
|
|
* @param int $maxResults |
44
|
|
|
*/ |
45
|
|
|
public function __construct(string $path, $mimeType = null, $mediaId = null, $minConfidence = null, $maxResults = 1000) |
46
|
|
|
{ |
47
|
|
|
$this->path = $path; |
48
|
|
|
$this->mimeType = $mimeType; |
49
|
|
|
$this->mediaId = $mediaId; |
50
|
|
|
$this->minConfidence = $minConfidence; |
51
|
|
|
$this->maxResults = $maxResults; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Execute the job. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
|
|
public function handle() |
61
|
|
|
{ |
62
|
|
|
$this->ensureMimeTypeIsSet(); |
63
|
|
|
|
64
|
|
|
if (Str::contains($this->mimeType, 'image')) { |
65
|
|
|
$result = Recognize::source($this->path, $this->mimeType, $this->mediaId)->detectImageLabels($this->minConfidence, $this->maxResults); |
66
|
|
|
|
67
|
|
|
// we need to manually fire the event for image analyses because unlike the video analysis, |
68
|
|
|
// AWS is not sending a webhook upon completion of the image analysis |
69
|
|
|
event(new LabelAnalysisCompleted($result, $this->mediaId)); |
70
|
|
|
|
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (Str::contains($this->mimeType, 'video')) { |
75
|
|
|
Recognize::source($this->path, $this->mimeType, $this->mediaId)->detectVideoLabels($this->minConfidence, $this->maxResults); |
76
|
|
|
|
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new \Exception('$mimeType does neither indicate being a video nor an image'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function ensureMimeTypeIsSet() |
84
|
|
|
{ |
85
|
|
|
if (is_null($this->mimeType)) { |
86
|
|
|
$this->mimeType = Storage::disk(config('media-recognition.disk'))->mimeType($this->path); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|