Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Three Different Cases for Adding a New Instance #1859

Merged
merged 14 commits into from
Jul 26, 2024
Merged
36 changes: 34 additions & 2 deletions sleap/gui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,8 @@ def create_new_instance(
copy_instance=copy_instance,
new_instance=new_instance,
mark_complete=mark_complete,
init_method=init_method,
location=location,
)

if has_missing_nodes:
Expand Down Expand Up @@ -2984,6 +2986,8 @@ def set_visible_nodes(
copy_instance: Optional[Union[Instance, PredictedInstance]],
new_instance: Instance,
mark_complete: bool,
7174Andy marked this conversation as resolved.
Show resolved Hide resolved
init_method: str,
location: Optional[QtCore.QPoint] = None,
) -> bool:
"""Sets visible nodes for new instance.

Expand All @@ -3010,6 +3014,14 @@ def set_visible_nodes(
scale_width = new_size_width / old_size_width
scale_height = new_size_height / old_size_height

# Calculate the difference from the cursor to the
# original position for getting new x and y values.
if location is not None and init_method == "best":
7174Andy marked this conversation as resolved.
Show resolved Hide resolved
reference_x = copy_instance[context.state["skeleton"].node_names[0]].x
reference_y = copy_instance[context.state["skeleton"].node_names[0]].y
offset_x = location.x() - reference_x
offset_y = location.y() - reference_y
7174Andy marked this conversation as resolved.
Show resolved Hide resolved

# Go through each node in skeleton.
for node in context.state["skeleton"].node_names:
# If we're copying from a skeleton that has this node.
Expand All @@ -3018,8 +3030,28 @@ def set_visible_nodes(
# We don't want to copy a PredictedPoint or score attribute.
x_old = copy_instance[node].x
y_old = copy_instance[node].y
x_new = x_old * scale_width
y_new = y_old * scale_height

# Copy the instance without scale or offset if predicted
if isinstance(copy_instance, PredictedInstance):
x_new = x_old
y_new = y_old
else:
# Scale the x and y values to the new frame size.
if (x_old + 10) * scale_width <= (new_size_width - 10):
x_new = (x_old + 10) * scale_width
else:
x_new = x_old

# Scale the x and y values to the new frame size.
if (y_old + 10) * scale_height <= (new_size_height - 10):
y_new = (y_old + 10) * scale_height
else:
y_new = y_old
7174Andy marked this conversation as resolved.
Show resolved Hide resolved

# Add offsets when location is passed
if location is not None and init_method == "best":
x_new += offset_x
y_new += offset_y
7174Andy marked this conversation as resolved.
Show resolved Hide resolved

new_instance[node] = Point(
x=x_new,
Expand Down
5 changes: 4 additions & 1 deletion sleap/gui/widgets/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,10 @@ def show_contextual_menu(self, where: QtCore.QPoint):

menu.addAction("Add Instance:").setEnabled(False)

menu.addAction("Default", lambda: self.context.newInstance(init_method="best"))
menu.addAction(
"Default",
lambda: self.context.newInstance(init_method="best", location=scene_pos),
)

menu.addAction(
"Average",
Expand Down