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