From b169618b166f1c906867aa15e03cedba6129758e Mon Sep 17 00:00:00 2001 From: linyimin <18316471919@139.com> Date: Tue, 16 Jun 2026 15:18:38 +0800 Subject: [PATCH] fix: --- network.py | 1 + test/test_decect_circle_v4.py | 54 +++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/network.py b/network.py index a39f22d..3c8b4c9 100644 --- a/network.py +++ b/network.py @@ -2092,6 +2092,7 @@ class NetworkManager: battery_data = { "battery": battery_percent, "voltage": round(float(voltage), 3), + "netType": self.network_type, } self.safe_enqueue(battery_data, 2) self.logger.info(f"电量上报: {battery_percent}%") diff --git a/test/test_decect_circle_v4.py b/test/test_decect_circle_v4.py index abd5249..9f92c84 100644 --- a/test/test_decect_circle_v4.py +++ b/test/test_decect_circle_v4.py @@ -94,29 +94,33 @@ def detect_circle_v3(frame, laser_point=None): # 如果检测到黄色圆圈,再检测红色圆圈进行验证 if yellow_center and yellow_radius: # HSV 红色掩码检测(红色在HSV中跨越0度,需要两个范围) - # 红色范围1: 0-10度(接近0度的红色) - lower_red1 = np.array([0, 50, 40]) - upper_red1 = np.array([10, 255, 255]) + # 红色范围1: 0-12度(接近0度的红色) + # 放宽S/V阈值:S>=30, V>=20 以捕获淡红/暗红 + lower_red1 = np.array([0, 30, 20]) + upper_red1 = np.array([12, 255, 255]) mask_red1 = cv2.inRange(hsv, lower_red1, upper_red1) - # 红色范围2: 170-180度(接近180度的红色) - lower_red2 = np.array([170, 50, 40]) + # 红色范围2: 168-180度(接近180度的红色) + lower_red2 = np.array([168, 30, 20]) upper_red2 = np.array([180, 255, 255]) mask_red2 = cv2.inRange(hsv, lower_red2, upper_red2) # 合并两个红色掩码 mask_red = cv2.bitwise_or(mask_red1, mask_red2) - # 形态学操作 + # 形态学操作:先CLOSE填充空洞,再DILATE加厚环状区域 kernel_red = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) mask_red = cv2.morphologyEx(mask_red, cv2.MORPH_CLOSE, kernel_red) + mask_red = cv2.dilate(mask_red, kernel_red, iterations=1) contours_red, _ = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + red_pixel_count = np.sum(mask_red > 0) + print(f"Debug -> 红色掩码: {red_pixel_count} 像素, {len(contours_red)} 个轮廓") + found_valid_red = False if contours_red: - # 找到所有符合条件的红色圆圈 for cnt_red in contours_red: area_red = cv2.contourArea(cnt_red) perimeter_red = cv2.arcLength(cnt_red, True) @@ -126,9 +130,10 @@ def detect_circle_v3(frame, laser_point=None): else: circularity_red = 0 - # 红色圆圈也应该有一定的圆度 - if area_red > 30 and circularity_red > 0.4: - # 计算红色圆圈的中心和半径 + # 环状轮廓圆度可能偏低,放宽到0.2 + print(f"Debug -> 红轮廓: 面积={area_red:.1f}, 圆度={circularity_red:.2f}" + + f" (面积>15={area_red > 15}, 圆度>0.2={circularity_red > 0.2})") + if area_red > 15 and circularity_red > 0.2: if len(cnt_red) >= 5: (x_red, y_red), (w_red, h_red), angle_red = cv2.fitEllipse(cnt_red) radius_red = min(w_red, h_red) / 2 @@ -139,22 +144,25 @@ def detect_circle_v3(frame, laser_point=None): red_center = (int(x_red), int(y_red)) red_radius = int(radius_red) - # 计算黄色和红色圆心的距离 if red_center: dx = yellow_center[0] - red_center[0] dy = yellow_center[1] - red_center[1] distance = np.sqrt(dx * dx + dy * dy) - # 圆心距离阈值:应该小于黄色半径的某个倍数(比如1.5倍) - max_distance = yellow_radius * 1.5 + max_distance = yellow_radius * 2.0 + min_r = min(red_radius, yellow_radius) + max_r = max(red_radius, yellow_radius) + size_ratio = min_r / max_r if max_r > 0 else 0 + print(f"Debug -> 圆心距={distance:.1f}(阈值={max_distance:.1f}), " + f"大小比={size_ratio:.2f}(阈值=0.5), " + f"距离OK={distance < max_distance}, 大小OK={size_ratio > 0.5}") - # 红色圆圈应该比黄色圆圈大(外圈) - if distance < max_distance and red_radius > yellow_radius * 0.7: + # 允许红圈在黄圈外侧或内侧,只要大小相近(较小/较大 >= 0.5) + if distance < max_distance and size_ratio > 0.5: found_valid_red = True print( f"[target] -> 找到匹配的红圈: 黄心({yellow_center}), 红心({red_center}), 距离:{distance:.1f}, 黄半径:{yellow_radius}, 红半径:{red_radius}") - # 记录这个有效目标 valid_targets.append({ 'center': yellow_center, 'radius': yellow_radius, @@ -164,7 +172,17 @@ def detect_circle_v3(frame, laser_point=None): break if not found_valid_red: - print("Debug -> 未找到匹配的红色圆圈,可能是误识别") + # 如果黄圈非常可靠(大且圆),在没有红圈验证时仍接受 + if area > 30 and circularity > 0.85: + print(f"[target] -> 黄圈高置信度(面积:{area:.0f}, 圆度:{circularity:.2f}),跳过红圈验证直接接受") + valid_targets.append({ + 'center': yellow_center, + 'radius': yellow_radius, + 'ellipse': yellow_ellipse, + 'area': area + }) + else: + print("Debug -> 未找到匹配的红色圆圈,可能是误识别") # 从所有有效目标中选择最佳目标 if valid_targets: @@ -596,7 +614,7 @@ if __name__ == "__main__": # 检查文件扩展名 if any(filename.lower().endswith(ext) for ext in IMAGE_EXTENSIONS): # 过滤掉 _result.jpg 后缀的文件 - if not filename.endswith('_result.jpg'): + if filename.endswith('no_target.jpg'): filepath = os.path.join(TARGET_DIR, filename) if os.path.isfile(filepath): image_files.append(filepath)