-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
35 lines (25 loc) · 951 Bytes
/
visualize.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
from PIL import Image, ImageDraw
image_filename = "dataset/images_test/knife_853.jpg"
label_filename = "dataset/annotations2_test/knife_853.txt"
bboxes = []
def yolo_to_xml_bbox(bbox, w, h):
# x_center, y_center width heigth
w_half_len = (bbox[2] * w) / 2
h_half_len = (bbox[3] * h) / 2
xmin = int((bbox[0] * w) - w_half_len)
ymin = int((bbox[1] * h) - h_half_len)
xmax = int((bbox[0] * w) + w_half_len)
ymax = int((bbox[1] * h) + h_half_len)
return [xmin, ymin, xmax, ymax]
def draw_image(img, bboxes):
draw = ImageDraw.Draw(img)
for bbox in bboxes:
draw.rectangle(bbox, outline="red", width=2)
img.show()
img = Image.open(image_filename)
with open(label_filename, 'r', encoding='utf8') as f:
for line in f:
data = line.strip().split(' ')
bbox = [float(x) for x in data[1:]]
bboxes.append(yolo_to_xml_bbox(bbox, img.width, img.height))
draw_image(img, bboxes)