18 from typing
import Optional, List, Set
23 import xml.etree.ElementTree
as ET
25 from .pascal.shared
import getTag, getBoxes, toInt
26 from ..base_converter
import BaseConverter
27 from ...annotation
import CoretexImageAnnotation, CoretexSegmentationInstance, BBox
30 class VOCConverter(BaseConverter):
32 def __init__(self, datasetName: str, projectId: int, datasetPath: str) ->
None:
33 super().__init__(datasetName, projectId, datasetPath)
35 self.__imagesPath = os.path.join(datasetPath,
"images")
37 annotations = os.path.join(datasetPath,
"annotations")
38 self.__fileNames = glob.glob(os.path.join(annotations,
"*.xml"))
40 def _dataSource(self) -> List[str]:
41 return self.__fileNames
43 def _extractLabels(self) -> Set[str]:
44 labels: Set[str] = set()
46 for filename
in self.__fileNames:
47 tree = ET.parse(filename)
49 objects = root.findall(
"object")
52 labelElement = obj.find(
"name")
53 if labelElement
is None:
56 label = labelElement.text
64 def __extractInstance(self, obj: ET.Element) -> Optional[CoretexSegmentationInstance]:
65 label = getTag(obj,
"name")
69 coretexClass = self._dataset.classByName(label)
70 if coretexClass
is None:
71 logging.getLogger(
"coretexpylib").info(f
">> [Coretex] Class: ({label}) is not a part of dataset")
74 bboxElement = obj.find(
'bndbox')
75 if bboxElement
is None:
78 encodedBbox = getBoxes(bboxElement)
79 if encodedBbox
is None:
82 bbox = BBox.decode(encodedBbox)
83 return CoretexSegmentationInstance.create(coretexClass.classIds[0], bbox, [bbox.polygon])
85 def _extractImageAnnotation(self, root: ET.Element) ->
None:
86 fileName = getTag(root,
"filename")
90 size = root.find(
'size')
94 width, height = toInt(size,
"width",
"height")
95 if width
is None or height
is None:
98 coretexAnnotation = CoretexImageAnnotation.create(fileName, width, height, [])
100 for obj
in root.findall(
"object"):
101 instance = self.__extractInstance(obj)
105 coretexAnnotation.instances.append(instance)
107 self._saveImageAnnotationPair(os.path.join(self.__imagesPath, fileName), coretexAnnotation)
109 def _extractSingleAnnotation(self, fileName: str) ->
None:
110 tree = ET.parse(fileName)
111 root = tree.getroot()
113 self._extractImageAnnotation(root)