Spaces:
Running on Zero
Running on Zero
Upload 3 files
Browse files
app.py
CHANGED
|
@@ -194,6 +194,11 @@ def refresh_editor(image_id):
|
|
| 194 |
return annotation_canvas_html(image_id),f"**{item['filename']}** — {item['width']} × {item['height']} px",item.get("annotations",[])
|
| 195 |
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
def add_annotation(image_id, cls, x, y, w, h):
|
| 198 |
if not image_id:return annotation_canvas_html(None),"Select an image first.",[]
|
| 199 |
if not cls:return annotation_canvas_html(image_id),"Select a class first.",[]
|
|
@@ -236,62 +241,6 @@ def save_classes(text):
|
|
| 236 |
return f"Saved {len(classes)} classes.", gr.update(choices=classes, value=classes[0]), dataset_status()
|
| 237 |
|
| 238 |
|
| 239 |
-
def add_annotation(image_id, cls, x, y, w, h):
|
| 240 |
-
if not image_id:
|
| 241 |
-
return None, "Select an image first.", [], []
|
| 242 |
-
if not cls:
|
| 243 |
-
return None, "Select a class.", [], []
|
| 244 |
-
try:
|
| 245 |
-
x, y, w, h = map(float, [x, y, w, h])
|
| 246 |
-
except Exception:
|
| 247 |
-
return None, "Coordinates must be numbers.", [], []
|
| 248 |
-
if w <= 0 or h <= 0:
|
| 249 |
-
return None, "Width and height must be greater than zero.", [], []
|
| 250 |
-
data = load_dataset()
|
| 251 |
-
item = next((z for z in data["images"] if z["id"] == image_id), None)
|
| 252 |
-
if not item:
|
| 253 |
-
return None, "Image not found.", [], []
|
| 254 |
-
x = max(0, min(x, item["width"] - 1))
|
| 255 |
-
y = max(0, min(y, item["height"] - 1))
|
| 256 |
-
w = min(w, item["width"] - x)
|
| 257 |
-
h = min(h, item["height"] - y)
|
| 258 |
-
item.setdefault("annotations", []).append({"class": cls, "box": [x, y, w, h]})
|
| 259 |
-
save_dataset(data)
|
| 260 |
-
anns = item["annotations"]
|
| 261 |
-
return draw_annotations(image_id), f"Added {cls}: [{x:.0f}, {y:.0f}, {w:.0f}, {h:.0f}]", anns, anns
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
def remove_annotation(image_id, index):
|
| 265 |
-
if not image_id:
|
| 266 |
-
return None, "Select an image first.", []
|
| 267 |
-
data = load_dataset()
|
| 268 |
-
item = next((z for z in data["images"] if z["id"] == image_id), None)
|
| 269 |
-
if not item:
|
| 270 |
-
return None, "Image not found.", []
|
| 271 |
-
try:
|
| 272 |
-
idx = int(index) - 1
|
| 273 |
-
except Exception:
|
| 274 |
-
return None, "Enter the annotation number to delete.", item.get("annotations", [])
|
| 275 |
-
anns = item.get("annotations", [])
|
| 276 |
-
if idx < 0 or idx >= len(anns):
|
| 277 |
-
return None, "Annotation number not found.", anns
|
| 278 |
-
deleted = anns.pop(idx)
|
| 279 |
-
save_dataset(data)
|
| 280 |
-
return draw_annotations(image_id), f"Deleted annotation {index}: {deleted['class']}", anns
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
def clear_annotations(image_id):
|
| 284 |
-
if not image_id:
|
| 285 |
-
return None, "Select an image first.", []
|
| 286 |
-
data = load_dataset()
|
| 287 |
-
item = next((z for z in data["images"] if z["id"] == image_id), None)
|
| 288 |
-
if not item:
|
| 289 |
-
return None, "Image not found.", []
|
| 290 |
-
item["annotations"] = []
|
| 291 |
-
save_dataset(data)
|
| 292 |
-
return draw_annotations(image_id), "Annotations cleared.", []
|
| 293 |
-
|
| 294 |
-
|
| 295 |
def build_coco():
|
| 296 |
data = load_dataset()
|
| 297 |
classes = read_classes()
|
|
|
|
| 194 |
return annotation_canvas_html(image_id),f"**{item['filename']}** — {item['width']} × {item['height']} px",item.get("annotations",[])
|
| 195 |
|
| 196 |
|
| 197 |
+
def draw_annotations(image_id):
|
| 198 |
+
"""Render the annotation canvas for the selected image."""
|
| 199 |
+
return annotation_canvas_html(image_id)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
def add_annotation(image_id, cls, x, y, w, h):
|
| 203 |
if not image_id:return annotation_canvas_html(None),"Select an image first.",[]
|
| 204 |
if not cls:return annotation_canvas_html(image_id),"Select a class first.",[]
|
|
|
|
| 241 |
return f"Saved {len(classes)} classes.", gr.update(choices=classes, value=classes[0]), dataset_status()
|
| 242 |
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
def build_coco():
|
| 245 |
data = load_dataset()
|
| 246 |
classes = read_classes()
|