18 from typing
import Dict, Any
19 from pathlib
import Path
23 from .image_sample_data
import AnnotatedImageSampleData
24 from .image_format
import ImageFormat
25 from ..local_sample
import LocalSample
26 from ...annotation
import CoretexImageAnnotation
32 Represents the local Image Sample object\n
33 Contains basic properties and functionality for local image Sample classes\n
34 The class has several methods that allow users to access and
35 manipulate local image data and annotations
39 def imagePath(self) -> Path:
40 for format
in ImageFormat:
41 imagePaths = list(self.
pathpathpath.glob(f
"*.{format.extension}"))
42 imagePaths = [path
for path
in imagePaths
if not "thumbnail" in str(path)]
44 if len(imagePaths) > 0:
45 return Path(imagePaths[0])
47 raise FileNotFoundError
50 def annotationPath(self) -> Path:
51 return self.
pathpathpath /
"annotations.json"
54 def metadataPath(self) -> Path:
55 return self.
pathpathpath /
"metadata.json"
57 def load(self) -> AnnotatedImageSampleData:
59 Loads image and its annotation if it exists
63 AnnotatedImageSampleData -> image data and annotation in Coretex.ai format
70 Loads sample metadata into a dictionary
74 dict[str, Any] -> the metadata as a dict object
78 FileNotFoundError -> if metadata file is missing\n
79 ValueError -> if json in the metadata file is list
83 raise FileNotFoundError(f
"Metadata file \"{self.metadataPath}\" not found")
85 with self.
metadataPathmetadataPath.open(
"r", encoding =
"utf-8")
as metadataFile:
86 metadata = json.load(metadataFile)
87 if not isinstance(metadata, dict):
88 raise ValueError(f
"Metatada for sample \"{self.name}\" is a list. Expected dictionary")
94 Updates annotation for the image
98 bool -> returns True if successful, False otherwise
102 json.dump(coretexAnnotation.encode(), file)
109 Saves a json object as metadata for the sample
113 metadata : dict[str, Any]
114 Json object containing sample metadata
118 json.dump(metadata, file)
Path annotationPath(self)
bool saveAnnotation(self, CoretexImageAnnotation coretexAnnotation)
AnnotatedImageSampleData load(self)
Dict[str, Any] loadMetadata(self)
None saveMetadata(self, Dict[str, Any] metadata)
None _updateArchive(self)