18 from typing
import List, Set
22 import xml.etree.ElementTree
as ET
24 from .shared
import getTag, toInt
25 from .instance_extractor
import InstanceExtractor
26 from ...base_converter
import BaseConverter
27 from .....entities
import CoretexImageAnnotation
33 Represents the Converter from Pascal VOC 2012 Format to Cortex Format
36 def __init__(self, datasetName: str, projectId: int, datasetPath: str) ->
None:
37 super().__init__(datasetName, projectId, datasetPath)
39 self.
__imagesPath__imagesPath = os.path.join(datasetPath,
"JPEGImages")
40 self.
__segmentationPath__segmentationPath = os.path.join(datasetPath,
"SegmentationObject")
42 annotations = os.path.join(datasetPath,
"Annotations")
43 self.
__fileNames__fileNames = glob.glob(os.path.join(annotations,
"*.xml"))
45 def _dataSource(self) -> List[str]:
48 def _extractLabels(self) -> Set[str]:
49 labels: Set[str] = set()
52 tree = ET.parse(filename)
54 objects = root.findall(
"object")
57 labelElement = obj.find(
"name")
58 if labelElement
is None:
61 label = labelElement.text
69 def __extractImageAnnotation(self, root: ET.Element) ->
None:
70 fileName = getTag(root,
"filename")
74 baseFileName = os.path.splitext(fileName)[0]
75 filenamePNG = f
"{baseFileName}.png"
77 if not os.path.exists(os.path.join(self.
__imagesPath__imagesPath, fileName)):
80 instanceExtractor = InstanceExtractor(self._dataset)
81 instances = instanceExtractor.extractInstances(root, filenamePNG, self.
__segmentationPath__segmentationPath)
83 size = root.find(
'size')
87 width, height = toInt(size,
"width",
"height")
88 if width
is None or height
is None:
91 coretexAnnotation = CoretexImageAnnotation.create(fileName, width, height, instances)
94 def _extractSingleAnnotation(self, fileName: str) ->
None:
95 tree = ET.parse(fileName)
None _saveImageAnnotationPair(self, str imagePath, CoretexImageAnnotation annotation)
None __extractImageAnnotation(self, ET.Element root)