Skip to content

Commit

Permalink
Remove pickles in demo, add invert button to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Agustín Castro authored and Agustín Castro committed Feb 28, 2024
1 parent 3236a0d commit 3a2c9e9
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 60 deletions.
45 changes: 10 additions & 35 deletions demos/multi_camera/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,6 @@ def run():
action="store_true",
help="If your footage are a video where the camera might move, you should use a motion estimator. This argument will apply the motion estimator for all your videos indifferently.",
)

parser.add_argument(
"--save-transformation",
action="store_true",
help="Save the transformations after using the UI, so you can pickle them again later.",
)
parser.add_argument(
"--load-transformation",
action="store_true",
help="Load transformations from pickle files instead of using the UI. If no pickle file is found, you will use the UI",
)
parser.add_argument(
"--model",
type=str,
Expand Down Expand Up @@ -257,7 +246,7 @@ def run():
parser.add_argument(
"--hit-counter-max",
type=int,
default=35,
default=45,
help="Max iteration the tracked object is kept after when there are no detections",
)
parser.add_argument(
Expand Down Expand Up @@ -346,29 +335,15 @@ def mask_generator(frame):
reference_path = args.reference
for path in args.files[first_video_is_reference:]:

pickle_path = f"{os.path.splitext(os.path.basename(reference_path))[0]}_{os.path.splitext(os.path.basename(path))[0]}.pkl"

initial_transformations[path] = None
if args.load_transformation:
try:
with open(pickle_path, "rb") as file:
initial_transformations[path] = pickle.load(file)
except FileNotFoundError:
print(f"Couldn't load transformation for {path}. Using the UI instead")
pass
if initial_transformations[path] is None:
initial_transformations[path] = set_reference(
reference_path,
path,
motion_estimator_footage=motion_estimator_footage,
motion_estimator_reference=motion_estimator_reference,
mask_generator=mask_generator,
image_width=args.ui_width,
image_height=args.ui_height,
)
if args.save_transformation:
with open(pickle_path, "wb") as file:
pickle.dump(initial_transformations[path], file)
initial_transformations[path] = set_reference(
reference_path,
path,
motion_estimator_footage=motion_estimator_footage,
motion_estimator_reference=motion_estimator_reference,
mask_generator=mask_generator,
image_width=args.ui_width,
image_height=args.ui_height,
)

if args.use_motion_estimator_footage:
motion_estimators[path].transformation = initial_transformations[path]
Expand Down
121 changes: 96 additions & 25 deletions norfair/common_reference_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,10 @@ def handle_load_state():
global points_sampled
global window
global button_finish
global reference_point
global footage_point
global canvas_footage
global canvas_reference

initialfile = f"{os.path.splitext(os.path.basename(reference))[0]}_to_{os.path.splitext(os.path.basename(footage))[0]}.pkl"

Expand All @@ -1035,11 +1039,6 @@ def handle_load_state():

loaded_state = pickle.load(file)

# remove existing buttons
for point in points.values():
point["button"].destroy()
points = {}

points_reference = loaded_state["reference"]
points_footage = loaded_state["footage"]
transformation = loaded_state["transformation"]
Expand All @@ -1052,27 +1051,10 @@ def handle_load_state():
fg="grey", highlightbackground="SystemButtonFace"
)

points_sampled = 0
# create new points
for reference_point, footage_point, ignore in zip(
points_reference, points_footage, is_ignored
):

new_point = {
"reference": reference_point,
"footage": footage_point,
"button": None,
"marked": False,
"ignore": ignore,
}
points[points_sampled] = new_point
points[points_sampled]["button"] = create_annotation_button(new_point)
if ignore:
points[points_sampled]["button"].configure(
fg="gray", highlightbackground="gray"
)
points = recreate_all_buttons(points_reference, points_footage, is_ignored)

points_sampled += 1
remove_drawings_in_canvas(canvas_footage)
remove_drawings_in_canvas(canvas_reference)

reference_point = None
footage_point = None
Expand All @@ -1084,6 +1066,39 @@ def handle_load_state():
button_load_state.pack(side=tk.LEFT)
frame_options_files.pack(side=tk.TOP)

def recreate_all_buttons(points_reference, points_footage, is_ignored):
global points
global points_sampled

# remove existing buttons
for point in points.values():
point["button"].destroy()
points = {}

points_sampled = 0
# create new points
for reference_point, footage_point, ignore in zip(
points_reference, points_footage, is_ignored
):

new_point = {
"reference": reference_point,
"footage": footage_point,
"button": None,
"marked": False,
"ignore": ignore,
}
points[points_sampled] = new_point
points[points_sampled]["button"] = create_annotation_button(new_point)
if ignore:
points[points_sampled]["button"].configure(
fg="gray", highlightbackground="gray"
)

points_sampled += 1

return points

###### MAKE SUBBLOCK TO CHANGE BETWEEN ANOTATE AND TEST
frame_options_annotate_or_test = tk.Frame(master=frame_options)

Expand Down Expand Up @@ -1230,6 +1245,62 @@ def handle_remove_point():

frame_options_remove.pack(side=tk.TOP)

###### MAKE SUBBLOCK TO INVERT TRANSFORMATION

frame_options_invert = tk.Frame(master=frame_options)
text_invert = tk.Label(
master=frame_options_invert,
text="Invert (pts and transf)",
foreground="white",
background="#5f9ea0",
width=20,
height=1,
)
button_invert = tk.Button(
master=frame_options_invert,
text="Invert",
width=16,
height=1,
bg="blue",
fg="black",
command=lambda: handle_invert(),
)

def handle_invert():
global points
global transformation
global points_sampled
global footage_point
global reference_point
global canvas_footage
global canvas_reference

points_reference = []
points_footage = []
is_ignored = []

for key, couple in points.items():
# swap footage coordinate with reference
points_reference.append(couple["footage"])
points_footage.append(couple["reference"])
is_ignored.append(couple["ignore"])

remove_drawings_in_canvas(canvas_footage)
remove_drawings_in_canvas(canvas_reference)

points = recreate_all_buttons(points_reference, points_footage, is_ignored)
points_sampled = len(points)

reference_point = None
footage_point = None

transformation = estimate_transformation(points)

text_invert.pack(side=tk.LEFT)
button_invert.pack(side=tk.LEFT)

frame_options_invert.pack(side=tk.TOP)

###### MAKE SUBBLOCK TO RESIZE FRAMES

def get_handle_resize(video_type, delta_height):
Expand Down

0 comments on commit 3a2c9e9

Please sign in to comment.