Skip to content

Commit c3dea66

Browse files
Add tracking_result argument to tracking annotator
1 parent ce86083 commit c3dea66

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ TODO link to video tutorial
129129

130130
- By default, the applications pre-compute the image embeddings produced by SegmentAnything and store them on disc. If you are using a CPU this step can take a while for 3d data or timeseries (you will see a progress bar with a time estimate). If you have access to a GPU without graphical interface (e.g. via a local computer cluster or a cloud provider), you can also pre-compute the embeddings there and then copy them to your laptop / local machine to speed this up. You can use the command `micro_sam.precompute_embeddings` for this (it is installed with the rest of the applications). You can specify the location of the precomputed embeddings via the `embedding_path` argument.
131131
- Most other processing steps are very fast even on a CPU, so interactive annotation is possible. An exception is the automatic segmentation step (2d segmentation), which takes several minutes without a GPU (depending on the image size). For large volumes and timeseries segmenting an object in 3d / tracking across time can take a couple settings with a CPU (it is very fast with a GPU).
132-
- You can save and load the results from the `committed_objects` / `committed_tracks` layer to correct segmentations you obtained from another tool (e.g. CellPose) or to save intermediate annotation results. The results can be saved via `File->Save Selected Layer(s) ...` in the napari menu. They can be loaded again by specifying the corresponding location via the `segme` (2d and 3d segmentation) `xxx` (tracking) argument.
132+
- You can save and load the results from the `committed_objects` / `committed_tracks` layer to correct segmentations you obtained from another tool (e.g. CellPose) or to save intermediate annotation results. The results can be saved via `File->Save Selected Layer(s) ...` in the napari menu (see the tutorial videos for details). They can be loaded again by specifying the corresponding location via the `segmentation_result` (2d and 3d segmentation) or `tracking_result` (tracking) argument.
133133

134134
### Known limitations
135135

