DynamicScene/DynamicGeneration
8
1import datetime2import json3from pathlib import Path4import os5import pickle6import shutil7import uuid8 9SCENE_ROOT_DIR = Path('data/scenes')10OBJECT_PICTURE_DIR = Path('data/object_pic')11SCENE_FILE_NAME_LIST = os.listdir(SCENE_ROOT_DIR)12SCENE_IMAGE_NAME = 'background.png'13SCENE_DATA_FILE_NAME = 'house_info.json'14 15def get_scene_dir_path(scene_file_name):16 return SCENE_ROOT_DIR / scene_file_name17 18def get_scene_image_path(scene_file_name):19 return SCENE_ROOT_DIR / scene_file_name / SCENE_IMAGE_NAME20 21def get_scene_data(scene_dir_path):22 with open(scene_dir_path / SCENE_DATA_FILE_NAME, 'r') as f:23 scene_data = json.load(f)24 return scene_data25 26def get_room_counter(scene_data: dict) -> dict[str, int]:27 room_type_list = [room_info['roomType'] for room_info in scene_data['rooms']]28 return {room_type: room_type_list.count(room_type) for room_type in set(room_type_list)}29 30TEMP_DIR = Path("tmp")31TEMP_DIR.mkdir(exist_ok=True)32MAX_DAYS = 733SCHEDULE_COLUMNS = ["start_time", "end_time", "activity"]34 35 36def create_file_for_character(character_dict):37 name = character_dict['persona']['name'].replace(" ", "_")38 file_path = TEMP_DIR / f"{name}_{uuid.uuid4()}.pkl"39 pickle.dump(character_dict, open(file_path, 'wb'))40 return file_path41 42def clean_temp_dir(max_num: int=100):43 files = os.listdir(TEMP_DIR)44 if len(files) > max_num:45 oldest_file = min(files, key=lambda x: os.path.getctime(os.path.join(TEMP_DIR, x)))46 print('delete {}'.format(oldest_file))47 file_path = os.path.join(TEMP_DIR, oldest_file)48 if os.path.isdir(file_path):49 shutil.rmtree(file_path)50 elif os.path.isfile(file_path):51 os.remove(file_path)52 53 54PERSON_ROOT_PATH = Path('static/exist_characters')55CACHE_PERSON_NAME_LIST = sorted(os.listdir(PERSON_ROOT_PATH))56CACHE_PERSON_PATH_LIST = [PERSON_ROOT_PATH / name / f"{name}.pkl" for name in CACHE_PERSON_NAME_LIST]57 58 59def get_video_path():60 return TEMP_DIR / f"video_{uuid.uuid4()}.mp4"61 62def read_character(character_file: str|Path|dict):63 if isinstance(character_file, str) or isinstance(character_file, Path):64 with open(character_file, 'rb') as f:65 character_file = pickle.load(f)66 67 activity_base = character_file['event_base']68 character_info = character_file['persona']69 schedule = character_file['schedule']['default']70 return activity_base, character_info, schedule71 