File size: 2,427 Bytes
8f3b56b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import datetime
import json
from pathlib import Path
import os
import pickle
import shutil
import uuid

SCENE_ROOT_DIR = Path('data/scenes')
OBJECT_PICTURE_DIR = Path('data/object_pic')
SCENE_FILE_NAME_LIST = os.listdir(SCENE_ROOT_DIR)
SCENE_IMAGE_NAME = 'background.png'
SCENE_DATA_FILE_NAME = 'house_info.json'

def get_scene_dir_path(scene_file_name):
    return SCENE_ROOT_DIR / scene_file_name

def get_scene_image_path(scene_file_name):
    return SCENE_ROOT_DIR / scene_file_name / SCENE_IMAGE_NAME

def get_scene_data(scene_dir_path):
    with open(scene_dir_path / SCENE_DATA_FILE_NAME, 'r') as f:
        scene_data = json.load(f)
    return scene_data

def get_room_counter(scene_data: dict) -> dict[str, int]:
    room_type_list = [room_info['roomType'] for room_info in scene_data['rooms']]
    return {room_type: room_type_list.count(room_type) for room_type in set(room_type_list)}

TEMP_DIR = Path("tmp")
TEMP_DIR.mkdir(exist_ok=True)
MAX_DAYS = 7
SCHEDULE_COLUMNS = ["start_time", "end_time", "activity"]


def create_file_for_character(character_dict):
    name = character_dict['persona']['name'].replace(" ", "_")
    file_path = TEMP_DIR / f"{name}_{uuid.uuid4()}.pkl"
    pickle.dump(character_dict, open(file_path, 'wb'))
    return file_path

def clean_temp_dir(max_num: int=100):
    files = os.listdir(TEMP_DIR)
    if len(files) > max_num:
        oldest_file = min(files, key=lambda x: os.path.getctime(os.path.join(TEMP_DIR, x)))
        print('delete {}'.format(oldest_file))
        file_path = os.path.join(TEMP_DIR, oldest_file)
        if os.path.isdir(file_path):
            shutil.rmtree(file_path)
        elif os.path.isfile(file_path):
            os.remove(file_path)


PERSON_ROOT_PATH = Path('static/exist_characters')
CACHE_PERSON_NAME_LIST = sorted(os.listdir(PERSON_ROOT_PATH))
CACHE_PERSON_PATH_LIST = [PERSON_ROOT_PATH / name / f"{name}.pkl" for name in CACHE_PERSON_NAME_LIST]


def get_video_path():
    return TEMP_DIR / f"video_{uuid.uuid4()}.mp4"

def read_character(character_file: str|Path|dict):
    if isinstance(character_file, str) or isinstance(character_file, Path):
        with open(character_file, 'rb') as f:
            character_file = pickle.load(f)
    
    activity_base = character_file['event_base']
    character_info = character_file['persona']
    schedule = character_file['schedule']['default']
    return activity_base, character_info, schedule