def parse_xml(self, filename: str) -> Annotation: name = os.path.splitext(filename)[0] annotation = Annotation(name) annotation.init_size() with open(filename, 'r') as xml_file: tree = ET.parse(xml_file) root = tree.getroot() labels = list() for obj in root.iter('object'): difficult = obj.find('difficult').text cls = obj.find('name').text if cls in self.classes.keys() and int(difficult) == 0: cls_id = str(annotation.classes.get(cls)) xml_box = obj.find('bndbox') bbox = (float(xml_box.find('xmin').text), float(xml_box.find('xmax').text), float(xml_box.find('ymin').text), float(xml_box.find('ymax').text)) label = (cls_id, bbox) print(label) labels.append(label) annotation.labels = labels return annotation
def parse_txt(self, filename: str) -> Annotation: name = os.path.splitext(filename)[0] annotation = Annotation(name) annotation.init_size() labels = [] with open(filename, "r") as txt_file: lines = txt_file.readlines() for line in lines: line = line.strip() words = line.split() class_id = words[0] w, h = annotation.size bbox_width = float(words[3]) * w bbox_height = float(words[4]) * h center_x = float(words[1]) * w center_y = float(words[2]) * h bbox = [] bbox.append(center_x - (bbox_width / 2)) bbox.append(center_y - (bbox_height / 2)) bbox.append(center_x + (bbox_width / 2)) bbox.append(center_y + (bbox_height / 2)) labels.append((class_id, bbox)) annotation.labels = labels return annotation