Skip to content

Commit

Permalink
Load the labels object through command
Browse files Browse the repository at this point in the history
  • Loading branch information
roomrys committed Oct 10, 2023
1 parent a8e587a commit 816869b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
4 changes: 3 additions & 1 deletion sleap/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ def __init__(
print("Restoring GUI state...")
self.restoreState(prefs["window state"])

if labels_path:
if labels_path is not None:
self.commands.loadProjectFile(filename=labels_path)
elif labels is not None:
self.commands.loadLabelsObject(labels=labels)
else:
self.state["project_loaded"] = False

Expand Down
25 changes: 12 additions & 13 deletions sleap/gui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class which inherits from `AppCommand` (or a more specialized class such as
from enum import Enum
from glob import glob
from pathlib import Path, PurePath
from typing import Callable, Dict, Iterator, List, Optional, Tuple, Type
from typing import Callable, Dict, Iterator, List, Optional, Tuple, Type, Union

import attr
import cv2
Expand Down Expand Up @@ -260,16 +260,15 @@ def loadLabelsObject(self, labels: Labels, filename: Optional[str] = None):
"""
self.execute(LoadLabelsObject, labels=labels, filename=filename)

def loadProjectFile(self, filename: str):
def loadProjectFile(self, filename: Union[str, Labels]):
"""Loads given labels file into GUI.
Args:
filename: The path to the saved labels dataset. If None,
then don't do anything.
filename: The path to the saved labels dataset or the `Labels` object.
If None, then don't do anything.
Returns:
None
"""
self.execute(LoadProjectFile, filename=filename)

Expand Down Expand Up @@ -647,9 +646,8 @@ def do_action(context: "CommandContext", params: dict):
Returns:
None.
"""
filename = params["filename"]
filename = params.get("filename", None) # If called with just a Labels object
labels: Labels = params["labels"]

context.state["labels"] = labels
Expand All @@ -669,7 +667,9 @@ def do_action(context: "CommandContext", params: dict):
context.state["video"] = labels.videos[0]

context.state["project_loaded"] = True
context.state["has_changes"] = params.get("changed_on_load", False)
context.state["has_changes"] = params.get("changed_on_load", False) or (
filename is None
)

# This is not listed as an edit command since we want a clean changestack
context.app.on_data_update([UpdateTopic.project, UpdateTopic.all])
Expand All @@ -683,17 +683,16 @@ def ask(context: "CommandContext", params: dict):
if len(filename) == 0:
return

gui_video_callback = Labels.make_gui_video_callback(
search_paths=[os.path.dirname(filename)], context=params
)

has_loaded = False
labels = None
if type(filename) == Labels:
if isinstance(filename, Labels):
labels = filename
filename = None
has_loaded = True
else:
gui_video_callback = Labels.make_gui_video_callback(
search_paths=[os.path.dirname(filename)], context=params
)
try:
labels = Labels.load_file(filename, video_search=gui_video_callback)
has_loaded = True
Expand Down

0 comments on commit 816869b

Please sign in to comment.