解决独立标注的数据,一键转为标准VOC格式
file_save = "yolo_format"
if os.path.exists(file_save):
shutil.rmtree(file_save)
os.makedirs(file_save)
file_annotation = "Annotations"
file_images = "JPEGImages"
classes = []
traintxt = []
valtxt = []
train = {
'images':[],'type':"instances",'annotations':[],'categories':[]}
val = {
'images':[],'type':"instances",'annotations':[],'categories':[]}
for i in os.listdir(file_annotation):
if "xml" not in i:
continue
temp_cat = {
"supercategory":"none","id":0,"name":"080002"}
temp_ann = {
"area":1164024,"iscrowd":0,"category_id":12,"ignore":0,"image_id":0,"id":0, "bbox":[]}
temp_img = {
"file_name":"DZB_F_1.jpg","height":1386,"width":3031,"id":0}
p = Path(i)
name = p.stem
stat = random.random()
tree = ET.parse(os.path.join(file_annotation, i))
root = tree.getroot()
for size in root.iter('size'):
width = int(size.find('width').text)
height = int(size.find('height').text)
temp_img["file_name"] = i.replace(".xml",".jpg")
temp_img["height"] = height
temp_img["width"] = width
if stat>0.1:
temp_img["id"] = len(train["images"])
train["images"].append(copy.deepcopy(temp_img))
traintxt.append(temp_img["file_name"].replace(".jpg",""))
else:
temp_img["id"] = len(val["images"])
val["images"].append(copy.deepcopy(temp_img))
valtxt.append(temp_img["file_name"].replace(".jpg",""))
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in classes:
classes.append(cls)
temp_cat["id"] = classes.index(cls)
temp_cat["name"] = cls
train["categories"].append(copy.deepcopy(temp_cat))
val["categories"].append(copy.deepcopy(temp_cat))
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
xmin = int(xmlbox.find('xmin').text)
ymin = int(xmlbox.find('ymin').text)
xmax = int(xmlbox.find('xmax').text)
ymax = int(xmlbox.find('ymax').text)
if stat>0.1:
temp_ann["area"] = (ymax - ymin) * (xmax - xmin)
temp_ann["category_id"] = cls_id
temp_ann["image_id"] = temp_img["id"]
temp_ann["id"] = len(train["annotations"])
temp_ann["bbox"] = [xmin, ymin, xmax, ymax]
train["annotations"].append(copy.deepcopy(temp_ann))
else:
temp_ann["area"] = (ymax - ymin) * (xmax - xmin)
temp_ann["category_id"] = cls_id
temp_ann["image_id"] = temp_img["id"]
temp_ann["id"] = len(val["annotations"])
temp_ann["bbox"] = [xmin, ymin, xmax, ymax]
val["annotations"].append(copy.deepcopy(temp_ann))
with open("label.txt", "w") as file:
classes = "\n".join(classes)
file.write(classes)
with open("train.txt", "w") as file:
classes = "\n".join(traintxt)
file.write(classes)
with open("val.txt", "w") as file:
classes = "\n".join(valtxt)
file.write(classes)
import json
with open("train.json", "w") as file:
file.write(json.dumps(train))
with open("val.json", "w") as file:
file.write(json.dumps(val))