Wenn ich versuche, ein Objekt mit Tensorflow mithilfe von Yolo zu erkennen, liegen die Daten häufig im XML-Format vor und können nicht auf Yolo angewendet werden. Dann machen wir es uns selbst.
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()
Es ist nicht verallgemeinert, daher ist es sehr schwer zu sehen (; ^ ω ^) Ich werde die Parameter auflisten, die durch Ändern verwendet werden können
yolo verwendet txt für das Objekt, also geben Sie es aus.
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))
Eine Funktion zum Codieren des Objektnamens und der 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]
Eine Funktion, die XML-Daten in eine txt-Datei konvertiert. Was Sie tun, ist so einfach wie das Lesen mit tml.etree.ElementTree, das Abrufen und Schreiben aller Daten.
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))
Alles was übrig bleibt ist es zu tun.
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()
Objekterkennung ist interessant (^ ▽ ^) Datenorganisation ist schwierig.
Recommended Posts