mi-task/test/task-arrow/test_arrow_with_image.py
Havoc cfcdf24a4c feat(task_5): 添加箭头方向检测与运动控制
- 初始化箭头检测器并获取图像
- 根据检测到的箭头方向调整运动方向
- 更新运动参数,包括速度和步态
- 添加资源清理逻辑以确保检测器正常关闭
2025-05-13 09:44:34 +08:00

57 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import os
import sys
import cv2
import numpy as np
import argparse
# 添加父目录到路径以便能够导入utils
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils.decode_arrow import detect_arrow_direction, visualize_arrow_detection
def main():
# 创建参数解析器
parser = argparse.ArgumentParser(description='测试箭头方向检测')
parser.add_argument('image_path', help='图像文件路径')
parser.add_argument('--save', help='保存可视化结果的路径', default=None)
parser.add_argument('--show', help='显示可视化结果', action='store_true')
parser.add_argument('--debug', help='输出详细的调试信息', action='store_true')
args = parser.parse_args()
# 检查文件是否存在
if not os.path.exists(args.image_path):
print(f"错误: 文件 '{args.image_path}' 不存在")
sys.exit(1)
print(f"正在处理图像: {args.image_path}")
# 加载图像
img = cv2.imread(args.image_path)
if img is None:
print(f"错误: 无法加载图像 '{args.image_path}'")
sys.exit(1)
# 如果需要,显示原始图像
if args.debug:
cv2.imshow('Original Image', img)
cv2.waitKey(0)
# 检测箭头方向
direction = detect_arrow_direction(img)
print(f"检测到的箭头方向: {direction}")
# 如果需要,显示可视化结果
if args.show or args.save:
visualize_arrow_detection(img, args.save)
# 如果不需要显示可视化结果但需要保存
if args.save and not args.show:
print(f"可视化结果已保存到: {args.save}")
return direction
if __name__ == "__main__":
main()