Label   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 26
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 12 3
A getOriginalResponse() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AhmadMayahi\Vision\Detectors;
6
7
use AhmadMayahi\Vision\Contracts\Detectable;
8
use AhmadMayahi\Vision\Data\Label as LabelData;
9
use AhmadMayahi\Vision\Support\AbstractDetector;
10
use AhmadMayahi\Vision\Traits\Arrayable;
11
use AhmadMayahi\Vision\Traits\Jsonable;
12
use Generator;
13
use Google\Cloud\Vision\V1\EntityAnnotation;
14
use Google\Protobuf\Internal\RepeatedField;
15
16
/**
17
 * Detect and extract information about entities in an image, across a broad group of categories..\
18
 *
19
 * @see https://cloud.google.com/vision/docs/labels
20
 */
21
class Label extends AbstractDetector implements Detectable
22
{
23
    use Arrayable;
24
    use Jsonable;
25
26
    public function getOriginalResponse(): RepeatedField
27
    {
28
        $response = $this
29
            ->imageAnnotatorClient
30
            ->labelDetection($this->file->toVisionFile());
31
32
        return $response->getLabelAnnotations();
33
    }
34
35
    public function detect(): ?Generator
36
    {
37
        $response = $this->getOriginalResponse();
38
39
        if (0 === $response->count()) {
40
            return null;
41
        }
42
43
        /** @var EntityAnnotation $item */
44
        foreach ($response as $item) {
45
            yield new LabelData(
46
                description: $item->getDescription(),
47
            );
48
        }
49
    }
50
}
51