Skip to content

Instantly share code, notes, and snippets.

@YimianDai
Last active August 26, 2019 16:44
Show Gist options
  • Save YimianDai/268e127821dac3d9cc05720538badef9 to your computer and use it in GitHub Desktop.
Save YimianDai/268e127821dac3d9cc05720538badef9 to your computer and use it in GitHub Desktop.
Convert Binary Mask Image to BBoX XML
from data import IceSegmentation
from model import PhaseFourierTransform
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import platform, os
from skimage import measure, color
from mxnet import nd
#######################################################
###################### Aux Func #######################
#######################################################
def get_xml_for_bbox(bbox):
ymin, xmin, ymax, xmax = bbox[0], bbox[1], bbox[2], bbox[3]
xml = "\t<object>\n"
xml = xml + "\t\t<name>" + 'Iceberg' + "</name>\n"
xml = xml + "\t\t<pose>Unspecified</pose>\n"
xml = xml + "\t\t<truncated>0</truncated>\n"
xml = xml + "\t\t<difficult>0</difficult>\n"
xml = xml + "\t\t<occluded>0</occluded>\n"
xml = xml + "\t\t<bndbox>\n"
xml = xml + "\t\t\t<xmin>" + str(xmin) + "</xmin>\n"
xml = xml + "\t\t\t<xmax>" + str(xmax) + "</xmax>\n"
xml = xml + "\t\t\t<ymin>" + str(ymin) + "</ymin>\n"
xml = xml + "\t\t\t<ymax>" + str(ymax) + "</ymax>\n"
xml = xml + "\t\t</bndbox>\n"
xml = xml + "\t</object>\n"
return xml
#######################################################
################## Hyper-Parameter ####################
#######################################################
print(os.getcwd())
show_flag = False
if platform.system() == "Darwin":
data_root = os.path.join('~', 'Nutstore Files', 'Dataset', 'Iceberg')
elif platform.system() == "Linux":
data_root = os.path.join('~', 'datasets', 'Iceberg')
else:
raise ValueError('Notice Dataset Path')
train_dataset = IceSegmentation(split="trainvaltest", mode='testval', base_size=512, crop_size=512)
width, height = 512, 512
for i, data in enumerate(train_dataset):
fileName = data[2]
labels = measure.label(data[1].asnumpy(), background=0)
xml = "<annotation>\n\t<folder>" + 'image' + "</folder>\n"
xml = xml + "\t<filename>" + fileName +"</filename>\n"
xml = xml + "\t<size>\n"
xml = xml + "\t\t<width>" + str(width) + "</width>\n"
xml = xml + "\t\t<height>" + str(height) + "</height>\n"
xml = xml + "\t\t<depth>3</depth>\n"
xml = xml + "\t</size>\n"
xml = xml + "\t<segmented>0</segmented>\n"
for i, region in enumerate(measure.regionprops(labels)):
# minr, minc, maxr, maxc = region.bbox
ymin, xmin, ymax, xmax = region.bbox
xml = xml + get_xml_for_bbox([ymin, xmin, ymax, xmax])
xml = xml + "</annotation>"
# print(xml)
filePath = "/Users/grok/Nutstore Files/Dataset/Iceberg/labels/bbox/" + fileName
f = open(filePath+".xml", "w")
f.write(xml)
f.close()
# break
# Reference
# https://dataturks.com/help/ibbx_dataturks_to_pascal_voc_format.php
# 注意,这个代码是生成 bbox label 的,如果是 RectLabel 的 mask 需要的 bbox
# 引导文件,还要再加上 <pixels><id>0</id></pixels>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment