1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace AhmadMayahi\Vision; |
||
6 | |||
7 | use AhmadMayahi\Vision\Detectors\CropHints as CropHintsDetector; |
||
8 | use AhmadMayahi\Vision\Detectors\Face as FaceDetector; |
||
9 | use AhmadMayahi\Vision\Detectors\ImageProperties as ImagePropertiesDetector; |
||
10 | use AhmadMayahi\Vision\Detectors\ImageText as ImageTextDetector; |
||
11 | use AhmadMayahi\Vision\Detectors\Label as LabelDetector; |
||
12 | use AhmadMayahi\Vision\Detectors\Landmark as LandmarkDetector; |
||
13 | use AhmadMayahi\Vision\Detectors\Logo as LogoDetector; |
||
14 | use AhmadMayahi\Vision\Detectors\ObjectLocalizer as ObjectLocalizerDetector; |
||
15 | use AhmadMayahi\Vision\Detectors\SafeSearch as SafeSearchDetector; |
||
16 | use AhmadMayahi\Vision\Detectors\Web as WebDetector; |
||
17 | use AhmadMayahi\Vision\Exceptions\FileException; |
||
18 | use AhmadMayahi\Vision\Support\File; |
||
19 | use AhmadMayahi\Vision\Support\Image; |
||
20 | use Google\Cloud\Vision\V1\ImageAnnotatorClient; |
||
21 | |||
22 | class Vision |
||
23 | { |
||
24 | /** |
||
25 | * @var string|resource|\SplFileInfo|\SplFileObject |
||
26 | */ |
||
27 | protected $inputFile = null; |
||
28 | |||
29 | protected static ?self $instance = null; |
||
30 | |||
31 | public function __construct(protected Config $config, protected ImageAnnotatorClient $annotatorClient) |
||
32 | { |
||
33 | } |
||
34 | |||
35 | public static function init(Config $config, ImageAnnotatorClient $client = null): static |
||
36 | { |
||
37 | $client = $client ?? new ImageAnnotatorClient($config->getConnectorConfig()); |
||
38 | |||
39 | if (is_null(self::$instance)) { |
||
40 | self::$instance = new self($config, $client); |
||
41 | } |
||
42 | |||
43 | return self::$instance; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string|resource|\SplFileInfo|\SplFileObject $file |
||
48 | * |
||
49 | * @return Vision|static |
||
50 | * @throws \Exception |
||
51 | */ |
||
52 | public function file($file): static |
||
53 | { |
||
54 | $this->inputFile = $file; |
||
55 | |||
56 | return $this; |
||
57 | } |
||
58 | |||
59 | public function cropHintsDetection(): CropHintsDetector |
||
60 | { |
||
61 | return new CropHintsDetector( |
||
62 | $this->annotatorClient, |
||
63 | $this->getFile(), |
||
64 | new Image($this->getFile()), |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | public function faceDetection(): FaceDetector |
||
69 | { |
||
70 | return new FaceDetector( |
||
71 | imageAnnotatorClient: $this->annotatorClient, |
||
72 | file: $this->getFile(), |
||
73 | image: new Image($this->getFile()), |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | public function imageTextDetection(): ImageTextDetector |
||
78 | { |
||
79 | return new ImageTextDetector($this->annotatorClient, $this->getFile()); |
||
80 | } |
||
81 | |||
82 | public function imagePropertiesDetection(): ImagePropertiesDetector |
||
83 | { |
||
84 | return new ImagePropertiesDetector($this->annotatorClient, $this->getFile()); |
||
85 | } |
||
86 | |||
87 | public function labelDetection(): LabelDetector |
||
88 | { |
||
89 | return new LabelDetector($this->annotatorClient, $this->getFile()); |
||
90 | } |
||
91 | |||
92 | public function landmarkDetection(): LandmarkDetector |
||
93 | { |
||
94 | return new LandmarkDetector($this->annotatorClient, $this->getFile()); |
||
95 | } |
||
96 | |||
97 | public function logoDetection(): LogoDetector |
||
98 | { |
||
99 | return new LogoDetector($this->annotatorClient, $this->getFile()); |
||
100 | } |
||
101 | |||
102 | public function objectLocalizer(): ObjectLocalizerDetector |
||
103 | { |
||
104 | return new ObjectLocalizerDetector( |
||
105 | imageAnnotatorClient: $this->annotatorClient, |
||
106 | file: $this->getFile(), |
||
107 | image: new Support\Image($this->getFile()), |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | public function safeSearchDetection(): SafeSearchDetector |
||
112 | { |
||
113 | return new SafeSearchDetector($this->annotatorClient, $this->getFile()); |
||
114 | } |
||
115 | |||
116 | public function webDetection(): WebDetector |
||
117 | { |
||
118 | return new WebDetector($this->annotatorClient, $this->getFile()); |
||
119 | } |
||
120 | |||
121 | protected function getFile(): File |
||
122 | { |
||
123 | if (! $this->inputFile) { |
||
124 | throw new FileException('Please specify the file!'); |
||
125 | } |
||
126 | |||
127 | return new File($this->inputFile, $this->config->getTempDirPath()); |
||
128 | } |
||
129 | } |
||
130 |