From f9182f66d7437e2750e4931a3f9bb4060c140d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Castro?= Date: Wed, 21 Feb 2024 16:09:24 -0300 Subject: [PATCH] Add buttons to resize images Add button to load or save the state --- norfair/common_reference_ui.py | 362 ++++++++++++++++++++++++++------- 1 file changed, 287 insertions(+), 75 deletions(-) diff --git a/norfair/common_reference_ui.py b/norfair/common_reference_ui.py index ed9705e3..97ae8980 100644 --- a/norfair/common_reference_ui.py +++ b/norfair/common_reference_ui.py @@ -1,5 +1,7 @@ import os +import pickle import tkinter as tk +import tkinter.filedialog from copy import deepcopy import cv2 @@ -320,35 +322,34 @@ def handle_annotation(event): footage_point_in_rel_coords = skipper["footage"][ "motion_transformation" ].rel_to_abs(np.array([points[key]["footage"]]))[0] - footage_point_in_rel_coords = np.multiply( - footage_point_in_rel_coords, - np.array( - [ - footage_canvas_size[0] / footage_original_size[0], - footage_canvas_size[1] / footage_original_size[1], - ] - ), - ).astype(int) except AttributeError: - footage_point_in_rel_coords = points[key]["footage_canvas"] - pass + footage_point_in_rel_coords = points[key]["footage"] + footage_point_in_rel_coords = np.multiply( + footage_point_in_rel_coords, + np.array( + [ + footage_canvas_size[0] / footage_original_size[0], + footage_canvas_size[1] / footage_original_size[1], + ] + ), + ).astype(int) try: reference_point_in_rel_coords = skipper["reference"][ "motion_transformation" ].rel_to_abs(np.array([points[key]["reference"]]))[0] - reference_point_in_rel_coords = np.multiply( - reference_point_in_rel_coords, - np.array( - [ - reference_canvas_size[0] / reference_original_size[0], - reference_canvas_size[1] / reference_original_size[1], - ] - ), - ).astype(int) except AttributeError: - reference_point_in_rel_coords = points[key]["reference_canvas"] - pass + reference_point_in_rel_coords = points[key]["reference"] + + reference_point_in_rel_coords = np.multiply( + reference_point_in_rel_coords, + np.array( + [ + reference_canvas_size[0] / reference_original_size[0], + reference_canvas_size[1] / reference_original_size[1], + ] + ), + ).astype(int) if points[key]["ignore"]: color = "gray" @@ -429,21 +430,19 @@ def handle_annotation(event): motion_transformation = motion_estimator_footage.update(image, mask) image = Image.fromarray(image) - footage_original_width = image.width - footage_original_height = image.height - footage_original_size = (footage_original_width, footage_original_height) + footage_original_size = (image.width, image.height) - image = resize_image(image, desired_width=image_width, desired_height=image_height) + resized_image = resize_image( + image, desired_width=image_width, desired_height=image_height + ) - footage_photo = ImageTk.PhotoImage(image) - footage_canvas_width = footage_photo.width() - footage_canvas_height = footage_photo.height() - footage_canvas_size = (footage_canvas_width, footage_canvas_height) + footage_photo = ImageTk.PhotoImage(resized_image) + footage_canvas_size = (footage_photo.width(), footage_photo.height()) canvas_footage = tk.Canvas( frame_images, - width=footage_canvas_width, - height=footage_canvas_height, + width=footage_canvas_size[0], + height=footage_canvas_size[1], bg="gray", ) footage_image_container = canvas_footage.create_image( @@ -457,6 +456,8 @@ def reference_coord_chosen_in_footage(event): global canvas_reference global reference_original_size global reference_canvas_size + global footage_original_size + global footage_canvas_size global skipper footage_point_canvas = (event.x, event.y) @@ -464,8 +465,8 @@ def reference_coord_chosen_in_footage(event): footage_point = np.array( [ - event.x * (footage_original_width / footage_canvas_width), - event.y * (footage_original_height / footage_canvas_height), + event.x * (footage_original_size[0] / footage_canvas_size[0]), + event.y * (footage_original_size[1] / footage_canvas_size[1]), ] ) print("Footage window clicked at: ", footage_point.round(1)) @@ -510,6 +511,7 @@ def reference_coord_chosen_in_footage(event): "image_container": footage_image_container, "current_frame_label": None, "path": footage, + "original_image": image, } motion_transformation = None @@ -534,21 +536,18 @@ def reference_coord_chosen_in_footage(event): image = Image.fromarray(image) - reference_original_width = image.width - reference_original_height = image.height - reference_original_size = (reference_original_width, reference_original_height) - - image = resize_image(image, desired_width=image_width, desired_height=image_height) + reference_original_size = (image.width, image.height) + resized_image = resize_image( + image, desired_width=image_width, desired_height=image_height + ) - reference_photo = ImageTk.PhotoImage(image) - reference_canvas_width = reference_photo.width() - reference_canvas_height = reference_photo.height() - reference_canvas_size = (reference_canvas_width, reference_canvas_height) + reference_photo = ImageTk.PhotoImage(resized_image) + reference_canvas_size = (reference_photo.width(), reference_photo.height()) canvas_reference = tk.Canvas( frame_images, - width=reference_canvas_width, - height=reference_canvas_height, + width=reference_canvas_size[0], + height=reference_canvas_size[1], bg="gray", ) reference_image_container = canvas_reference.create_image( @@ -560,6 +559,8 @@ def reference_coord_chosen_in_reference(event): global reference_point_canvas global transformation global canvas_footage + global reference_original_size + global reference_canvas_size global footage_original_size global footage_canvas_size global skipper @@ -569,8 +570,8 @@ def reference_coord_chosen_in_reference(event): reference_point = np.array( [ - event.x * (reference_original_width / reference_canvas_width), - event.y * (reference_original_height / reference_canvas_height), + event.x * (reference_original_size[0] / reference_canvas_size[0]), + event.y * (reference_original_size[1] / reference_canvas_size[1]), ] ) print("Reference window clicked at: ", reference_point.round(1)) @@ -615,6 +616,7 @@ def reference_coord_chosen_in_reference(event): "image_container": reference_image_container, "current_frame_label": None, "path": reference, + "original_image": image, } ######### MAKE SUBBLOCK FOR LOGO @@ -652,6 +654,8 @@ def handle_reset_video(event): global skipper global canvas_footage global canvas_reference + global reference_canvas_size + global footage_canvas_size if skipper[video_type]["current_frame"] > 1: skipper[video_type]["video"].video_capture.release() @@ -672,9 +676,14 @@ def handle_reset_video(event): ].update(image, mask) skipper[video_type]["current_frame"] = 1 image = Image.fromarray(image) + skipper[video_type]["original_image"] = image + if video_type == "reference": + size = reference_canvas_size + else: + size = footage_canvas_size image = resize_image( - image, desired_width=image_width, desired_height=image_height + image, desired_width=size[0], desired_height=size[1] ) image = ImageTk.PhotoImage(image) @@ -698,6 +707,8 @@ def handle_skip_frame(event): global skipper global canvas_footage global canvas_reference + global reference_canvas_size + global footage_canvas_size try: frames_to_skip = int(entry_skip.get()) @@ -735,8 +746,13 @@ def handle_skip_frame(event): if change_image: image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) + skipper[video_type]["original_image"] = image + if video_type == "reference": + size = reference_canvas_size + else: + size = footage_canvas_size image = resize_image( - image, desired_width=image_width, desired_height=image_height + image, desired_width=size[0], desired_height=size[1] ) image = ImageTk.PhotoImage(image) @@ -881,15 +897,37 @@ def handle_skip_frame(event): command=lambda: handle_add_annotation(), ) + def create_annotation_button(point): + global points_sampled + global frame_options_annotations + global handling_mark_functions + global handle_mark_annotation + + handling_mark_functions[points_sampled] = handle_mark_annotation(points_sampled) + + new_button = tk.Button( + master=frame_options_annotations, + text=f"{points_sampled}: reference ({point['reference'][0]}, {point['reference'][1]}) <-> footage ({point['footage'][0]}, {point['footage'][1]})", + width=35, + height=1, + bg="blue", + fg="black", + highlightbackground="SystemButtonFace", + ) + + new_button.bind("