- Updated task_1.py to improve navigation logic and streamline movement functions. - Enhanced task_2.py with refined movement parameters for better execution and added logging for debugging. - Adjusted function calls in main.py to reflect changes in task execution flow.
106 lines
3.7 KiB
Python
106 lines
3.7 KiB
Python
import cv2
|
|
import os
|
|
import sys
|
|
import time
|
|
import argparse
|
|
|
|
# 添加父目录到系统路径
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
project_root = os.path.dirname(os.path.dirname(current_dir))
|
|
sys.path.append(project_root)
|
|
|
|
from utils.yellow_area_analyzer import analyze_yellow_area_ratio
|
|
|
|
def process_image(image_path, save_dir=None, show_steps=False):
|
|
"""处理单张图像,分析黄色区域占比"""
|
|
print(f"处理图像: {image_path}")
|
|
|
|
# 分析黄色区域占比
|
|
start_time = time.time()
|
|
yellow_ratio = analyze_yellow_area_ratio(image_path, debug=show_steps, save_result=save_dir is not None)
|
|
processing_time = time.time() - start_time
|
|
|
|
# 输出结果
|
|
print(f"处理时间: {processing_time:.3f}秒")
|
|
print(f"黄色区域占比: {yellow_ratio:.2%}")
|
|
|
|
if yellow_ratio > 0.5:
|
|
print("警告: 图像中黄色区域占比超过50%")
|
|
elif yellow_ratio > 0.3:
|
|
print("注意: 图像中黄色区域占比较高")
|
|
elif yellow_ratio < 0.05:
|
|
print("注意: 图像中几乎没有黄色区域")
|
|
|
|
print("-" * 30)
|
|
|
|
# 如果保存结果,确保目录存在
|
|
if save_dir and not os.path.exists(save_dir):
|
|
os.makedirs(save_dir)
|
|
|
|
return yellow_ratio
|
|
|
|
def process_directory(dir_path, save_dir=None, show_steps=False):
|
|
"""处理目录中的所有图像"""
|
|
print(f"处理目录: {dir_path}")
|
|
|
|
# 检查目录是否存在
|
|
if not os.path.isdir(dir_path):
|
|
print(f"错误: 目录 '{dir_path}' 不存在")
|
|
return
|
|
|
|
# 获取目录中的所有图像文件
|
|
image_extensions = ['.jpg', '.jpeg', '.png', '.bmp']
|
|
image_files = [f for f in os.listdir(dir_path)
|
|
if os.path.isfile(os.path.join(dir_path, f)) and
|
|
os.path.splitext(f)[1].lower() in image_extensions]
|
|
|
|
if not image_files:
|
|
print(f"错误: 目录 '{dir_path}' 中没有图像文件")
|
|
return
|
|
|
|
# 处理每个图像文件
|
|
results = {}
|
|
for image_file in image_files:
|
|
image_path = os.path.join(dir_path, image_file)
|
|
yellow_ratio = process_image(image_path, save_dir, show_steps)
|
|
results[image_file] = yellow_ratio
|
|
|
|
# 输出统计信息
|
|
print("=" * 50)
|
|
print(f"处理完成,共 {len(results)} 张图像")
|
|
if results:
|
|
avg_ratio = sum(results.values()) / len(results)
|
|
max_ratio = max(results.values())
|
|
min_ratio = min(results.values())
|
|
max_file = max(results, key=results.get)
|
|
min_file = min(results, key=results.get)
|
|
|
|
print(f"平均黄色区域占比: {avg_ratio:.2%}")
|
|
print(f"最大黄色区域占比: {max_ratio:.2%} (文件: {max_file})")
|
|
print(f"最小黄色区域占比: {min_ratio:.2%} (文件: {min_file})")
|
|
|
|
return results
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='黄色区域分析演示程序')
|
|
parser.add_argument('--input', type=str, default='captured_images/test/image_20250525_090252.png', help='输入图像或目录的路径')
|
|
parser.add_argument('--output', type=str, default='./results', help='输出结果的保存路径')
|
|
parser.add_argument('--show', default=True, action='store_true', help='显示处理步骤')
|
|
|
|
args = parser.parse_args()
|
|
|
|
# 检查输入路径
|
|
if not os.path.exists(args.input):
|
|
print(f"错误:路径 '{args.input}' 不存在")
|
|
return
|
|
|
|
# 根据输入类型处理
|
|
if os.path.isfile(args.input):
|
|
# 单个文件
|
|
process_image(args.input, args.output, args.show)
|
|
else:
|
|
# 目录
|
|
process_directory(args.input, args.output, args.show)
|
|
|
|
if __name__ == "__main__":
|
|
main() |