1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AhmadMayahi\Vision\Detectors\ObjectLocalizer; |
6
|
|
|
|
7
|
|
|
use AhmadMayahi\Vision\Contracts\Drawable; |
8
|
|
|
use AhmadMayahi\Vision\Data\LocalizedObject as LocalizedObjectData; |
9
|
|
|
use AhmadMayahi\Vision\Detectors\ObjectLocalizer; |
10
|
|
|
use AhmadMayahi\Vision\Enums\Color; |
11
|
|
|
use AhmadMayahi\Vision\Enums\Font; |
12
|
|
|
use AhmadMayahi\Vision\Support\Image; |
13
|
|
|
|
14
|
|
|
class DrawBoxAroundObjectsWithText implements Drawable |
15
|
|
|
{ |
16
|
|
|
private int $boxColor = Color::GREEN; |
17
|
|
|
|
18
|
|
|
private int $textColor = Color::RED; |
19
|
|
|
|
20
|
|
|
private int $fontSize = 15; |
21
|
|
|
|
22
|
|
|
private string $font = Font::OPEN_SANS_BOLD; |
23
|
|
|
|
24
|
|
|
public function __construct(private ObjectLocalizer $objectLocalizer, private Image $image) |
25
|
|
|
{ |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function draw(): Image |
29
|
|
|
{ |
30
|
|
|
$draw = new DrawBoxAroundObjects($this->objectLocalizer, $this->image); |
31
|
|
|
$draw->boxColor($this->boxColor); |
32
|
|
|
|
33
|
|
|
$draw->callback(function (Image $image, LocalizedObjectData $obj) { |
34
|
|
|
$width = $image->getWidth(); |
35
|
|
|
$height = $image->getHeight(); |
36
|
|
|
|
37
|
|
|
$x1 = $obj->normalizedVertices[0]->x; |
38
|
|
|
$y2 = $obj->normalizedVertices[2]->y; |
39
|
|
|
|
40
|
|
|
$image->writeText( |
41
|
|
|
text: $obj->name, |
42
|
|
|
fontFile: $this->font, |
43
|
|
|
color: $this->textColor, |
44
|
|
|
fontSize: $this->fontSize, |
45
|
|
|
x: intval($x1 * $width) + 5, |
46
|
|
|
y: intval($y2 * $height) - 5 |
47
|
|
|
); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
return $draw->draw(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function boxColor(int $boxColor): static |
54
|
|
|
{ |
55
|
|
|
$this->boxColor = $boxColor; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function textColor(int $textColor): static |
61
|
|
|
{ |
62
|
|
|
$this->textColor = $textColor; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function fontSize(int $fontSize): static |
68
|
|
|
{ |
69
|
|
|
$this->fontSize = $fontSize; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function font(string $font): static |
75
|
|
|
{ |
76
|
|
|
$this->font = $font; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|