Created
December 17, 2020 00:57
-
-
Save jangsoopark/1f7d2bce9808b70ad2e3c0915daca990 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 | |
import glob | |
import os | |
original_label_dir = '{YOLO DATASET PATH}' | |
image_dir = '{PNG IMAGE DIR}' | |
label_dir = '{KITTI TYPE LABEL PATH}' | |
images = glob.glob(os.path.join(image_dir, '*.png')) | |
# below ones are example of 7 class examples | |
name = [ | |
'bus', | |
'car', | |
'truck', | |
'human', | |
'bicycle', | |
'motorcycle', | |
'phone' | |
] | |
for im in images: | |
fname = os.path.basename(im) | |
print(fname) | |
base_name = os.path.splitext(fname)[0] | |
image = Image.open(im) | |
w, h = image.size | |
image.close() | |
f = open(os.path.join(original_label_dir, '%s.txt' % base_name)) | |
data = f.read() | |
f.close() | |
data = data.split('\n') | |
labels = [] | |
for d in data: | |
if len(d) <= 1: | |
continue | |
d = d.split() | |
d = [int(d[0])] + [float(e) for e in d[1:]] | |
d[1] *= w | |
d[3] *= w | |
d[2] *= h | |
d[4] *= h | |
obj_name = name[d[0]] | |
xmin = d[1] - d[3] / 2 | |
ymin = d[2] - d[4] / 2 | |
xmax = d[1] + d[3] / 2 | |
ymax = d[2] + d[4] / 2 | |
labels.append('%s 0.00 0 0.0 %.2f %.2f %.2f %.2f 0.0 0.0 0.0 0.0 0.0 0.0 0.0' % (obj_name, xmin, ymin, xmax, ymax)) | |
with open(os.path.join(label_dir, '%s.txt' % base_name), 'wt') as f: | |
f.write('\n'.join(labels)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment