-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize.py
More file actions
38 lines (30 loc) · 1.18 KB
/
visualize.py
File metadata and controls
38 lines (30 loc) · 1.18 KB
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
from PIL import Image, ImageDraw
def show_bbox(image_path):
# convert image path to label path
label_path = image_path.replace('/images/', '/darknet/')
label_path = label_path.replace('.jpg', '.txt')
# Open the image and create ImageDraw object for drawing
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
with open(label_path, 'r') as f:
for line in f.readlines():
# Split the line into five values
label, x, y, w, h = line.split(' ')
# Convert string into float
x = float(x)
y = float(y)
w = float(w)
h = float(h)
# Convert center position, width, height into
# top-left and bottom-right coordinates
W, H = image.size
x1 = (x - w/2) * W
y1 = (y - h/2) * H
x2 = (x + w/2) * W
y2 = (y + h/2) * H
# Draw the bounding box with red lines
draw.rectangle((x1, y1, x2, y2),
outline=(255, 0, 0), # Red in RGB
width=5) # Line width
image.show()
show_bbox('data/images/train/0a0df46ca3f886c9.jpg')