18 from typing
import Any, Optional, List, Set, Dict
19 from pathlib
import Path
28 from ..base_converter
import BaseConverter
29 from ...annotation
import CoretexImageAnnotation, CoretexSegmentationInstance, BBox
32 class LabelMeConverter(BaseConverter):
34 def __init__(self, datasetName: str, projectId: int, datasetPath: str) ->
None:
35 super().__init__(datasetName, projectId, datasetPath)
37 self.__imagesPath = os.path.join(datasetPath,
"images")
39 annotations = os.path.join(datasetPath,
"annotations")
40 self.__fileNames = glob.glob(os.path.join(annotations,
"*.json"))
42 def _dataSource(self) -> List[str]:
43 return self.__fileNames
45 def _extractLabels(self) -> Set[str]:
46 labels: Set[str] = set()
48 for fileName
in self.__fileNames:
49 with open(fileName)
as jsonFile:
50 data = json.load(jsonFile)
52 for shape
in data[
"shapes"]:
53 labels.add(shape[
"label"])
57 def __extractInstance(self, shape: Dict[str, Any]) -> Optional[CoretexSegmentationInstance]:
58 label = shape[
"label"]
60 coretexClass = self._dataset.classByName(label)
61 if coretexClass
is None:
62 logging.getLogger(
"coretexpylib").info(f
">> [Coretex] Class: ({label}) is not a part of dataset")
65 points: List[int] = np.array(shape[
"points"]).flatten().tolist()
66 bbox = BBox.fromPoly(points)
68 return CoretexSegmentationInstance.create(coretexClass.classIds[0], bbox, [points])
70 def __extractImageAnnotation(self, imageAnnotation: Dict[str, Any]) ->
None:
71 imageName = Path(imageAnnotation[
"imagePath"]).stem
72 imageName = f
"{imageName}.jpg"
74 width = imageAnnotation[
"imageWidth"]
75 height = imageAnnotation[
"imageHeight"]
77 coretexAnnotation = CoretexImageAnnotation.create(imageName, width, height, [])
79 for shape
in imageAnnotation[
"shapes"]:
80 instance = self.__extractInstance(shape)
84 coretexAnnotation.instances.append(instance)
86 self._saveImageAnnotationPair(os.path.join(self.__imagesPath, imageName), coretexAnnotation)
88 def _extractSingleAnnotation(self, fileName: str) ->
None:
89 with open(fileName)
as jsonFile:
90 data = json.load(jsonFile)
91 self.__extractImageAnnotation(data)