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