123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import pandas as pd
- import re
- from pathlib import Path
- def load_detection_data(csv_path):
- """加载检测报告CSV数据"""
- df = pd.read_csv(csv_path)
- df['Camera'] = df['Image File'].apply(lambda x: re.search(r'cam_(\d+)_', x).group(1) if re.search(r'cam_(\d+)_', x) else None)
- return df[df['Object Count'] > 0] # 过滤有效检测记录
- def generate_analysis_report(df):
- """生成分析报告"""
- report = {
- 'total_detections': df.groupby('Camera')['Object Count'].sum().to_dict(),
- 'confirmed_positives': df.groupby('Camera')['Confirmed Positive'].sum().to_dict(),
- 'average_sizes': {
- cam: {
- 'width': round(group['BBox Width'].mean(), 2),
- 'height': round(group['BBox Height'].mean(), 2)
- } for cam, group in df.groupby('Camera')
- },
- 'false_positives': {
- cam: len(group[~group['Confirmed Positive']])
- for cam, group in df.groupby('Camera')
- },
- 'true_positives': {
- cam: len(group[group['Confirmed Positive']])
- for cam, group in df.groupby('Camera')
- }
- }
- return report
- def save_report(report, output_path):
- """保存分析报告"""
- with open(output_path, 'w', encoding='gbk') as f:
- f.write("摄像头检测分析报告\n=====================\n\n")
-
- # 目标数量统计
- f.write("各摄像头目标数量统计:\n")
- for cam, count in report['total_detections'].items():
- f.write(f"摄像头 {cam}: {count} 个目标\n")
-
- # 确认阳性统计
- f.write("\n确认阳性目标统计:\n")
- for cam, count in report['confirmed_positives'].items():
- f.write(f"摄像头 {cam}: {count} 个确认阳性目标\n")
-
- # 真阳性/假阳性统计
- f.write("\n真阳性/假阳性统计:\n")
- for cam in report['total_detections'].keys():
- total = report['total_detections'][cam]
- true_pos = report['true_positives'][cam]
- false_pos = report['false_positives'][cam]
- f.write(f"摄像头 {cam}:\n")
- f.write(f" 总检测数: {total}\n")
- f.write(f" 真阳性数: {true_pos} ({true_pos/total*100:.2f}%)\n")
- f.write(f" 假阳性数: {false_pos} ({false_pos/total*100:.2f}%)\n")
-
- # 平均尺寸统计
- f.write("\n目标平均尺寸统计:\n")
- for cam, sizes in report['average_sizes'].items():
- f.write(f"摄像头 {cam}: 平均宽度 {sizes['width']}px, 平均高度 {sizes['height']}px\n")
-
- # 总体统计
- total_detections = sum(report['total_detections'].values())
- total_true_positives = sum(report['true_positives'].values())
- total_false_positives = sum(report['false_positives'].values())
-
- f.write("\n总体统计:\n")
- f.write(f"总检测数: {total_detections}\n")
- f.write(f"总真阳性数: {total_true_positives} ({total_true_positives/total_detections*100:.2f}%)\n")
- f.write(f"总假阳性数: {total_false_positives} ({total_false_positives/total_detections*100:.2f}%)\n")
- if __name__ == "__main__":
- import argparse
-
- parser = argparse.ArgumentParser(description='检测报告分析工具')
- parser.add_argument('input_csv', help='输入CSV文件路径')
- parser.add_argument('--output', '-o', default=None, help='输出报告路径')
-
- args = parser.parse_args()
-
- # 设置默认输出路径
- if not args.output:
- csv_dir = Path(args.input_csv).parent
- args.output = csv_dir / "analysis_report.txt"
-
- # 执行分析流程
- df = load_detection_data(args.input_csv)
- report = generate_analysis_report(df)
- save_report(report, args.output)
-
- print(f"分析完成!报告已保存至:{args.output}")
|