Skip to content

Commit c5968a0

Browse files
author
Agustín Castro
committed
Use motion estimator and mask generator when using videos
1 parent 1fdf76a commit c5968a0

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

norfair/common_reference_ui.py

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def set_reference(
4545
reference: str,
4646
footage: str,
4747
transformation_getter: TransformationGetter = None,
48-
mask_generator=None,
48+
mask_generator=None,
4949
desired_size=700,
5050
motion_estimator=None,
5151
):
@@ -58,11 +58,11 @@ def set_reference(
5858
To add a point, just click a pair of points (one from the footage window, and another from the reference window) and select "Add"
5959
To remove a point, just select the corresponding point at the bottom left corner, and select "Remove".
6060
61-
If either footage or reference are videos, you can jump to future frames to pick points that match.
61+
If either footage or reference are videos, you can jump to future frames to pick points that match.
6262
For example, to jump 215 frames in the footage, just write an integer number of frames to jump next to 'Frames to skip (footage)', and select "Skip frames".
6363
A motion estimator can be used to relate the coordinates of the current frame you see (in either footage or reference) to coordinates in its corresponding first frame.
6464
65-
Once a transformation has been estimated, you can test it:
65+
Once a transformation has been estimated, you can test it:
6666
To Test your transformation, Select the 'Test' mode, and pick a point in either the reference or the footage, and see the associated point in the other window.
6767
You can keep adding more associated points until you are satisfied with the estimated transformation
6868
@@ -74,16 +74,16 @@ def set_reference(
7474
Path to the footage image or video
7575
7676
- transformation_getter: TransformationGetter, optional
77-
TransformationGetter defining the type of transformation you want to fix between reference and footage.
78-
Since the transformation can be really far from identity (given that the perspectives in footage and reference can be immensely different),
77+
TransformationGetter defining the type of transformation you want to fix between reference and footage.
78+
Since the transformation can be really far from identity (given that the perspectives in footage and reference can be immensely different),
7979
and also knowing that outliers shouldn't be common given that a human is picking the points, it is recommended to use a high ransac_reproj_threshold (~ 1000)
8080
81-
- mask_generator: optional function that creates a mask (np.ndarray) from a PIL image. This mask is then provided to the corresponding MotionEstimator to avoid
81+
- mask_generator: optional function that creates a mask (np.ndarray) from a PIL image. This mask is then provided to the corresponding MotionEstimator to avoid
8282
sampling points within the mask.
8383
8484
- desired_size: int, optional
8585
How large you want the clickable windows in the UI to be.
86-
86+
8787
- motion_estimator: MotionEstimator, optional
8888
When using videos for either the footage or the reference, you can provide a MotionEstimator to relate the coordinates in all the frames in the video.
8989
The motion estimator is only useful if the camera in either the video of the footage or the video of the reference can move. Otherwise, avoid using it.
@@ -176,15 +176,22 @@ def estimate_transformation(points):
176176
return None
177177

178178
def test_transformation(
179-
change_of_coordinates, canvas, point, original_size, canvas_size, motion_transformation=None,
179+
change_of_coordinates,
180+
canvas,
181+
point,
182+
original_size,
183+
canvas_size,
184+
motion_transformation=None,
180185
):
181186
point_in_new_coordinates = change_of_coordinates(np.array([point]))[0]
182187

183188
try:
184-
point_in_new_coordinates = motion_transformation.abs_to_rel(np.array([point_in_new_coordinates]))[0]
189+
point_in_new_coordinates = motion_transformation.abs_to_rel(
190+
np.array([point_in_new_coordinates])
191+
)[0]
185192
except AttributeError:
186193
pass
187-
194+
188195
point_in_canvas_coordinates = np.multiply(
189196
point_in_new_coordinates,
190197
np.array(
@@ -256,37 +263,46 @@ def handle_annotation(event):
256263
global reference_canvas_size
257264
global footage_original_size
258265
global footage_canvas_size
259-
266+
260267
points[key]["marked"] = not points[key]["marked"]
261268

262269
if points[key]["marked"]:
263270
points[key]["button"].configure(fg="black", highlightbackground="red")
264271

265272
try:
266-
footage_point_in_rel_coords = skipper["footage"]["motion_transformation"].abs_to_rel(np.array([points[key]["footage"]]))[0]
273+
footage_point_in_rel_coords = skipper["footage"][
274+
"motion_transformation"
275+
].abs_to_rel(np.array([points[key]["footage"]]))[0]
267276
footage_point_in_rel_coords = np.multiply(
268277
footage_point_in_rel_coords,
269278
np.array(
270-
[footage_canvas_size[0] / footage_original_size[0], footage_canvas_size[1] / footage_original_size[1]]
279+
[
280+
footage_canvas_size[0] / footage_original_size[0],
281+
footage_canvas_size[1] / footage_original_size[1],
282+
]
271283
),
272284
).astype(int)
273285
except AttributeError:
274286
footage_point_in_rel_coords = points[key]["footage_canvas"]
275287
pass
276-
288+
277289
try:
278-
reference_point_in_rel_coords = skipper["reference"]["motion_transformation"].abs_to_rel(np.array([points[key]["footage"]]))[0]
290+
reference_point_in_rel_coords = skipper["reference"][
291+
"motion_transformation"
292+
].abs_to_rel(np.array([points[key]["footage"]]))[0]
279293
reference_point_in_rel_coords = np.multiply(
280294
reference_point_in_rel_coords,
281295
np.array(
282-
[reference_canvas_size[0] / reference_original_size[0], reference_canvas_size[1] / reference_original_size[1]]
296+
[
297+
reference_canvas_size[0] / reference_original_size[0],
298+
reference_canvas_size[1] / reference_original_size[1],
299+
]
283300
),
284301
).astype(int)
285302
except AttributeError:
286303
reference_point_in_rel_coords = points[key]["reference_canvas"]
287304
pass
288305

289-
290306
draw_point_in_canvas(
291307
canvas_footage, footage_point_in_rel_coords, color="red"
292308
)
@@ -348,7 +364,9 @@ def handle_annotation(event):
348364
mask = mask_generator(image)
349365
else:
350366
mask = None
351-
motion_transformation = motion_estimator_footage.update(np.array(image), mask)
367+
motion_transformation = motion_estimator_footage.update(
368+
np.array(image), mask
369+
)
352370

353371
footage_original_width = image.width
354372
footage_original_height = image.height
@@ -379,14 +397,20 @@ def reference_coord_chosen_in_footage(event):
379397
footage_point_canvas = (event.x, event.y)
380398
draw_point_in_canvas(canvas_footage, footage_point_canvas)
381399

382-
383400
footage_point = np.array(
384-
[event.x * (footage_original_width / footage_canvas_width), event.y * (footage_original_height / footage_canvas_height)]
401+
[
402+
event.x * (footage_original_width / footage_canvas_width),
403+
event.y * (footage_original_height / footage_canvas_height),
404+
]
385405
)
386406
print("Footage window clicked at: ", footage_point.round(1))
387407

388408
try:
389-
footage_point = skipper["footage"]["motion_transformation"].rel_to_abs(np.array([footage_point]))[0].round(1)
409+
footage_point = (
410+
skipper["footage"]["motion_transformation"]
411+
.rel_to_abs(np.array([footage_point]))[0]
412+
.round(1)
413+
)
390414
except AttributeError:
391415
pass
392416

@@ -420,7 +444,6 @@ def reference_coord_chosen_in_footage(event):
420444
"current_frame_label": None,
421445
}
422446

423-
424447
motion_estimator_reference = None
425448
motion_transformation = None
426449
try:
@@ -467,18 +490,23 @@ def reference_coord_chosen_in_reference(event):
467490
global footage_canvas_size
468491
global skipper
469492

470-
471-
472493
reference_point_canvas = (event.x, event.y)
473494
draw_point_in_canvas(canvas_reference, reference_point_canvas)
474495

475496
reference_point = np.array(
476-
[event.x * (reference_original_width / reference_canvas_width), event.y * (reference_original_height / reference_canvas_height)]
497+
[
498+
event.x * (reference_original_width / reference_canvas_width),
499+
event.y * (reference_original_height / reference_canvas_height),
500+
]
477501
)
478502
print("Reference window clicked at: ", reference_point.round(1))
479503

480504
try:
481-
reference_point = skipper["reference"]["motion_transformation"].rel_to_abs(np.array([reference_point]))[0].round(1)
505+
reference_point = (
506+
skipper["reference"]["motion_transformation"]
507+
.rel_to_abs(np.array([reference_point]))[0]
508+
.round(1)
509+
)
482510
except AttributeError:
483511
pass
484512

@@ -560,8 +588,10 @@ def handle_skip_frame(event):
560588
mask = mask_generator(image)
561589
else:
562590
mask = None
563-
motion_transformation = motion_estimator.update(np.array(image), mask)
564-
591+
motion_transformation = motion_estimator.update(
592+
np.array(image), mask
593+
)
594+
565595
skipper[video_type]["motion_estimator"] = motion_estimator
566596
skipper[video_type]["motion_transformation"] = motion_transformation
567597

0 commit comments

Comments
 (0)