From 8e3475465a261c6caac799024bcda61ad288aaa0 Mon Sep 17 00:00:00 2001 From: Harrison Cole Date: Sat, 8 May 2021 15:01:14 +1000 Subject: [PATCH 1/5] Prevent index out of bounds issue --- xml_to_csv.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/xml_to_csv.py b/xml_to_csv.py index 03692c4cfa0..9b6d33bccae 100644 --- a/xml_to_csv.py +++ b/xml_to_csv.py @@ -33,15 +33,16 @@ def xml_to_csv(path): root = tree.getroot() for member in root.findall("object"): classes_names.append(member[0].text) + box = member.find("bndbox") 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), + int(box[0].text), + int(box[1].text), + int(box[2].text), + int(box[3].text), ) xml_list.append(value) column_name = [ From ef515d1fe361a612e188df8d29867a3c901acfff Mon Sep 17 00:00:00 2001 From: Harrison Cole Date: Sat, 8 May 2021 18:12:33 +1000 Subject: [PATCH 2/5] Scale coordinates for compatibility with TF 1.* --- xml_to_csv.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/xml_to_csv.py b/xml_to_csv.py index 9b6d33bccae..e21d6c3b15a 100644 --- a/xml_to_csv.py +++ b/xml_to_csv.py @@ -32,17 +32,28 @@ def xml_to_csv(path): tree = ET.parse(xml_file) root = tree.getroot() for member in root.findall("object"): - classes_names.append(member[0].text) + classname = member.find("name").text + classes_names.append(classname) + + size = member.find("size") + width = int(size.find("width").text) + height = int(size.find("height").text) + box = member.find("bndbox") + xmin = int(box.find("xmin").text) / width + xmax = int(box.find("xmax").text) / width + ymin = int(box.find("ymin").text) / height + xmax = int(box.find("xmax").text) / height + value = ( root.find("filename").text, - int(root.find("size")[0].text), - int(root.find("size")[1].text), - member[0].text, - int(box[0].text), - int(box[1].text), - int(box[2].text), - int(box[3].text), + width, + height, + classname, + xmin, + xmax, + ymin, + ymax, ) xml_list.append(value) column_name = [ From c6cf4a1a6c4597d99047e5c2b2a01383f32462b4 Mon Sep 17 00:00:00 2001 From: Harrison Cole Date: Sat, 8 May 2021 18:17:30 +1000 Subject: [PATCH 3/5] Update xml_to_csv.py --- xml_to_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml_to_csv.py b/xml_to_csv.py index e21d6c3b15a..919b5dcd3be 100644 --- a/xml_to_csv.py +++ b/xml_to_csv.py @@ -35,7 +35,7 @@ def xml_to_csv(path): classname = member.find("name").text classes_names.append(classname) - size = member.find("size") + size = root.find("size") width = int(size.find("width").text) height = int(size.find("height").text) From fc5f1df4540a8ff2c0c53dc374e70340c7d2cb56 Mon Sep 17 00:00:00 2001 From: Harrison Cole Date: Sat, 8 May 2021 18:24:39 +1000 Subject: [PATCH 4/5] Update xml_to_csv.py --- xml_to_csv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xml_to_csv.py b/xml_to_csv.py index 919b5dcd3be..77098e399e5 100644 --- a/xml_to_csv.py +++ b/xml_to_csv.py @@ -43,7 +43,7 @@ def xml_to_csv(path): xmin = int(box.find("xmin").text) / width xmax = int(box.find("xmax").text) / width ymin = int(box.find("ymin").text) / height - xmax = int(box.find("xmax").text) / height + ymax = int(box.find("ymax").text) / height value = ( root.find("filename").text, @@ -51,8 +51,8 @@ def xml_to_csv(path): height, classname, xmin, - xmax, ymin, + xmax, ymax, ) xml_list.append(value) From af868da80a926a9b3c8f55255c82ba10fcf16781 Mon Sep 17 00:00:00 2001 From: Harrison Cole Date: Sat, 15 May 2021 22:18:37 +1000 Subject: [PATCH 5/5] Fix coordinate bounds --- xml_to_csv.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/xml_to_csv.py b/xml_to_csv.py index 77098e399e5..a4c24e34d83 100644 --- a/xml_to_csv.py +++ b/xml_to_csv.py @@ -14,6 +14,11 @@ import xml.etree.ElementTree as ET +def confine(value, lower, upper): + return max(min(upper, value), lower) + + + 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. @@ -40,20 +45,20 @@ def xml_to_csv(path): height = int(size.find("height").text) box = member.find("bndbox") - xmin = int(box.find("xmin").text) / width - xmax = int(box.find("xmax").text) / width - ymin = int(box.find("ymin").text) / height - ymax = int(box.find("ymax").text) / height + xmin = int(box.find("xmin").text) + xmax = int(box.find("xmax").text) + ymin = int(box.find("ymin").text) + ymax = int(box.find("ymax").text) value = ( root.find("filename").text, width, height, classname, - xmin, - ymin, - xmax, - ymax, + confine(xmin, 0, width), + confine(ymin, 0, width), + confine(xmax, 0, height), + confine(ymax, 0, height) ) xml_list.append(value) column_name = [