from datetime import datetime import json def parse_args(): import argparse parser = argparse.ArgumentParser(description="ODM log time") parser.add_argument( "--path", default=r"E:\datasets\UAV\1815\project\log.json") args = parser.parse_args() return args def main(args): # 读取 JSON 文件 with open(args.path, 'r') as file: data = json.load(file) # 提取 "stages" 中每个步骤的开始时间和持续时间 stage_timings = [] for i, stage in enumerate(data.get("stages", [])): stage_name = stage.get("name", "Unnamed Stage") start_time = stage.get("startTime") # 获取当前阶段的开始时间 if start_time: start_dt = datetime.fromisoformat(start_time) # 获取阶段的结束时间:可以是下一个阶段的开始时间,或当前阶段的 `endTime`(如果存在) if i + 1 < len(data["stages"]): end_time = data["stages"][i + 1].get("startTime") else: end_time = stage.get("endTime") or data.get("endTime") if end_time: end_dt = datetime.fromisoformat(end_time) duration = (end_dt - start_dt).total_seconds() stage_timings.append((stage_name, duration)) # 输出每个阶段的持续时间,调整为对齐格式 total_time = 0 print(f"{'Stage Name':<25} {'Duration (seconds)':>15}") print("=" * 45) for stage_name, duration in stage_timings: print(f"{stage_name:<25} {duration:>15.2f}") total_time += duration print('Total Time:', total_time) if __name__ == '__main__': args = parse_args() main(args)