Open AbdullahHoor opened 4 years ago
""" Usage:
python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/train -o [PATH_TO_ANNOTATIONS_FOLDER]/train_labels.csv
python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/test -o [PATH_TO_ANNOTATIONS_FOLDER]/test_labels.csv """
import os import glob import pandas as pd import argparse import xml.etree.ElementTree as ET
def xml_to_csv(path): """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame.
Parameters:
----------
path : {str}
The path containing the .xml files
Returns
-------
Pandas DataFrame
The produced dataframe
"""
classes_names = []
xml_list = []
for xml_file in glob.glob(path + "/*.xml"):
tree = ET.parse(xml_file)
root = tree.getroot()
for member in root.findall("object"):
classes_names.append(member[0].text)
value = (
root.find("filename").text,
int(root.find("size")[0].text),
int(root.find("size")[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text),
)
xml_list.append(value)
column_name = [
"filename",
"width",
"height",
"class",
"xmin",
"ymin",
"xmax",
"ymax",
]
xml_df = pd.DataFrame(xml_list, columns=column_name)
classes_names = list(set(classes_names))
classes_names.sort()
return xml_df, classes_names
def main():
parser = argparse.ArgumentParser(
description="Sample TensorFlow XML-to-CSV converter"
)
parser.add_argument(
"-i",
"--inputDir",
help="Path to the folder where the input .xml files are stored",
type=str,
default="E:/University/FYP/Datasets/Car_and_Number_plate_labeling/Test_cars",
)
parser.add_argument(
"-o", "--outputFile",
help="Name of output .csv file (including path)",
type=str,
default="E:/University/FYP/All_Codes/Object_Detection_and_Count/My_model/new/newtest.csv",
)
parser.add_argument(
"-l",
"--labelMapDir",
help="Directory path to save label_map.pbtxt file is specified.",
type=str,
default="E:/University/FYP/All_Codes/Object_Detection_and_Count/My_model/label",
)
args = parser.parse_args()
if args.inputDir is None:
args.inputDir = os.getcwd()
if args.outputFile is None:
args.outputFile = args.inputDir + "/labels.csv"
assert os.path.isdir(args.inputDir)
os.makedirs(os.path.dirname(args.outputFile), exist_ok=True)
xml_df, classes_names = xml_to_csv(args.inputDir)
xml_df.to_csv(args.outputFile, index=None)
print("Successfully converted xml to csv.")
if args.labelMapDir:
os.makedirs(args.labelMapDir, exist_ok=True)
label_map_path = os.path.join(args.labelMapDir, "label_map.pbtxt")
print("Generate `{}`".format(label_map_path))
# Create the `label_map.pbtxt` file
pbtxt_content = ""
for i, class_name in enumerate(classes_names):
pbtxt_content = (
pbtxt_content
+ "item {{\n id: {0}\n name: '{1}'\n}}\n\n".format(
i + 1, class_name
)
)
pbtxt_content = pbtxt_content.strip()
with open(label_map_path, "w") as f:
f.write(pbtxt_content)
if name == "main": main()
Ye dekho blue mei wo file jo run krni haiRed mei us kay arguments Jon si files us code ko chaiye as inputjtny input arguments lene hain utnay parser_add_argument kay function banty Us kay baad -i likha wa hai yahan -kuch bhi likh saktiLikn power shell me input file ka path likhny se phele wo likhna zarori hyjesay is mei -i likha wa hai Or agy input file ka path
Or is mei jo -i kay neechy likha hai --inputDirYe is liye likha hai kay tum jo run krty waqt jo bhi path do gi wo is (inputDir) naam kay variable mei store ho jaye gaOr us ko access jab b karna hoga tu tum(args inputDir) kar kay access kr sakti ho