diff --git a/source/applications/advanced/visualization/capture_vis_3d_in_loop_with_keypress_exit.py b/source/applications/advanced/visualization/capture_vis_3d_in_loop_with_keypress_exit.py index 9414d3be..fae1161a 100644 --- a/source/applications/advanced/visualization/capture_vis_3d_in_loop_with_keypress_exit.py +++ b/source/applications/advanced/visualization/capture_vis_3d_in_loop_with_keypress_exit.py @@ -46,9 +46,7 @@ def _main() -> None: print("Setting up visualization") visualizer_running = threading.Event() - accept_end = threading.Event() - accept_end.set() - quit_requested = threading.Event() + visualizer_running.set() use_raw_terminal = sys.platform != "win32" and sys.stdin.isatty() if use_raw_terminal: @@ -62,36 +60,22 @@ def _main() -> None: def _capture_and_keypress_thread() -> None: print("Press 'q' in the terminal to quit") - while not quit_requested.is_set(): - if not visualizer_running.wait(timeout=0.01): - continue - key = _get_key_non_blocking() - if key == "q": + while visualizer_running.is_set(): + if _get_key_non_blocking() == "q": print("Closing application because user pressed 'q'") - quit_requested.set() visualizer.close() - else: - accept_end.clear() - new_frame = camera.capture_2d_3d(settings) - if visualizer_running.is_set(): - visualizer.show(new_frame) - accept_end.set() + break + new_frame = camera.capture_2d_3d(settings) + if visualizer_running.is_set(): + visualizer.show(new_frame) time.sleep(0.01) capture_thread = threading.Thread(target=_capture_and_keypress_thread) capture_thread.start() - print("Running visualizer. Blocking until window closes.") - while True: - visualizer_running.set() - visualizer.run() - visualizer_running.clear() - - if quit_requested.is_set(): - break - print("Visualizer window closed by user. It will be reopened if we're currently capturing.") - if accept_end.is_set(): - break + print("Running visualizer. Blocking until the window closes or 'q' is pressed.") + visualizer.run() + visualizer_running.clear() capture_thread.join() diff --git a/source/camera/basic/connect.py b/source/camera/basic/connect.py index 90ca3897..7bed60aa 100644 --- a/source/camera/basic/connect.py +++ b/source/camera/basic/connect.py @@ -1,13 +1,28 @@ """ Connect to a Zivid camera using the different available methods. -Replace the IP address and serial number in the code with the ones of your camera. +Replace the IP address, serial number and hostname in the code with the ones of your camera, +or provide them with --serial, --ip and --hostname. """ +import argparse + import zivid +def _options() -> argparse.Namespace: + parser = argparse.ArgumentParser(description=__doc__) + + parser.add_argument("--serial", required=False, type=str, default="2020C0DE", help="Serial number of the camera") + parser.add_argument("--ip", required=False, type=str, default="172.28.60.5", help="IP address of the camera") + parser.add_argument( + "--hostname", required=False, type=str, default="zivid-2020C0DE.local", help="Hostname of the camera" + ) + + return parser.parse_args() + + def _print_discovered_cameras(app: zivid.Application) -> None: print("Discovered cameras:") for camera in app.cameras(): @@ -15,30 +30,28 @@ def _print_discovered_cameras(app: zivid.Application) -> None: def _main() -> None: + user_options = _options() + app = zivid.Application() _print_discovered_cameras(app) - print( - "The serial number, IP address and hostname below are placeholders. Replace them with the ones of your camera." - ) - print("Connecting to the first available camera") camera = app.connect_camera() camera.disconnect() - print("Connecting to the camera with a specific serial number") - camera = app.connect_camera(serial_number="2020C0DE") + print(f"Connecting to the camera with serial number {user_options.serial}") + camera = app.connect_camera(serial_number=user_options.serial) camera.disconnect() - print("Connecting to the camera at a specific IP address") - camera = app.connect_camera(address=zivid.CameraAddress("172.28.60.5")) + print(f"Connecting to the camera at IP address {user_options.ip}") + camera = app.connect_camera(address=zivid.CameraAddress(user_options.ip)) camera.disconnect() - print("Connecting to the camera at a specific hostname") + print(f"Connecting to the camera at hostname {user_options.hostname}") # The default hostname format is "zivid-.local". # The hostname cannot be read or set through the SDK. - camera = app.connect_camera(address=zivid.CameraAddress("zivid-2020C0DE.local")) + camera = app.connect_camera(address=zivid.CameraAddress(user_options.hostname)) camera.disconnect() print("Connecting to all available cameras")