-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbox_drawer.py
83 lines (65 loc) · 2.84 KB
/
bbox_drawer.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
from PIL import Image , ImageDraw
def draw_bounding_boxes_with_original_coordinates(main_dir, frame_name):
frame_image_path = os.path.join(main_dir, 'images', f'{frame_name}.jpg')
txt_file_path = os.path.join(main_dir, 'labels', f'{frame_name}.txt')
try:
image = Image.open(frame_image_path)
original_width, original_height = image.size
print('Image Width:', original_width, 'Image Height:', original_height)
except FileNotFoundError:
print(f"Error: Could not load the image at '{frame_image_path}'.")
return
draw = ImageDraw.Draw(image)
if not os.path.exists(txt_file_path):
print(f"Error: Annotations file does not exist at '{txt_file_path}'.")
return
with open(txt_file_path, 'r') as file:
for line in file:
coordinates = line.strip().split()
if len(coordinates) == 5:
class_id, x, y, width, height = map(float, coordinates) # Convert from str to float
print('x:', x, 'y:', y, 'width:', width, 'height:', height)
draw.rectangle(
[x, y, x+ width , y + height],
outline='red',
width=2
)
image.show()
def yolo_to_original_coordinates(cx, cy, w, h, img_width, img_height):
center_x = cx * img_width
center_y = cy * img_height
width = w * img_width
height = h * img_height
x1 = int(center_x - (width / 2))
y1 = int(center_y - (height / 2))
x2 = int(center_x + (width / 2))
y2 = int(center_y + (height / 2))
return x1, y1, x2, y2
def draw_bounding_boxes_with_yolo_format(main_dir, frame_name):
frame_image_path = os.path.join(main_dir, 'images', f'{frame_name}.jpg')
txt_file_path = os.path.join(main_dir, 'labels', f'{frame_name}.txt')
try:
image = Image.open(frame_image_path)
original_width, original_height = image.size
print('Image Width:', original_width, 'Image Height:', original_height)
except FileNotFoundError:
print(f"Error: Could not load the image at '{frame_image_path}'.")
return
draw = ImageDraw.Draw(image)
if not os.path.exists(txt_file_path):
print(f"Error: Annotations file does not exist at '{txt_file_path}'.")
return
with open(txt_file_path, 'r') as file:
for line in file:
coordinates = line.strip().split()
if len(coordinates) == 5:
class_id, x, y, width, height = map(float, coordinates) # Convert from str to float
print('x:', x, 'y:', y, 'width:', width, 'height:', height)
x1, y1, x2, y2 = yolo_to_original_coordinates(x, y, width, height)
draw.rectangle(
[x1, y1, x2, y2],
outline='red',
width=2
)
image.show()