micro_sam/sam_annotator/annotator_2d.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def main():
152152
"otherwise they will be recomputed every time (which can take a long time)."
153153
)
154154
parser.add_argument(
155-
"-s", "--segmentation",
155+
"-s", "--segmentation_result",
156156
help="Optional filepath to a precomputed segmentation. If passed this will be used to initialize the "
157157
"'committed_objects' layer. This can be useful if you want to correct an existing segmentation or if you "
158158
"have saved intermediate results from the annotator and want to continue with your annotations. "
@@ -170,15 +170,15 @@ def main():
170170
args = parser.parse_args()
171171
raw = util.load_image_data(args.input, ndim=2, key=args.key)
172172

173-
if args.segmentation is None:
174-
segmentation = None
173+
if args.segmentation_result is None:
174+
segmentation_result = None
175175
else:
176-
segmentation = util.load_image_data(args.segmentation, args.segmentation_key)
176+
segmentation_result = util.load_image_data(args.segmentation_result, args.segmentation_key)
177177

178178
if args.embedding_path is None:
179179
warnings.warn("You have not passed an embedding_path. Restarting the annotator may take a long time.")
180180

181181
annotator_2d(
182182
raw, embedding_path=args.embedding_path,
183-
show_embeddings=args.show_embeddings, segmentation_result=segmentation
183+
show_embeddings=args.show_embeddings, segmentation_result=segmentation_result
184184
)

micro_sam/sam_annotator/annotator_3d.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def main():
277277
"otherwise they will be recomputed every time (which can take a long time)."
278278
)
279279
parser.add_argument(
280-
"-s", "--segmentation",
280+
"-s", "--segmentation_result",
281281
help="Optional filepath to a precomputed segmentation. If passed this will be used to initialize the "
282282
"'committed_objects' layer. This can be useful if you want to correct an existing segmentation or if you "
283283
"have saved intermediate results from the annotator and want to continue with your annotations. "
@@ -295,15 +295,15 @@ def main():
295295
args = parser.parse_args()
296296
raw = util.load_image_data(args.input, ndim=3, key=args.key)
297297

298-
if args.segmentation is None:
299-
segmentation = None
298+
if args.segmentation_result is None:
299+
segmentation_result = None
300300
else:
301-
segmentation = util.load_image_data(args.segmentation, args.segmentation_key)
301+
segmentation_result = util.load_image_data(args.segmentation_result, args.segmentation_key)
302302

303303
if args.embedding_path is None:
304304
warnings.warn("You have not passed an embedding_path. Restarting the annotator may take a long time.")
305305

306306
annotator_3d(
307307
raw, embedding_path=args.embedding_path,
308-
show_embeddings=args.show_embeddings, segmentation_result=segmentation
308+
show_embeddings=args.show_embeddings, segmentation_result=segmentation_result
309309
)

micro_sam/sam_annotator/annotator_tracking.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def commit_tracking_widget(v: Viewer, layer: str = "current_track"):
299299
v.layers["prompts"].refresh()
300300

301301

302-
def annotator_tracking(raw, embedding_path=None, show_embeddings=False):
302+
def annotator_tracking(raw, embedding_path=None, show_embeddings=False, tracking_result=None):
303303
# global state
304304
global PREDICTOR, IMAGE_EMBEDDINGS, CURRENT_TRACK_ID, LINEAGE
305305
global TRACKING_WIDGET
@@ -317,7 +317,11 @@ def annotator_tracking(raw, embedding_path=None, show_embeddings=False):
317317
v = Viewer()
318318

319319
v.add_image(raw)
320-
v.add_labels(data=np.zeros(raw.shape, dtype="uint32"), name="committed_tracks")
320+
if tracking_result is None:
321+
v.add_labels(data=np.zeros(raw.shape, dtype="uint32"), name="committed_tracks")
322+
else:
323+
assert tracking_result.shape == raw.shape
324+
v.add_labels(data=tracking_result, name="committed_tracks")
321325
v.add_labels(data=np.zeros(raw.shape, dtype="uint32"), name="current_track")
322326

323327
# show the PCA of the image embeddings
@@ -426,19 +430,17 @@ def main():
426430
"NOTE: It is recommended to pass this argument and store the embeddings, "
427431
"otherwise they will be recomputed every time (which can take a long time)."
428432
)
429-
# Not implemented for the tracking annotator yet.
430-
# And we should change the name for it.
431-
# parser.add_argument(
432-
# "-s", "--segmentation",
433-
# help="Optional filepath to a precomputed segmentation. If passed this will be used to initialize the "
434-
# "'committed_objects' layer. This can be useful if you want to correct an existing segmentation or if you "
435-
# "have saved intermediate results from the annotator and want to continue with your annotations. "
436-
# "Supports the same file formats as 'input'."
437-
# )
438-
# parser.add_argument(
439-
# "-sk", "--segmentation_key",
440-
# help="The key for opening the segmentation data. Same rules as for 'key' apply."
441-
# )
433+
parser.add_argument(
434+
"-t", "--tracking_result",
435+
help="Optional filepath to a precomputed tracking result. If passed this will be used to initialize the "
436+
"'committed_tracks' layer. This can be useful if you want to correct an existing tracking result or if you "
437+
"have saved intermediate results from the annotator and want to continue. "
438+
"Supports the same file formats as 'input'."
439+
)
440+
parser.add_argument(
441+
"-tk", "--tracking_key",
442+
help="The key for opening the tracking result. Same rules as for 'key' apply."
443+
)
442444
parser.add_argument(
443445
"--show_embeddings", action="store_true",
444446
help="Visualize the embeddings computed by SegmentAnything. This can be helpful for debugging."
@@ -447,14 +449,14 @@ def main():
447449
args = parser.parse_args()
448450
raw = util.load_image_data(args.input, ndim=3, key=args.key)
449451

450-
# if args.segmentation is None:
451-
# segmentation = None
452-
# else:
453-
# segmentation = util.load_image_data(args.segmentation, args.segmentation_key)
452+
if args.tracking_result is None:
453+
tracking_result = None
454+
else:
455+
tracking_result = util.load_image_data(args.tracking_result, args.tracking_key)
454456

455457
if args.embedding_path is None:
456458
warnings.warn("You have not passed an embedding_path. Restarting the annotator may take a long time.")
457459

458460
annotator_tracking(
459-
raw, embedding_path=args.embedding_path, show_embeddings=args.show_embeddings
461+
raw, embedding_path=args.embedding_path, show_embeddings=args.show_embeddings, tracking_result=tracking_result
460462
)

0 commit comments

Comments
 (0)