mi-task/test/test_image.py
2025-08-18 11:06:42 +08:00

83 lines
2.4 KiB
Python
Executable File

import cv2
import os
import sys
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 detect_dual_track_lines, auto_detect_dual_track_lines
# 图片路径
# image_path = "res/path/image_20250514_024347.png"
image_path = "logs/res/3/dual_track_orig_20250531_060458_547428.jpg"
# 确保图片存在
if not os.path.exists(image_path):
print(f"图片 {image_path} 不存在!")
exit(1)
# 先尝试普通模式
print("正在使用普通模式检测...")
center_info, left_track_info, right_track_info = detect_dual_track_lines(
image_path,
observe=True, # 设置为True以查看处理过程和结果
delay=1000, # 增加显示时间以便观察
save_log=True,
)
if center_info:
print("\n普通模式检测结果:")
print(f"中心点: {center_info['point']}")
print(f"偏差: {center_info['deviation']:.2f}")
print(f"斜率: {center_info['slope']:.2f}")
print(f"轨道宽度: {center_info['track_width']:.2f}")
else:
print("普通模式检测失败")
exit(0)
# 再尝试石板路模式
print("\n正在使用石板路模式检测...")
center_info, left_track_info, right_track_info = detect_dual_track_lines(
image_path,
observe=True,
delay=2000,
save_log=True,
stone_path_mode=True
)
if center_info:
print("\n石板路模式检测结果:")
print(f"中心点: {center_info['point']}")
print(f"偏差: {center_info['deviation']:.2f}")
print(f"斜率: {center_info['slope']:.2f}")
print(f"轨道宽度: {center_info['track_width']:.2f}")
else:
print("石板路模式检测失败")
# 最后尝试自动检测模式
print("\n正在使用自动检测模式...")
center_info, left_track_info, right_track_info = auto_detect_dual_track_lines(
image_path,
observe=True,
delay=2000,
save_log=True
)
if center_info:
print("\n自动检测模式结果:")
print(f"中心点: {center_info['point']}")
print(f"偏差: {center_info['deviation']:.2f}")
print(f"斜率: {center_info['slope']:.2f}")
print(f"轨道宽度: {center_info['track_width']:.2f}")
print(f"使用石板路模式: {center_info['stone_path_mode']}")
else:
print("自动检测模式失败")
# 等待用户按键关闭窗口
print("\n按任意键退出...")
cv2.waitKey(0)
cv2.destroyAllWindows()