Skip to content

Commit

Permalink
Add option to set image width and height in the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Agustín Castro committed Feb 9, 2024
1 parent 4c166c6 commit 6002348
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
14 changes: 14 additions & 0 deletions demos/multi_camera/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ def run():
default=[360, 288],
help="Output resolution for each subblock",
)
parser.add_argument(
"--ui-width",
type=int,
default=None,
help="Image width in the UI",
)
parser.add_argument(
"--ui-height",
type=int,
default=None,
help="Image height in the UI",
)
parser.add_argument(
"--use-motion-estimator-footage",
action="store_true",
Expand Down Expand Up @@ -345,6 +357,8 @@ def mask_generator(frame):
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:
Expand Down
51 changes: 39 additions & 12 deletions norfair/common_reference_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,24 @@
global button_ignore


def resize_image(image, desired_width=None, desired_height=None):
aspect_ratio = image.height / image.width

if (desired_width is None) and (desired_height is not None):
desired_width = int(desired_height / aspect_ratio)
elif (desired_width is not None) and (desired_height is None):
desired_height = int(aspect_ratio * desired_width)

return image.resize((desired_width, desired_height), Image.LANCZOS)


def set_reference(
reference: str,
footage: str,
transformation_getter: TransformationGetter = None,
mask_generator=None,
desired_size=700,
image_width=None,
image_height=None,
motion_estimator_footage=None,
motion_estimator_reference=None,
):
Expand Down Expand Up @@ -88,8 +100,11 @@ def set_reference(
- 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
sampling points within the mask.
- desired_size: int, optional
How large you want the clickable windows in the UI to be.
- image_width: int, optional
Width of the image of the UI. If the height is not provided, then it will be calculated so that the aspect ratio is preserved.
- image_height: int, optional
Height of the image of the UI. If the width is not provided, then it will be calculated so that the aspect ratio is preserved.
- motion_estimator_footage: MotionEstimator, optional
When using videos for the footage, you can provide a MotionEstimator to relate the coordinates in all the frames in the video.
Expand Down Expand Up @@ -149,7 +164,13 @@ def set_reference(

skipper = {}

radius = max(int(desired_size / 100), 1)
radius = None
if (image_width is None) and (image_height is None):
image_height = 450
elif (image_width is not None) and (image_height is None):
radius = max(int(image_width / 100), 1)
if radius is None:
radius = max(int(image_height / 100), 1)

points = {}
points_sampled = len(points)
Expand Down Expand Up @@ -412,16 +433,17 @@ def handle_annotation(event):
footage_original_height = image.height
footage_original_size = (footage_original_width, footage_original_height)

image.thumbnail((desired_size, desired_size))
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)

canvas_footage = tk.Canvas(
frame_images,
width=footage_photo.width(),
height=footage_photo.height(),
width=footage_canvas_width,
height=footage_canvas_height,
bg="gray",
)
footage_image_container = canvas_footage.create_image(
Expand Down Expand Up @@ -516,16 +538,17 @@ def reference_coord_chosen_in_footage(event):
reference_original_height = image.height
reference_original_size = (reference_original_width, reference_original_height)

image.thumbnail((desired_size, desired_size))
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)

canvas_reference = tk.Canvas(
frame_images,
width=reference_photo.width(),
height=reference_photo.height(),
width=reference_canvas_width,
height=reference_canvas_height,
bg="gray",
)
reference_image_container = canvas_reference.create_image(
Expand Down Expand Up @@ -650,7 +673,9 @@ def handle_reset_video(event):
skipper[video_type]["current_frame"] = 1
image = Image.fromarray(image)

image.thumbnail((desired_size, desired_size))
image = resize_image(
image, desired_width=image_width, desired_height=image_height
)
image = ImageTk.PhotoImage(image)

skipper[video_type]["canvas"].itemconfig(
Expand Down Expand Up @@ -710,7 +735,9 @@ def handle_skip_frame(event):

if change_image:
image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
image.thumbnail((desired_size, desired_size))
image = resize_image(
image, desired_width=image_width, desired_height=image_height
)
image = ImageTk.PhotoImage(image)

skipper[video_type]["canvas"].itemconfig(
Expand Down

0 comments on commit 6002348

Please sign in to comment.