Skip to content

Commit 8637338

Browse files
author
wz
committed
skip annotation files which no have objects
1 parent 3cd608f commit 8637338

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

pytorch_object_detection/faster_rcnn/my_dataset.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,28 @@ def __init__(self, voc_root, year="2012", transforms=None, txt_name: str = "trai
2525
assert os.path.exists(txt_path), "not found {} file.".format(txt_name)
2626

2727
with open(txt_path) as read:
28-
self.xml_list = [os.path.join(self.annotations_root, line.strip() + ".xml")
29-
for line in read.readlines() if len(line.strip()) > 0]
28+
xml_list = [os.path.join(self.annotations_root, line.strip() + ".xml")
29+
for line in read.readlines() if len(line.strip()) > 0]
3030

31+
self.xml_list = []
3132
# check file
33+
for xml_path in xml_list:
34+
if os.path.exists(xml_path) is False:
35+
print(f"Warning: not found '{xml_path}', skip this annotation file.")
36+
continue
37+
38+
# check for targets
39+
with open(xml_path) as fid:
40+
xml_str = fid.read()
41+
xml = etree.fromstring(xml_str)
42+
data = self.parse_xml_to_dict(xml)["annotation"]
43+
if "object" not in data:
44+
print(f"INFO: no objects in {xml_path}, skip this annotation file.")
45+
continue
46+
47+
self.xml_list.append(xml_path)
48+
3249
assert len(self.xml_list) > 0, "in '{}' file does not find any information.".format(txt_path)
33-
for xml_path in self.xml_list:
34-
assert os.path.exists(xml_path), "not found '{}' file.".format(xml_path)
3550

3651
# read class_indict
3752
json_file = './pascal_voc_classes.json'

0 commit comments

Comments
 (0)