DariusGiannoli commited on
Commit
1170eb8
Β·
1 Parent(s): ee5ef05

ROI: validate end>start (error instead of crash) + pixel ruler overlay

Browse files
tabs/generalisation/data_lab.py CHANGED
@@ -135,6 +135,7 @@ def render():
135
  st.write("Define bounding boxes on the **TRAIN image**.")
136
 
137
  H, W = img_train.shape[:2]
 
138
 
139
  if "gen_rois" not in st.session_state:
140
  st.session_state["gen_rois"] = [
@@ -172,18 +173,40 @@ def render():
172
  step=1, key=f"gen_roi_x0_{i}"))
173
  roi["y0"] = int(cr2.number_input("Y start", 0, H-2, int(roi["y0"]),
174
  step=1, key=f"gen_roi_y0_{i}"))
175
- roi["x1"] = max(roi["x0"] + 1, int(cr3.number_input("X end", 1, W,
176
  min(W, int(roi["x1"])),
177
- step=1, key=f"gen_roi_x1_{i}")))
178
- roi["y1"] = max(roi["y0"] + 1, int(cr4.number_input("Y end", 1, H,
179
  min(H, int(roi["y1"])),
180
- step=1, key=f"gen_roi_y1_{i}")))
 
 
 
 
181
 
182
  st.button("βž• Add Another ROI", on_click=_add_roi,
183
  disabled=len(st.session_state["gen_rois"]) >= 20,
184
  key="gen_add_roi")
185
 
 
 
 
 
 
 
 
 
186
  overlay = img_train.copy()
 
 
 
 
 
 
 
 
 
 
187
  crops = []
188
  for i, roi in enumerate(st.session_state["gen_rois"]):
189
  color = ROI_COLORS[i % len(ROI_COLORS)]
 
135
  st.write("Define bounding boxes on the **TRAIN image**.")
136
 
137
  H, W = img_train.shape[:2]
138
+ st.caption(f"πŸ“ Image size: **{W} Γ— {H}** px (X: 0 β†’ {W-1}, Y: 0 β†’ {H-1})")
139
 
140
  if "gen_rois" not in st.session_state:
141
  st.session_state["gen_rois"] = [
 
173
  step=1, key=f"gen_roi_x0_{i}"))
174
  roi["y0"] = int(cr2.number_input("Y start", 0, H-2, int(roi["y0"]),
175
  step=1, key=f"gen_roi_y0_{i}"))
176
+ roi["x1"] = int(cr3.number_input("X end", 0, W,
177
  min(W, int(roi["x1"])),
178
+ step=1, key=f"gen_roi_x1_{i}"))
179
+ roi["y1"] = int(cr4.number_input("Y end", 0, H,
180
  min(H, int(roi["y1"])),
181
+ step=1, key=f"gen_roi_y1_{i}"))
182
+ if roi["x1"] <= roi["x0"] or roi["y1"] <= roi["y0"]:
183
+ st.error(f"ROI {i+1}: end must be greater than start "
184
+ f"(X: {roi['x0']}β†’{roi['x1']}, Y: {roi['y0']}β†’{roi['y1']}). "
185
+ f"Adjust the values above.")
186
 
187
  st.button("βž• Add Another ROI", on_click=_add_roi,
188
  disabled=len(st.session_state["gen_rois"]) >= 20,
189
  key="gen_add_roi")
190
 
191
+ # Validate all ROIs before drawing
192
+ roi_valid = all(r["x1"] > r["x0"] and r["y1"] > r["y0"]
193
+ for r in st.session_state["gen_rois"])
194
+
195
+ if not roi_valid:
196
+ st.warning("⚠️ Fix the invalid ROI coordinates above before proceeding.")
197
+ st.stop()
198
+
199
  overlay = img_train.copy()
200
+ # Draw pixel ruler (tick marks every 100 px)
201
+ for px in range(0, W, 100):
202
+ cv2.line(overlay, (px, 0), (px, 12), (200, 200, 200), 1)
203
+ cv2.putText(overlay, str(px), (px + 2, 11),
204
+ cv2.FONT_HERSHEY_SIMPLEX, 0.3, (200, 200, 200), 1)
205
+ for py in range(0, H, 100):
206
+ cv2.line(overlay, (0, py), (12, py), (200, 200, 200), 1)
207
+ cv2.putText(overlay, str(py), (1, py + 12),
208
+ cv2.FONT_HERSHEY_SIMPLEX, 0.3, (200, 200, 200), 1)
209
+
210
  crops = []
211
  for i, roi in enumerate(st.session_state["gen_rois"]):
212
  color = ROI_COLORS[i % len(ROI_COLORS)]
