Coretex
image_sample.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from typing import Any, Dict
19 
20 from .image_sample_data import AnnotatedImageSampleData
21 from .local_image_sample import LocalImageSample
22 from ..network_sample import NetworkSample
23 from ...annotation import CoretexImageAnnotation
24 from ....networking import networkManager, NetworkRequestError
25 
26 
27 class ImageSample(NetworkSample[AnnotatedImageSampleData], LocalImageSample):
28 
29  """
30  Represents the generic image sample\n
31  Contains basic properties and functionality for all image sample classes\n
32  The class has several methods that allow users to access and
33  manipulate image data and annotations, as well as to create new image samples
34  """
35 
36  def __init__(self) -> None:
37  NetworkSample.__init__(self)
38 
39  def saveAnnotation(self, coretexAnnotation: CoretexImageAnnotation) -> bool:
40  # Encrypted sample must be downloaded for annotation to be updated
41  if self.isEncrypted:
42  self.downloaddownloaddownloaddownload(decrypt = True)
43 
44  # Only save annotation locally if it is downloaded
45  if self.zipPathzipPathzipPathzipPath.exists():
46  self.unzipunzipunzip()
47 
48  super().saveAnnotation(coretexAnnotation)
49 
50  if self.isEncrypted:
51  try:
52  self._overwriteSample_overwriteSample(self.zipPathzipPathzipPathzipPath)
53  return True
54  except NetworkRequestError:
55  return False
56  else:
57  parameters = {
58  "id": self.id,
59  "data": coretexAnnotation.encode()
60  }
61 
62  response = networkManager.post("session/save-annotations", parameters)
63  return not response.hasFailed()
64 
65  def saveMetadata(self, metadata: Dict[str, Any]) -> None:
66  # Encrypted sample must be downloaded for metadata to be updated
67  if self.isEncrypted:
68  self.downloaddownloaddownloaddownload(decrypt = True)
69 
70  # Only save metadata locally if it is downloaded
71  if self.zipPathzipPathzipPathzipPath.exists():
72  self.unzipunzipunzip()
73 
74  super().saveMetadata(metadata)
75 
76  if self.isEncrypted:
77  self._overwriteSample_overwriteSample(self.zipPathzipPathzipPathzipPath)
78  else:
79  parameters = {
80  "id": self.id,
81  "data": metadata
82  }
83 
84  response = networkManager.post("session/save-metadata", parameters)
85  if response.hasFailed():
86  raise NetworkRequestError(response, f"Failed to upload metadata for sample \"{self.name}\"")
bool saveAnnotation(self, CoretexImageAnnotation coretexAnnotation)
Definition: image_sample.py:39
None download(self, bool decrypt=True, bool ignoreCache=False)
Definition: local_sample.py:70
None download(self, bool decrypt=True, bool ignoreCache=False)
None download(self, bool decrypt=True, bool ignoreCache=False)
Definition: sample.py:50
None unzip(self, bool ignoreCache=False)
Definition: sample.py:65