18 from typing
import TypeVar, Generic, Optional, List
19 from pathlib
import Path
23 from ...sample
import Sample
24 from ...annotation
import ImageDatasetClass, ImageDatasetClasses
27 SampleType = TypeVar(
"SampleType", bound =
"Sample")
30 class BaseImageDataset(Generic[SampleType]):
32 samples: List[SampleType]
33 classes: ImageDatasetClasses
37 def classesPath(self) -> Path:
41 Path -> path to classes.json file
44 return self.path /
"classes.json"
46 def classByName(self, name: str) -> Optional[ImageDatasetClass]:
47 for clazz
in self.classes:
48 if clazz.label == name:
53 def _writeClassesToFile(self) -> None:
54 self.path.mkdir(exist_ok =
True)
56 with open(self.classesPath,
"w")
as file:
57 json.dump([clazz.encode()
for clazz
in self.classes], file)
59 def saveClasses(self, classes: ImageDatasetClasses) -> bool:
61 Saves provided classes (including their color) to dataset.
62 ImageDataset.classes property will be updated on successful save
66 classes : List[ImageDatasetClass]
71 bool -> True if dataset classes were saved, False if failed to save dataset classes
75 >>> from coretex import ImageDatasetClass
77 >>> labels = {"car", "bicycle", "person", "tree"}
78 >>> imgDatasetClasses = ImageDatasetClass.generate(labels)
79 >>> imageDatasetObj.saveClasses(imgDatasetClasses)
83 self.classes = classes
84 self._writeClassesToFile()