update example and coomputation dependency error handling

This commit is contained in:
unknown
2025-12-30 15:58:06 +08:00
parent 398d2b1706
commit 157d7b1131
10 changed files with 73 additions and 313 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

After

Width:  |  Height:  |  Size: 2.1 MiB

-112
View File
@@ -1,112 +0,0 @@
"""
示例 1: 基础用法 - 单传感器同步处理
本示例演示 OrisysSensor 的基本使用流程:
1. 初始化传感器(视频文件或摄像头)
2. 循环获取图像并计算形变
3. 读取各类传感器数据
4. 可视化显示
适用场景:单传感器、简单应用、学习入门
"""
import os
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0" # Windows 下禁用硬件加速,避免兼容性问题
import cv2
import orisys
def main():
# =========================================================================
# 1. 初始化传感器
# =========================================================================
# 参数说明:
# vid_src: 视频源,可以是文件路径(如 "./s1.mp4")或摄像头索引(如 0)
# isstitch: 是否启用复眼拼接模式(多单元传感器需要开启)
# cuda: 是否使用 CUDA GPU 加速(需要 CuPy 和 CUDA 支持)
# verbose: 是否打印详细初始化日志
# 方式1: 从视频文件读取
sensor = orisys.Sensor("./s1.mp4", isstitch=True, cuda=True, verbose=True,config_name="prototype")
# 方式2: 从摄像头读取(取消注释使用)
# sensor = orisys.Sensor(0, isstitch=True, cuda=True, verbose=True)
print("\n按 'q' 退出程序,按 'r' 重置光流追踪器")
# =========================================================================
# 2. 主循环:获取图像 → 计算形变 → 读取数据 → 显示
# =========================================================================
while True:
# 步骤 2.1: 获取并拼接图像
sensor.get_img()
# 步骤 2.2: 计算形变场
sensor.compute_deformation()
# 步骤 2.3: 读取传感器数据
# read_info() 支持以下数据类型:
# 向量场数据:
# - VRAW: 原始光流场 (H, W, 2) 的 numpy 数组
# - VNORMAL: 法向分量场
# - VSHEAR: 切向分量场
# 标量数据:
# - FNORMAL: 法向力总和(正压力)
# - FSHEARX: X方向切向力
# - FSHEARY: Y方向切向力
# 其他:
# - FPS: 当前帧率
# - TIMESTAMP: 时间戳字符串
fps, fn, fx, fy, flow = sensor.read_info(
sensor.info.FPS,
sensor.info.FNORMAL,
sensor.info.FSHEARX,
sensor.info.FSHEARY,
sensor.info.VRAW
)
# 打印力的数值
print(f"FPS: {fps:5.1f}, 法向力: {fn:8.2f}, 切向力X: {fx:8.2f}, 切向力Y: {fy:8.2f}")
# =====================================================================
# 3. 可视化显示
# =====================================================================
# 绘制光流箭头图
# 参数说明:
# img: 背景图像
# flow: 光流场
# threshold: 显示阈值(小于此值的箭头不显示)
# grid_spacing: 箭头网格间距(像素)
# arrow_scale: 箭头缩放比例
arrows = orisys.util.draw_arrows(
sensor.img.copy(), # 使用 copy() 避免修改原图
flow,
threshold=5,
grid_spacing=40,
arrow_scale=1.0
)
cv2.imshow("形变场 Deformation", arrows)
# =====================================================================
# 4. 键盘控制
# =====================================================================
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
print("\n退出程序...")
break
if key == ord("r"):
# 重置光流追踪器(当追踪漂移时使用)
sensor.reset()
print("光流追踪器已重置")
# =========================================================================
# 5. 清理资源
# =========================================================================
sensor.disconnect() # 释放视频源和相关资源
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
@@ -1,30 +1,42 @@
"""
示例 1: 基础用法 - 单传感器同步处理
本示例演示 OrisysSensor 的基本使用流程:
1. 初始化传感器(视频文件或摄像头)
2. 循环获取图像并计算形变
3. 读取各类传感器数据
适用场景:单传感器、简单应用、学习入门
"""
import os
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0" # Windows 下禁用硬件加速,避免兼容性问题
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
import cv2
import orisys
import numpy as np
import time
def main():
# 步骤1:创建传感器对象 (必要步骤),config_name为配置文件名(内置配置文件名)或路径(自定义配置文件路径)
# 方式1: 从视频文件读取
# sensor = orisys.Sensor("./s1.mp4", isstitch=True, cuda=True, verbose=True,config_name="prototype")
# 方式2: 从摄像头读取(取消注释使用)
sensor = orisys.Sensor(0, isstitch=True, cuda=True, verbose=True,config_name="prototype")
# 方式2: 从摄像头读取(取消注释使用)
sensor = orisys.Sensor(0, cuda=True, verbose=True, config_name="prototype")
print("\n按 'q' 退出程序,按 'r' 重置光流追踪器")
while True:
# 步骤 2.1: 获取并拼接图像
sensor.get_img()
# 步骤2:信息提取
# 步骤 2.1: 获取并拼接图像 (必要步骤)
sensor.get_img()
# 步骤 2.2: 计算形变场
# 步骤 2.2: 计算形变场 (必要步骤)
sensor.compute_deformation()
# 计算触碰点
# sensor.compute_contact(min_magnitude=0.1,expansion_threshold=0.01,denoise_sigma=15)
# 步骤 2.3: 计算触碰点
sensor.compute_contact()
#计算后读取信息
#步骤 2.4: 读取信息
fps, flow, vnormal, img, contour, centroid, depth_map = sensor.read_info(
sensor.info.FPS,
sensor.info.VRAW,
@@ -34,20 +46,23 @@ def main():
sensor.info.CENTROID,
sensor.info.DEPTH
)
#步骤 3: 可视化显示
# 步骤 3.1: 绘制形变场
arrows = orisys.util.draw_arrows(
img, # 使用 copy() 避免修改原图
vnormal,
img,
flow,
threshold=5,
grid_spacing=10,
arrow_scale=1.0
)
cv2.imshow("形变场 Deformation", arrows)
# 在图像上触碰点和区域
# 步骤 3.2: 在图像上触碰点和区域
image_with_foe = orisys.util.draw_contact(img, contour, centroid)
cv2.imshow("Contact Centroid", image_with_foe)
# 步骤 3.3: 绘制深度图
# 归一化散度值到0-255范围(使用绝对值,然后归一化)
div_abs = depth_map
if div_abs.max() > div_abs.min():
@@ -58,12 +73,10 @@ def main():
# 应用颜色映射(使用JET:蓝色=低散度,红色=高散度)
cv2.imshow("Depth Map", cv2.applyColorMap(div_normalized, cv2.COLORMAP_JET))
#步骤 3.4: 打印资料
print(f"fps: {fps}")
# =====================================================================
# 4. 键盘控制
# =====================================================================
#步骤 3.5: 键盘控制
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
print("\n退出程序...")
@@ -74,10 +87,9 @@ def main():
print("光流追踪器已重置")
# =========================================================================
# 5. 清理资源
# 4. 清理资源
# =========================================================================
sensor.disconnect() # 释放视频源和相关资源
cv2.destroyAllWindows()
if __name__ == '__main__':
-45
View File
@@ -1,45 +0,0 @@
import os
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
import cv2
import orisys
def main():
global start_time
box = []
dx = 90
dy = -95
for i in range(15):
box.append((410+dx, 900+dy, 620+dx, 1110+dy))
# 打开视频,手动设置box 的坐标
sensor1 = orisys.Sensor("./s1.mp4",isstitch=False,cuda = True,verbose = True, override=True, box=box)
# sensor1 = orisys.Sensor("./uv_2.mp4",isstitch=True,cuda = True,verbose = True, override=True, box=box)
# 打开摄像头
# sensor1 = Sensor(0,isstitch=True,cuda = True,verbose = True)
while True:
sensor1.get_img() # 提取并拼接复眼图像
sensor1.compute_deformation() # 计算形变和分解
# 提取信息:
# 向量场: VRAW, VNORMAL, VSHEAR: 原始,法向,切向
# 帧率,时间戳: FPS, TIMESTAMP
# 变形量总和: FNORMAL , FSHEARX, FSHEARY : 法向,切向(X),切向(Y)
fps, fn, fx, fy, flow = sensor1.read_info(sensor1.info.FPS, sensor1.info.FNORMAL, sensor1.info.FSHEARX, sensor1.info.FSHEARY, sensor1.info.VRAW)
print(fps,fn,fx,fy)
# 显示向量场
arrows = orisys.util.draw_arrows(sensor1.img, flow, threshold=5, grid_spacing=40, arrow_scale=1.0)
cv2.imshow("Deformation", arrows)
key = cv2.waitKey(1)
if key & 0xFF == ord("q"):
print("\n 停止程序")
break
if key & 0xFF == ord("r"):
sensor1.reset()
sensor1.disconnect() # 断开传感器连接
if __name__ == '__main__':
main()
+3 -3
View File
@@ -27,9 +27,9 @@ class Stitcher:
self.nominal_ov_pos=config_stitcher["nominal_ov_pos"]
self.margin=config_stitcher["margin"]
self.bound=self.nominal_ov_pos - self.margin
# for sensor 1
self.pos_default = config_stitcher["pos_default"]
if self.verbose:
print(f"Stitcher initialized with config: {self.config_stitcher}")
def stitch_rt_optimized(self, change_sv=False, s_mean=28., v_mean=180.):
tile_list = self.tile_list
+8 -3
View File
@@ -11,19 +11,24 @@
"stitcher": {
"min_area_frac": 0.018,
"max_area_frac": 0.035,
"max_aspect_ratio": 20,
"max_aspect_ratio": 100,
"index": [1,2,3,6,7,8,11,12,13],
"pos_default": [[260, 56], [577, 62], [897, 69], [1214, 75], [1530, 81], [256, 391], [570, 397], [889, 403], [1208, 409], [1523, 416], [248, 724], [563, 730], [882, 738], [1201, 743], [1516, 751]],
"margin": 10,
"nominal_ov_pos": 100,
"n": [3,3]
},
"tracker": {
"adaptive": true,
"lower_threshold": 12.0,
"upper_threshold": 18.0,
"loop_closure_interval": 1,
"dis_preset": "medium"
},
"contact": {
"min_contour_area": 3000,
"flow_threshold":1,
"div_threshold":0.02,
"expansion_threshold": 0.01,
"downsample_factor": 1,
"kernel_size": [5,5],
"denoise_sigma": 15
}
+1 -1
View File
@@ -19,7 +19,7 @@
"min_contour_area": 3000,
"min_magnitude": 1.0,
"expansion_threshold": 0.01,
"downsample_factor": 1,
"contact_ds_factor": 10,
"kernel_size": [5,5],
"denoise_sigma": 15
}
+1 -2
View File
@@ -19,7 +19,7 @@ class Contact3D:
self.config_contact = config_contact
self.flow_threshold = config_contact["flow_threshold"]
self.expansion_threshold = config_contact["expansion_threshold"]
self.downsample_factor = config_contact["downsample_factor"]
self.downsample_factor = 1
self.kernel_size = config_contact["kernel_size"]
self.denoise_sigma = config_contact["denoise_sigma"]
self.min_contour_area = config_contact["min_contour_area"]
@@ -75,7 +75,6 @@ class Contact3D:
return result
def compute_foe(self, flow_field, return_all_contours=False):
# def compute_foe(self, flow_field, min_magnitude=1.0, expansion_threshold=1, downsample_factor=1, return_all_contours=False, kernel_size=(5,5), denoise_sigma=10):
"""
计算光流场的扩张焦点(Focus of Expansion)。
+27 -13
View File
@@ -53,16 +53,16 @@ class Sensor:
# Initialization
# =========================================================================
def __init__(self, vid_src, isstitch=False, verbose=True, cuda=True,
config_name="prototype", cal_path=None, override=False, box=None,mirror=True):
def __init__(self, vid_src, cuda=True, isstitch=True, verbose=True,
config_name="prototype", cal_path=None, override=False, box=None, mirror=True):
"""
Initialize Sensor with video source and configuration.
Args:
vid_src: Video source (int for camera ID, str for file path)
cuda: Enable CUDA acceleration (default: True)
isstitch: Enable stitching mode for multi-tile sensor (default: False)
verbose: Print initialization logs (default: True)
cuda: Enable CUDA acceleration (default: True)
config_name: Configuration name (default: "prototype")
cal_path: Path to calibration file (default: None)
override: Skip auto-detection, use provided box (default: False)
@@ -89,9 +89,9 @@ class Sensor:
# State variables
self.stitch_valid = False
self.imgupdate = False
self.vupdate = False
self.stitch_calibration = []
self.compute_deformation_valid = False
self.compute_contact_valid = False
# Data Variables
self.__img = None
@@ -193,7 +193,6 @@ class Sensor:
self.__img_filtered = alpha * img + (1 - alpha) * self.__img_filtered
self.__img = self.__img_filtered.astype(np.uint8)
self.__imgupdate = True
if log_timing:
timings['total'] = (time.perf_counter() - t_start) * 1000
@@ -210,6 +209,8 @@ class Sensor:
Returns:
np.ndarray: Optical flow field
"""
if self.__img is None:
raise Exception("Warning: Get image first")
if log_timing:
timings = {}
t_start = time.perf_counter()
@@ -245,9 +246,7 @@ class Sensor:
# Update state
self.__flow = flow
self.imgupdate = False
self.vupdate = True
self.compute_deformation_valid = True
# FPS calculation
self._fps = 1.0 / (time.perf_counter() - self.last_log_time)
self.last_log_time = time.perf_counter()
@@ -258,8 +257,13 @@ class Sensor:
return True
def compute_contact(self, multiple_foe=False, min_contour_area=3000, min_magnitude=1.0, expansion_threshold=0.01, downsample_factor=1, kernel_size=(5,5), denoise_sigma=15):
def compute_contact(self):
multiple_foe=False
if not self.compute_deformation_valid:
raise Exception("Warning: Compute deformation first")
if multiple_foe:
result = self.__contact3d.compute_multiple_foe(self.__vnormal)
else:
@@ -274,6 +278,7 @@ class Sensor:
self.__contour = result['contour']
self.__depth_map = result['divergence_map']
self.compute_contact_valid = True
return True
@@ -307,9 +312,18 @@ class Sensor:
Returns:
Tuple: Requested data in order
"""
if not self.vupdate:
print("Warning: Call compute_deformation() first.")
if any(arg in [self.info.IMG] for arg in args):
if self.__img is None:
raise Exception("Warning: Get image first")
if any(arg in [self.info.VRAW, self.info.VNORMAL, self.info.VSHEAR, self.info.FNORMAL, self.info.FSHEARX, self.info.FSHEARY] for arg in args):
if not self.compute_deformation_valid:
raise Exception("Warning: Compute deformation first")
if any(arg in [self.info.CENTROID, self.info.CONTOUR, self.info.DEPTH] for arg in args):
if not self.compute_contact_valid:
raise Exception("Warning: Compute contact first")
mapping = {
# Return copies of arrays to prevent external modification of internal state
self.info.VRAW: lambda: self.__flow.copy() if self.__flow is not None else np.array([]),
-113
View File
@@ -75,62 +75,6 @@ def draw_arrows(image, optical_flow,fps=True, threshold=5, grid_spacing=40, arro
cv2.arrowedLine(image, (x_axis[i], y_axis[i]), (end_x[i], end_y[i]), color, thickness=thickness, tipLength=0.3)
return image
def draw_magnitude_map(image, flow_field, threshold=3.0, colormap=cv2.COLORMAP_JET,downsample_factor=10):
"""
Draw color map of pointwise magnitude of velocity field.
Args:
info_type: Type of flow field to visualize (VRAW, VNORMAL, VSHEAR, or None for total)
threshold: Minimum magnitude to display (below threshold shows as zero/blue)
colormap: OpenCV colormap to use (default: COLORMAP_JET)
Returns:
np.ndarray: 400x400 BGR image with colored magnitude map
"""
# Compute magnitude at each point
magnitude = np.sqrt(flow_field[:, :, 0]**2 + flow_field[:, :, 1]**2)
# Smooth by downsampling and interpolating back
h, w = magnitude.shape
magnitude_downsampled = magnitude[::downsample_factor, ::downsample_factor]
magnitude_smoothed = cv2.resize(magnitude_downsampled, (w, h), interpolation=cv2.INTER_CUBIC)
# Apply threshold - set below-threshold values to zero
magnitude_thresholded = magnitude_smoothed.copy()
magnitude_thresholded[magnitude_smoothed < threshold] = 0
# Normalize to 0-255 range
mag_min, mag_max = magnitude_thresholded.min(), magnitude_thresholded.max()
if mag_max > mag_min:
magnitude_norm = ((magnitude_thresholded - mag_min) / (mag_max - mag_min) * 255).astype(np.uint8)
else:
magnitude_norm = np.zeros_like(magnitude_thresholded, dtype=np.uint8)
# Apply colormap
magnitude_colored = cv2.applyColorMap(magnitude_norm, colormap)
return magnitude_colored
def draw_fps(image, fps):
"""
Draw FPS counter on the top left corner of the image.
"""
font_scale=0.5
thickness=2
color=(0, 255, 0)
image = image.copy()
# Draw FPS text
fps_text = f"FPS: {fps:.1f}"
position = (10, 30) # Top left corner with margin
font = cv2.FONT_HERSHEY_SIMPLEX
# Draw FPS text
cv2.putText(image, fps_text, position, font, font_scale, color, thickness, cv2.LINE_AA)
return image
def draw_contact(image, contour, centroid, point_color=(0, 255, 0), region_color=(0, 255, 255)):
"""
@@ -159,60 +103,3 @@ def draw_contact(image, contour, centroid, point_color=(0, 255, 0), region_color
cv2.circle(image, (cx, cy), 10, point_color, 2)
return image
def draw_multiple_foe(image, foe_result, point_color=(0, 255, 0),
region_color=(0, 255, 255), label_colors=None):
"""
在图像上绘制多个扩张焦点和扩张区域。
Args:
image: 背景图像
foe_result: compute_multiple_foe 函数的返回结果
point_color: 默认焦点点的颜色 (BGR)
region_color: 扩张区域轮廓的颜色 (BGR)
label_colors: 可选的颜色列表,用于不同焦点的标签颜色
Returns:
np.ndarray: 绘制了多个焦点和区域的图像
"""
image = image.copy()
# 默认标签颜色(彩虹色系)
if label_colors is None:
label_colors = [
(255, 0, 0), # 蓝色
(0, 255, 0), # 绿色
(0, 0, 255), # 红色
(255, 255, 0), # 青色
(255, 0, 255), # 洋红色
(0, 255, 255), # 黄色
(255, 128, 0), # 橙色
(128, 0, 255) # 紫色
]
# 绘制扩张区域轮廓
if 'expansion_regions' in foe_result and foe_result['expansion_regions']:
cv2.drawContours(image, foe_result['expansion_regions'], -1, region_color, 2)
# 绘制焦点点并添加标签
if 'foe_points' in foe_result:
for i, (cx, cy) in enumerate(foe_result['foe_points']):
# 选择颜色(循环使用颜色列表)
color = label_colors[i % len(label_colors)]
# 绘制焦点点
cv2.circle(image, (cx, cy), 8, color, -1)
cv2.circle(image, (cx, cy), 10, color, 2)
# 添加标签
label = f"FOE{i+1}"
cv2.putText(image, label, (cx + 15, cy),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
# 显示检测到的焦点数量
foe_count = len(foe_result.get('foe_points', []))
cv2.putText(image, f"FOE Count: {foe_count}", (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
return image