Files
zed-playground/workspaces/py_workspace/zed_network_utils.py
T
2026-03-06 17:17:59 +08:00

89 lines
2.5 KiB
Python

import json
import os
import pyzed.sl as sl
DEFAULT_CONFIG_PATH = "/workspaces/zed-playground/zed_settings/inside_network.json"
def parse_network_config(config_file=DEFAULT_CONFIG_PATH):
"""
Parses the network configuration JSON file and returns a dictionary of camera configurations.
Args:
config_file (str): Path to the JSON configuration file.
Returns:
dict: A dictionary where keys are serial numbers (str) and values are configuration dicts,
or None if the file doesn't exist or is invalid.
"""
if not os.path.exists(config_file):
print(f"Configuration file not found: {config_file}")
return None
try:
with open(config_file, "r") as f:
network_config = json.load(f)
return network_config
except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}")
return None
def get_camera_config_by_serial(serial_number, config_file=DEFAULT_CONFIG_PATH):
"""
Retrieves configuration for a specific camera serial number.
"""
config = parse_network_config(config_file)
if config:
return config.get(str(serial_number))
return None
def extract_ip_port(config_entry):
"""
Extracts IP and Port from a camera configuration entry.
Args:
config_entry (dict): A single camera configuration dictionary.
Returns:
tuple: (ip_address (str), port (int)) or (None, None) if parsing fails.
"""
try:
comm_params = config_entry["FusionConfiguration"]["communication_parameters"][
"CommunicationParameters"
]
ip = comm_params["ip_add"]
port = comm_params["ip_port"]
return ip, port
except (KeyError, TypeError) as e:
print(f"Error extracting IP/Port from config: {e}")
return None, None
def open_remote_camera(zed, ip, port):
"""
Opens a remote ZED camera using set_from_stream.
Args:
zed (sl.Camera): The ZED Camera object.
ip (str): IP address.
port (int): Port number.
Returns:
bool: True if opened successfully, False otherwise.
"""
init_params = sl.InitParameters()
init_params.depth_mode = sl.DEPTH_MODE.NONE
init_params.camera_resolution = sl.RESOLUTION.AUTO
print(f"Connecting to {ip}:{port}...")
init_params.set_from_stream(ip, port)
status = zed.open(init_params)
if status != sl.ERROR_CODE.SUCCESS:
print(f"Failed to open camera at {ip}:{port}. Error: {status}")
return False
return True