-
-
Save atultiwari/70e34ba97ef8ebbcd10b4782f55e5edb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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