Skip to content

Instantly share code, notes, and snippets.

@atultiwari
Forked from wfng92/draw.py
Created May 1, 2022 19:00
Show Gist options
  • Save atultiwari/70e34ba97ef8ebbcd10b4782f55e5edb to your computer and use it in GitHub Desktop.
Save atultiwari/70e34ba97ef8ebbcd10b4782f55e5edb to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw
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.save("example.jpg")
img.show()
image_filename = "images/medical_pills.jpg"
label_filename = "labels/medical_pills.txt"
bboxes = []
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment