lazylabel-gui 1.1.7__py3-none-any.whl → 1.1.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,12 +17,21 @@ class FileManager:
17
17
  self.segment_manager = segment_manager
18
18
 
19
19
  def save_npz(
20
- self, image_path: str, image_size: tuple[int, int], class_order: list[int]
20
+ self,
21
+ image_path: str,
22
+ image_size: tuple[int, int],
23
+ class_order: list[int],
24
+ crop_coords: tuple[int, int, int, int] | None = None,
21
25
  ) -> str:
22
26
  """Save segments as NPZ file."""
23
27
  final_mask_tensor = self.segment_manager.create_final_mask_tensor(
24
28
  image_size, class_order
25
29
  )
30
+
31
+ # Apply crop if coordinates are provided
32
+ if crop_coords:
33
+ final_mask_tensor = self._apply_crop_to_mask(final_mask_tensor, crop_coords)
34
+
26
35
  npz_path = os.path.splitext(image_path)[0] + ".npz"
27
36
  np.savez_compressed(npz_path, mask=final_mask_tensor.astype(np.uint8))
28
37
  return npz_path
@@ -33,11 +42,16 @@ class FileManager:
33
42
  image_size: tuple[int, int],
34
43
  class_order: list[int],
35
44
  class_labels: list[str],
45
+ crop_coords: tuple[int, int, int, int] | None = None,
36
46
  ) -> str | None:
37
47
  """Save segments as YOLO format TXT file."""
38
48
  final_mask_tensor = self.segment_manager.create_final_mask_tensor(
39
49
  image_size, class_order
40
50
  )
51
+
52
+ # Apply crop if coordinates are provided
53
+ if crop_coords:
54
+ final_mask_tensor = self._apply_crop_to_mask(final_mask_tensor, crop_coords)
41
55
  output_path = os.path.splitext(image_path)[0] + ".txt"
42
56
  h, w = image_size
43
57
 
@@ -118,6 +132,35 @@ class FileManager:
118
132
  }
119
133
  )
120
134
 
135
+ def _apply_crop_to_mask(
136
+ self, mask_tensor: np.ndarray, crop_coords: tuple[int, int, int, int]
137
+ ) -> np.ndarray:
138
+ """Apply crop to mask tensor by setting areas outside crop to 0."""
139
+ x1, y1, x2, y2 = crop_coords
140
+ h, w = mask_tensor.shape[:2]
141
+
142
+ # Create a copy of the mask tensor
143
+ cropped_mask = mask_tensor.copy()
144
+
145
+ # Set areas outside crop to 0
146
+ # Top area (0, 0, w, y1)
147
+ if y1 > 0:
148
+ cropped_mask[:y1, :, :] = 0
149
+
150
+ # Bottom area (0, y2, w, h)
151
+ if y2 < h:
152
+ cropped_mask[y2:, :, :] = 0
153
+
154
+ # Left area (0, y1, x1, y2)
155
+ if x1 > 0:
156
+ cropped_mask[y1:y2, :x1, :] = 0
157
+
158
+ # Right area (x2, y1, w, y2)
159
+ if x2 < w:
160
+ cropped_mask[y1:y2, x2:, :] = 0
161
+
162
+ return cropped_mask
163
+
121
164
  def is_image_file(self, filepath: str) -> bool:
122
165
  """Check if file is a supported image format."""
123
166
  return filepath.lower().endswith((".png", ".jpg", ".jpeg", ".tiff", ".tif"))
@@ -94,3 +94,15 @@ class ModelManager:
94
94
  return self.sam_model is not None and getattr(
95
95
  self.sam_model, "is_loaded", False
96
96
  )
97
+
98
+ def set_image_from_path(self, image_path: str) -> bool:
99
+ """Set image for SAM model from file path."""
100
+ if not self.is_model_available():
101
+ return False
102
+ return self.sam_model.set_image_from_path(image_path)
103
+
104
+ def set_image_from_array(self, image_array) -> bool:
105
+ """Set image for SAM model from numpy array."""
106
+ if not self.is_model_available():
107
+ return False
108
+ return self.sam_model.set_image_from_array(image_array)