Skip to content

Commit 72d275a

Browse files
Implement commit functionality for tracking
1 parent ddf23c4 commit 72d275a

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

micro_sam/sam_annotator/annotator_tracking.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def track_objet_widget(
221221

222222
# if a division has occurred and it's the first time it occurred for this track
223223
# we need to create the two daughter tracks and update the lineage
224-
if has_division and len(LINEAGE[CURRENT_TRACK_ID]) == 0:
224+
if has_division and (len(LINEAGE[CURRENT_TRACK_ID]) == 0):
225225
_update_lineage()
226226

227227
# clear the old track mask
@@ -271,6 +271,34 @@ def track_id_changed(new_track_id):
271271
return tracking_widget
272272

273273

274+
@magicgui(call_button="Commit [C]", layer={"choices": ["current_track"]})
275+
def commit_tracking_widget(v: Viewer, layer: str = "current_track"):
276+
global CURRENT_TRACK_ID, LINEAGE, TRACKING_WIDGET
277+
278+
seg = v.layers[layer].data
279+
280+
id_offset = int(v.layers["committed_tracks"].data.max())
281+
mask = seg != 0
282+
283+
v.layers["committed_tracks"].data[mask] = (seg[mask] + id_offset)
284+
v.layers["committed_tracks"].refresh()
285+
286+
# reset the lineage and track id
287+
CURRENT_TRACK_ID = 1
288+
LINEAGE = {1: []}
289+
290+
# reset the choices in the track_id menu
291+
track_ids = list(map(str, LINEAGE.keys()))
292+
TRACKING_WIDGET[1].choices = track_ids
293+
294+
shape = v.layers["raw"].data.shape
295+
v.layers[layer].data = np.zeros(shape, dtype="uint32")
296+
v.layers[layer].refresh()
297+
298+
v.layers["prompts"].data = []
299+
v.layers["prompts"].refresh()
300+
301+
274302
def annotator_tracking(raw, embedding_path=None, show_embeddings=False):
275303
# global state
276304
global PREDICTOR, IMAGE_EMBEDDINGS, CURRENT_TRACK_ID, LINEAGE
@@ -336,6 +364,7 @@ def annotator_tracking(raw, embedding_path=None, show_embeddings=False):
336364

337365
v.window.add_dock_widget(segment_frame_wigdet)
338366
v.window.add_dock_widget(track_objet_widget)
367+
v.window.add_dock_widget(commit_tracking_widget)
339368

340369
#
341370
# key bindings
@@ -353,6 +382,10 @@ def _track_object(v):
353382
def _toggle_label(event=None):
354383
toggle_label(prompts)
355384

385+
@v.bind_key("c")
386+
def _commit(v):
387+
commit_tracking_widget(v)
388+
356389
@v.bind_key("Shift-C")
357390
def clear_prompts(v):
358391
prompts.data = []

0 commit comments

Comments
 (0)