18 from typing
import Final, Optional
19 from pathlib
import Path
23 from PIL
import Image, ImageOps
24 from PIL.Image
import Image
as PILImage
28 from .image_format
import ImageFormat
29 from ...annotation
import CoretexImageAnnotation, ImageDatasetClasses
32 def _findImage(path: Path) -> Path:
33 for format
in ImageFormat:
34 imagePaths = list(path.glob(f
"*.{format.extension}"))
35 imagePaths = [path
for path
in imagePaths
if not "thumb" in str(path)]
37 if len(imagePaths) > 0:
38 return Path(imagePaths[0])
43 def _readImageData(path: Path) -> np.ndarray:
44 image = ImageOps.exif_transpose(Image.open(path))
45 if not isinstance(image, PILImage):
46 raise TypeError(f
"Expected \"PIL.Image.Image\" recieved \"{type(image)}\"")
48 if image.mode !=
"RGB":
49 image = image.convert(
"RGB")
51 imageData = np.frombuffer(image.tobytes(), dtype = np.uint8)
52 imageData = imageData.reshape((image.size[1], image.size[0], 3))
57 def _readAnnotationData(path: Path) -> CoretexImageAnnotation:
58 with open(path,
"r")
as annotationsFile:
59 return CoretexImageAnnotation.decode(
60 json.load(annotationsFile)
67 Contains image data as well as its annotation\n
68 Annotation is expected to be in Coretex.ai format
71 def __init__(self, path: Path) ->
None:
72 self.image: Final = _readImageData(_findImage(path))
73 self.
annotationannotation: Optional[CoretexImageAnnotation] =
None
75 annotationPath = path /
"annotations.json"
76 if annotationPath.exists():
77 self.
annotationannotation = _readAnnotationData(annotationPath)
81 Generates segmentation mask for the provided classes
85 classes : ImageDatasetClasses
86 list of dataset classes
90 np.ndarray -> segmentation mask represented as np.ndarray
np.ndarray extractSegmentationMask(self, ImageDatasetClasses classes)