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
+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