Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

Expand Down
35 changes: 24 additions & 11 deletions source/camera/basic/connect.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
"""
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():
print(f"Serial number: {camera.info.serial_number}, IP address: {camera.state.network.ipv4.address}")


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-<serial-number>.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")
Expand Down
Loading