File size: 1,960 Bytes
0e83290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from PIL import Image, ImageDraw, ImageFont
import os
from pathlib import Path


def draw(images_path, labels_path):
    pa = Path.cwd() / 'video-test' / 'dt-tomato'
    pal = Path(pa / labels_path)
    dir_list = sorted(pal.glob('*'))
    for each in dir_list:
        draw_labels_on_image(
            pa, images_path, pal, os.path.splitext(os.path.split(each)[1])[0]
        )


def draw_labels_on_image(path, images_path, labels_path, file_name) -> None:
    """Draws labels on an image using the provided labels file."""
    image = Image.open(f'{path}/{images_path}/{file_name}.jpg')
    width, height = image.size

    with open(labels_path / f"{file_name}.txt") as file:
        for line in file:
            class_id, x_center, y_center, width_factor, height_factor = line.split()
            x = float(x_center) * width
            y = float(y_center) * height
            width_factor = int(float(width_factor) * width)
            height_factor = int(float(height_factor) * height)

            font = ImageFont.truetype("arial.ttf", 15)
            draw = ImageDraw.Draw(image)
            draw.text((x, y), class_id, font=font, fill=(255, 0, 0))
            draw.rectangle(
                (
                    (x - width_factor // 2),
                    (y - height_factor // 2),
                    (x + width_factor // 2),
                    (y + height_factor // 2),
                ),
                outline=(255, 255, 255),
                width=2,
            )

    image.save(f'{path}/t{file_name}.jpg', quality=95)


if __name__ == '__main__':
    draw('images', 'lables')

    # # Укажите путь к папке, которую нужно архивировать
    # folder_path = Path.cwd() / 'data' / 'Helmetxy'

    # # Укажите путь и имя архива
    # archive_path = Path.cwd() / 'data' / 'Helmetxy'

    # # Создайте архив
    # shutil.make_archive(archive_path, 'zip', folder_path)