LensCraft / data /loader.py
abreza's picture
Initial commit
bdf752e
import json
from typing import Optional, Dict, Any, List, Tuple
def create_instruction_description(instruction: Dict[str, Any]) -> str:
parts = []
movement = instruction.get('cameraMovement')
shot_type = instruction.get('initialShotType')
frames = instruction.get('frameCount')
subject_idx = instruction.get('subjectIndex')
if movement and shot_type:
parts.append(f"{movement} {shot_type}")
elif movement:
parts.append(f"{movement} shot")
elif shot_type:
parts.append(f"Static {shot_type}")
if subject_idx is not None:
parts.append(f"of subject {subject_idx}")
if frames is not None:
parts.append(f"lasting {frames} frames")
if not parts:
return "No camera instruction details available"
return " ".join(parts)
def load_simulation_data(file) -> Tuple[Optional[List[Dict[str, Any]]], Optional[List[str]]]:
if file is None:
return None, None
try:
json_data = json.load(open(file.name))
simulations = json_data['simulations']
descriptions = []
for i, sim in enumerate(simulations):
header = f"Simulation {i + 1}"
instruction_texts = []
for j, instruction in enumerate(sim['instructions']):
inst_desc = create_instruction_description(instruction)
instruction_texts.append(f" {j + 1}. {inst_desc}")
full_description = f"{header}\n" + "\n".join(instruction_texts)
subject_count = len(sim['subjects'])
full_description = f"{full_description}\n ({subject_count} subjects)"
descriptions.append(full_description)
return simulations, descriptions
except Exception as e:
print(f"Error loading simulation data: {str(e)}")
return None, None