Data Collection and Processing
In the process of sensor data collection and post-processing, exportRuntimeConfig and createSolver are two key interfaces, which are respectively responsible for the core functions of configuration export and data parsing. The following is the interface description and best practices.
1. Core Interface Description
1. exportRuntimeConfig Interface
Sensor.exportRuntimeConfig(self, save_dir="", binary=False) is used to export the sensor runtime configuration. It persists the current sensor’s runtime parameters (such as calibration data, hardware characteristics, etc.) to a specified directory, providing a configuration basis for subsequent offline data parsing.
Parameters:
save_dir(Union[str, Path]): Directory for saving configuration files, default is the current working directory.binary (bool): Whether to return data in binary encrypted format (instead of saving to a file), default isFalse.Return Value:
NoneFunction: Ensure that the collected raw data can use the same sensor configuration as during collection when parsed offline, thus guaranteeing the accuracy of processing results.
2. createSolver Interface
Sensor.createSolver(runtime_path) is a factory class method. It is used to create a SensorSolver instance from a specified runtime configuration path, and this instance is a core tool for offline parsing of sensor data.
Parameter:
runtime_path (Union[str, Path]): Path pointing to the runtime configuration file (generated byexportRuntimeConfig).Return Value: Returns a
SensorSolverinstance on success, and returnsFalseon failure.Function: Load the configuration exported during collection, calculate derived data such as depth maps and force values based on raw data, and realize offline post-processing.
2. Complete Example of Data Collection and Processing
The following is a complete process of “data collection - configuration export - offline processing” combining the two interfaces, including code implementation and key step explanations.
1. Data Collection and Configuration Export (save_data Function)
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
SAVE_DIR = Path(SCRIPT_DIR / "test_dir") # 存放目录
SAVE_DIR.mkdir(parents=True, exist_ok=True)
import cv2
import time
import numpy as np
from xensesdk import Sensor
sensor_id = 'OG000265'
def save_data():
fps = 30
duration = 3 # 秒
frame_interval = 1.0 / fps
total_frames = fps * duration
sensor_0 = Sensor.create(sensor_id)
for i in range(total_frames):
start_time = time.time()
# 采集一帧
rec = sensor_0.selectSensorInfo(Sensor.OutputType.Rectify)
# 生成文件名
filename = SAVE_DIR / f"{sensor_id}_{i:03d}.png"
# 保存图片
cv2.imwrite(str(filename), rec)
print(f"Saved {filename}")
# 控制帧率(30Hz)
elapsed = time.time() - start_time
sleep_time = frame_interval - elapsed
if sleep_time > 0:
time.sleep(sleep_time)
# 导出配置
sensor_0.exportRuntimeConfig(SAVE_DIR)
sensor_0.release()
2. Offline Data Parsing and Post-Processing (replay_data Function)
def replay_data():
sensor_solver = Sensor.createSolver(SAVE_DIR / f"runtime_{sensor_id}")
for png_file in sorted(SAVE_DIR.glob("*.png")):
img = cv2.imread(str(png_file), cv2.IMREAD_UNCHANGED)
depth, force, diff = sensor_solver.selectSensorInfo(
Sensor.OutputType.Depth,
Sensor.OutputType.Force,
Sensor.OutputType.Difference,
rectify_image=img
)
depth_vis = np.clip(depth*200, 0, 255)
cv2.imwrite(SAVE_DIR / f"{png_file.stem}_depth.png", depth_vis)
sensor_solver.release()
3. Main Process Execution
if __name__ == '__main__':
save_data()
replay_data()
print("Data saved and replayed successfully.")
3. Process Description
Collection Phase: Initialize the sensor through
Sensor.create, collect raw images at a fixed frame rate, and callexportRuntimeConfigto export the configuration after collection is completed, ensuring the correspondence between “data and configuration”.Post-Processing Phase: Load the exported configuration through
createSolver, create a parser instance, perform processing such as depth calculation on the raw images, and finally generate derived data (e.g., depth maps).
This process ensures the consistency of data collection and parsing, and is applicable to scenarios that require offline analysis of sensor data (such as algorithm verification, data visualization, etc.).