When I try to detect an object with tensorflow using yolo, the data is often in xml format and cannot be applied to yolo. Then let's make it ourselves.
import xml.etree.ElementTree as ET
import sys , os
import glob
cate_list = ["Car","Pedestrian","Truck","Signal","Signs","Bicycle","Motorbike","Bus","SVehicle","Train"]
with open("voc_classes.txt","w") as f:
f.write('\n'.join(cate_list))
def class_encord(class_name):
cate_id = {"Car":0,"Pedestrian":1,"Truck":2,"Signal":3,"Signs":4,"Bicycle":5,"Motorbike":6,"Bus":7,"SVehicle":8,"Train":9}
return cate_id[class_name]
def convert(data_file,list_file):
in_file = open(data_file)
tree = ET.parse(in_file)
root = tree.getroot()
for obj in root.iter("item"):
cate = obj.find("category").text
cate = cate.lstrip("\n").lstrip(" ")
cate = cate.rstrip(" ").rstrip("\n")
category_id = class_encord(cate)
xmlbox = obj.find("box2d")
data = [int(float(xmlbox.find("x1").text)),int(float(xmlbox.find("x2").text)),int(float(xmlbox.find("y1").text)),int(float(xmlbox.find("y2").text))]
list_file.write(" " + ",".join([str(a) for a in data]) + "," + str(category_id))
data_file_list = glob.glob("Annotations/*.xml")
list_file = open("2007_train.txt","w")
for data_file in data_file_list:
jpg_file = "train_" + data_file.rstrip(".xml") + ".jpg "
list_file.write(jpg_file)
convert(data_file,list_file)
list_file.write("\n")
list_file.close()
It's not generalized, so it's very hard to see (; ^ ω ^) I will list the parameters that can be used by changing it
yolo uses the txt that describes the object, so output it.
cate_list = ["Car","Pedestrian","Truck","Signal","Signs","Bicycle","Motorbike","Bus","SVehicle","Train"]
with open("voc_classes.txt","w") as f:
f.write('\n'.join(cate_list))
A function for encoding the object name and id.
def class_encord(class_name):
cate_id = {"Car":0,"Pedestrian":1,"Truck":2,"Signal":3,"Signs":4,"Bicycle":5,"Motorbike":6,"Bus":7,"SVehicle":8,"Train":9}
return cate_id[class_name]
A function that converts xml data to a txt file. What you are doing is as simple as reading with tml.etree.ElementTree, fetching and writing each data.
def convert(data_file,list_file):
in_file = open(data_file)
tree = ET.parse(in_file)
root = tree.getroot()
for obj in root.iter("item"):
cate = obj.find("category").text
cate = cate.lstrip("\n").lstrip(" ")
cate = cate.rstrip(" ").rstrip("\n")
category_id = class_encord(cate)
xmlbox = obj.find("box2d")
data = [int(float(xmlbox.find("x1").text)),int(float(xmlbox.find("x2").text)),int(float(xmlbox.find("y1").text)),int(float(xmlbox.find("y2").text))]
list_file.write(" " + ",".join([str(a) for a in data]) + "," + str(category_id))
All that's left is to do it.
data_file_list = glob.glob("Annotations/*.xml")
list_file = open("2007_train.txt","w")
for data_file in data_file_list:
jpg_file = "train_" + data_file.rstrip(".xml") + ".jpg "
list_file.write(jpg_file)
convert(data_file,list_file)
list_file.write("\n")
list_file.close()
Object detection is interesting (^ ▽ ^) Data organization is difficult.
Recommended Posts