tabs/stereo/data_lab.py CHANGED
@@ -174,6 +174,7 @@ def render():
174
  st.write("Define bounding boxes on the **TRAIN (left) image** β€” each becomes a separate class.")
175
 
176
  H, W = img_l.shape[:2]
 
177
 
178
  if "stereo_rois" not in st.session_state:
179
  st.session_state["stereo_rois"] = [
@@ -211,19 +212,40 @@ def render():
211
  step=1, key=f"stereo_roi_x0_{i}"))
212
  roi["y0"] = int(cr2.number_input("Y start", 0, H-2, int(roi["y0"]),
213
  step=1, key=f"stereo_roi_y0_{i}"))
214
- roi["x1"] = max(roi["x0"] + 1, int(cr3.number_input("X end", 1, W,
215
  min(W, int(roi["x1"])),
216
- step=1, key=f"stereo_roi_x1_{i}")))
217
- roi["y1"] = max(roi["y0"] + 1, int(cr4.number_input("Y end", 1, H,
218
  min(H, int(roi["y1"])),
219
- step=1, key=f"stereo_roi_y1_{i}")))
 
 
 
 
220
 
221
  st.button("βž• Add Another ROI", on_click=_add_roi,
222
  disabled=len(st.session_state["stereo_rois"]) >= 20,
223
  key="stereo_add_roi")
224
 
225
- # Draw ROIs
 
 
 
 
 
 
 
 
226
  overlay = img_l.copy()
 
 
 
 
 
 
 
 
 
227
  crops = []
228
  for i, roi in enumerate(st.session_state["stereo_rois"]):
229
  color = ROI_COLORS[i % len(ROI_COLORS)]
 
174
  st.write("Define bounding boxes on the **TRAIN (left) image** β€” each becomes a separate class.")
175
 
176
  H, W = img_l.shape[:2]
177
+ st.caption(f"πŸ“ Image size: **{W} Γ— {H}** px (X: 0 β†’ {W-1}, Y: 0 β†’ {H-1})")
178
 
179
  if "stereo_rois" not in st.session_state:
180
  st.session_state["stereo_rois"] = [
 
212
  step=1, key=f"stereo_roi_x0_{i}"))
213
  roi["y0"] = int(cr2.number_input("Y start", 0, H-2, int(roi["y0"]),
214
  step=1, key=f"stereo_roi_y0_{i}"))
215
+ roi["x1"] = int(cr3.number_input("X end", 0, W,
216
  min(W, int(roi["x1"])),
217
+ step=1, key=f"stereo_roi_x1_{i}"))
218
+ roi["y1"] = int(cr4.number_input("Y end", 0, H,
219
  min(H, int(roi["y1"])),
220
+ step=1, key=f"stereo_roi_y1_{i}"))
221
+ if roi["x1"] <= roi["x0"] or roi["y1"] <= roi["y0"]:
222
+ st.error(f"ROI {i+1}: end must be greater than start "
223
+ f"(X: {roi['x0']}β†’{roi['x1']}, Y: {roi['y0']}β†’{roi['y1']}). "
224
+ f"Adjust the values above.")
225
 
226
  st.button("βž• Add Another ROI", on_click=_add_roi,
227
  disabled=len(st.session_state["stereo_rois"]) >= 20,
228
  key="stereo_add_roi")
229
 
230
+ # Validate all ROIs before drawing
231
+ roi_valid = all(r["x1"] > r["x0"] and r["y1"] > r["y0"]
232
+ for r in st.session_state["stereo_rois"])
233
+
234
+ if not roi_valid:
235
+ st.warning("⚠️ Fix the invalid ROI coordinates above before proceeding.")
236
+ st.stop()
237
+
238
+ # Draw ROIs + pixel ruler
239
  overlay = img_l.copy()
240
+ for px in range(0, W, 100):
241
+ cv2.line(overlay, (px, 0), (px, 12), (200, 200, 200), 1)
242
+ cv2.putText(overlay, str(px), (px + 2, 11),
243
+ cv2.FONT_HERSHEY_SIMPLEX, 0.3, (200, 200, 200), 1)
244
+ for py in range(0, H, 100):
245
+ cv2.line(overlay, (0, py), (12, py), (200, 200, 200), 1)
246
+ cv2.putText(overlay, str(py), (1, py + 12),
247
+ cv2.FONT_HERSHEY_SIMPLEX, 0.3, (200, 200, 200), 1)
248
+
249
  crops = []
250
  for i, roi in enumerate(st.session_state["stereo_rois"]):
251
  color = ROI_COLORS[i % len(ROI_COLORS)]