116 lines
4.6 KiB
Python
116 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import cv2
|
|
import os
|
|
import numpy as np
|
|
import sys
|
|
|
|
# 添加项目根目录到Python路径
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
project_root = os.path.dirname(current_dir)
|
|
sys.path.append(project_root)
|
|
|
|
from utils.detect_dual_track_lines import auto_detect_dual_track_lines
|
|
|
|
def visualize_dual_track_detection():
|
|
test_dir = 'res/path/dual'
|
|
results = []
|
|
|
|
# 创建结果保存目录
|
|
result_dir = 'results/dual_track'
|
|
os.makedirs(result_dir, exist_ok=True)
|
|
|
|
for img_file in sorted(os.listdir(test_dir)):
|
|
if img_file.endswith('.png'):
|
|
img_path = os.path.join(test_dir, img_file)
|
|
print(f'\n处理图片: {img_path}')
|
|
|
|
# 读取图像
|
|
img = cv2.imread(img_path)
|
|
if img is None:
|
|
print(f' 无法读取图像: {img_path}')
|
|
continue
|
|
|
|
# 调用自动检测函数,开启观察模式
|
|
result = auto_detect_dual_track_lines(img, observe=True, delay=1000, save_log=True)
|
|
|
|
# 检查检测结果
|
|
if result[0] is not None:
|
|
center_info, left_info, right_info = result
|
|
|
|
# 创建结果图像
|
|
result_img = img.copy()
|
|
height, width = img.shape[:2]
|
|
center_x = width // 2
|
|
|
|
# 绘制左右轨迹线
|
|
left_x1, left_y1, left_x2, left_y2 = left_info["line"]
|
|
right_x1, right_y1, right_x2, right_y2 = right_info["line"]
|
|
|
|
cv2.line(result_img, (int(left_x1), int(left_y1)), (int(left_x2), int(left_y2)), (255, 0, 0), 2)
|
|
cv2.line(result_img, (int(right_x1), int(right_y1)), (int(right_x2), int(right_y2)), (0, 0, 255), 2)
|
|
|
|
# 绘制中心线
|
|
center_point = center_info["point"]
|
|
center_line_x1 = (left_x1 + right_x1) // 2
|
|
center_line_y1 = (left_y1 + right_y1) // 2
|
|
cv2.line(result_img, (center_line_x1, center_line_y1), center_point, (0, 255, 0), 2)
|
|
|
|
# 绘制图像中心线
|
|
cv2.line(result_img, (center_x, 0), (center_x, height), (0, 0, 255), 1)
|
|
|
|
# 标记中心点
|
|
cv2.circle(result_img, center_point, 10, (255, 0, 255), -1)
|
|
|
|
# 显示偏差信息
|
|
deviation = center_info["deviation"]
|
|
cv2.putText(result_img, f"偏差: {deviation}px", (10, 30),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
|
|
|
# 显示是否为石板路模式
|
|
stone_path_mode = center_info["stone_path_mode"]
|
|
mode_text = "石板路模式" if stone_path_mode else "标准模式"
|
|
cv2.putText(result_img, mode_text, (10, 60),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
|
|
|
|
# 显示检测结果
|
|
cv2.imshow("双轨迹线检测结果", result_img)
|
|
key = cv2.waitKey(0) # 等待按键
|
|
|
|
# 保存结果图像
|
|
result_path = os.path.join(result_dir, f"result_{img_file}")
|
|
cv2.imwrite(result_path, result_img)
|
|
print(f' 检测成功! 偏差: {deviation}px, 模式: {mode_text}')
|
|
print(f' 结果已保存至: {result_path}')
|
|
|
|
# 按ESC键退出
|
|
if key == 27: # ESC键
|
|
break
|
|
|
|
results.append((img_file, True, deviation, stone_path_mode))
|
|
else:
|
|
print(f' 检测失败!')
|
|
results.append((img_file, False, None, None))
|
|
|
|
# 关闭所有窗口
|
|
cv2.destroyAllWindows()
|
|
|
|
# 打印检测结果汇总
|
|
print('\n检测结果汇总:')
|
|
success_count = 0
|
|
for img_file, success, deviation, stone_path_mode in results:
|
|
status = '成功' if success else '失败'
|
|
if success:
|
|
success_count += 1
|
|
mode_text = "石板路模式" if stone_path_mode else "标准模式"
|
|
dev_info = f'偏差: {deviation}px, 模式: {mode_text}'
|
|
else:
|
|
dev_info = ''
|
|
print(f'{img_file}: {status} {dev_info}')
|
|
|
|
success_rate = (success_count / len(results)) * 100 if results else 0
|
|
print(f'\n总成功率: {success_rate:.2f}% ({success_count}/{len(results)})')
|
|
|
|
if __name__ == "__main__":
|
|
visualize_dual_track_detection() |