|
|
import json |
|
|
import os |
|
|
from pathlib import Path |
|
|
|
|
|
class BenchmarkDataLoader: |
|
|
def __init__(self, data_root="data"): |
|
|
self.root = Path(data_root) |
|
|
self.img_dir = self.root / "images" |
|
|
self.meta_dir = self.root / "ground_truth_meta" |
|
|
self.raw_dir = self.root / "raw_models" |
|
|
|
|
|
def load_tasks_for_eval(self): |
|
|
""" |
|
|
加载用于评测的任务列表 (只读 meta 和图片) |
|
|
""" |
|
|
tasks = [] |
|
|
if not self.meta_dir.exists(): |
|
|
print(f"Warning: {self.meta_dir} does not exist. Please run tools/generate_gt.py first.") |
|
|
return [] |
|
|
|
|
|
for meta_file in self.meta_dir.glob("*.json"): |
|
|
try: |
|
|
with open(meta_file, 'r', encoding='utf-8') as f: |
|
|
meta = json.load(f) |
|
|
|
|
|
|
|
|
img_name = meta.get("image_filename") |
|
|
img_path = self.img_dir / img_name |
|
|
if not img_path.exists(): |
|
|
print(f"Skipping {meta_file.name}: Image not found at {img_path}") |
|
|
continue |
|
|
|
|
|
tasks.append({ |
|
|
"id": meta["id"], |
|
|
"difficulty": meta.get("difficulty", 1), |
|
|
"image_path": str(img_path), |
|
|
"gt_solution": meta["solution"] |
|
|
}) |
|
|
except Exception as e: |
|
|
print(f"Error loading {meta_file}: {e}") |
|
|
|
|
|
|
|
|
tasks.sort(key=lambda x: x['id']) |
|
|
|
|
|
return tasks |
|
|
|
|
|
def load_raw_models(self): |
|
|
""" |
|
|
加载原始 JSON 模型 (用于 tools/generate_gt.py 生成真值) |
|
|
""" |
|
|
models = [] |
|
|
for json_file in self.raw_dir.glob("*.json"): |
|
|
models.append({ |
|
|
"id": json_file.stem, |
|
|
"path": str(json_file), |
|
|
"filename": json_file.name |
|
|
}) |
|
|
return models |
|
|
|
|
|
def load_raw_model_by_id(self, task_id): |
|
|
""" |
|
|
[Debug模式专用] 根据 Task ID 读取原始的正确 JSON 文件 |
|
|
""" |
|
|
|
|
|
|
|
|
json_path = self.raw_dir / f"{task_id}.json" |
|
|
|
|
|
if not json_path.exists(): |
|
|
|
|
|
return None |
|
|
|
|
|
try: |
|
|
with open(json_path, 'r', encoding='utf-8') as f: |
|
|
return json.load(f) |
|
|
except Exception as e: |
|
|
print(f"Error reading raw model {json_path}: {e}") |
|
|
return None |