ObjectLocalizer::detect()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 23
rs 9.7666
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\Contracts\File;
9
use AhmadMayahi\Vision\Data\LocalizedObject as LocalizedObjectData;
10
use AhmadMayahi\Vision\Data\NormalizedVertex as NormalizedVertexData;
11
use AhmadMayahi\Vision\Detectors\ObjectLocalizer\DrawBoxAroundObjects;
12
use AhmadMayahi\Vision\Detectors\ObjectLocalizer\DrawBoxAroundObjectsWithText;
13
use AhmadMayahi\Vision\Support\AbstractDetector;
14
use AhmadMayahi\Vision\Support\Image;
15
use AhmadMayahi\Vision\Traits\Arrayable;
16
use AhmadMayahi\Vision\Traits\Jsonable;
17
use Generator;
18
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
19
use Google\Cloud\Vision\V1\LocalizedObjectAnnotation;
20
use Google\Cloud\Vision\V1\NormalizedVertex;
21
use Google\Protobuf\Internal\RepeatedField;
22
23
class ObjectLocalizer extends AbstractDetector implements Detectable
24
{
25
    use Arrayable;
26
    use Jsonable;
27
28
    public function __construct(
29
        protected ImageAnnotatorClient $imageAnnotatorClient,
30
        protected File $file,
31
        protected Image $image
32
    ) {
33
    }
34
35
    public function getOriginalResponse(): RepeatedField
36
    {
37
        $response = $this
38
            ->imageAnnotatorClient
39
            ->objectLocalization($this->file->toVisionFile());
40
41
        return $response->getLocalizedObjectAnnotations();
42
    }
43
44
    public function detect(): ?Generator
45
    {
46
        $response = $this->getOriginalResponse();
47
48
        if (0 === $response->count()) {
49
            return null;
50
        }
51
52
        /** @var LocalizedObjectAnnotation $obj */
53
        foreach ($response as $obj) {
54
            $vertices = $obj->getBoundingPoly()->getNormalizedVertices();
55
56
            yield new LocalizedObjectData(
57
                name: $obj->getName(),
58
                mid: $obj->getMid(),
59
                languageCode: $obj->getLanguageCode(),
60
                score: $obj->getScore(),
61
                normalizedVertices: array_map(function (NormalizedVertex $vertex) {
62
                    return new NormalizedVertexData(
63
                        x: $vertex->getX(),
64
                        y: $vertex->getY()
65
                    );
66
                }, iterator_to_array($vertices)),
67
            );
68
        }
69
    }
70
71
    public function drawBoxAroundObjects(): DrawBoxAroundObjects
72
    {
73
        return new DrawBoxAroundObjects($this, $this->image);
74
    }
75
76
    public function drawBoxAroundObjectsWithText(): DrawBoxAroundObjectsWithText
77
    {
78
        return (new DrawBoxAroundObjectsWithText($this, $this->image));
79
    }
80
}
81