def get_people_list() -> list: lifecycle_path = ['metadata', 'lifecycle'] # read json data file_name_list = ["1001.metadata-character.json"] metadata_dir = get_file_dir_path(file_name_list, lifecycle_path) json_data = json_file_input(metadata_dir) # read csv data csv_name_list = ["PeopleMappingTable.csv"] csv_data_dir = get_file_dir_path(csv_name_list, lifecycle_path) csv_data = csv_file_input(csv_data_dir) # make people_list people_list = set_people_list(json_data, csv_data) # print peopleList to json file people_list_name = ["PeopleList.1toN.json", "PeopleList.Nto1.json"] people_list_dir = get_file_dir_path(people_list_name, lifecycle_path) json_file_output(people_list_dir, people_list) # peopleList return return people_list
def get_metadata_control(opus: str = None, video_data: dict = None) -> MetadataControl: """ 'opus' or 'video_data' is required Function that return MetadataControl Class for managing "opus" metadata :param opus: opus english name :param video_data: video basic information and preprocessed data for generating metadata :return: MetadataControl Class """ video_info = None if opus is not None: pass elif video_data is not None: video_info = video_data['video_info'] opus = video_info['title_eng'] else: assert False, "need 'opus' or 'video_info' parameter" new_file = opus + '.json' origin_file = opus + '.origin.json' opus_path = get_file_dir_path(new_file, dir_route=['metadata', 'input', opus]) origin_path = get_file_dir_path(origin_file, dir_route=['metadata', 'input', opus]) if opus not in metadata.keys() or metadata[opus] is None: print('There is no opus data : {}\n' 'Now read {}'.format(opus, new_file)) if os.path.exists(opus_path): metadata[opus] = MetadataControl(in_filepath=opus_path, out_filepath=opus_path) else: print('There is no file : {}\n' 'Now create new {} from {}'.format(opus_path, new_file, origin_file)) if os.path.exists(origin_path): metadata[opus] = MetadataControl(in_filepath=origin_path, out_filepath=opus_path) else: print('There is no file : {}\n' 'Now create new {}'.format(origin_path, new_file)) if video_info is not None: metadata[opus] = MetadataControl.create_metadata_opus(video_info=video_info, out_path=opus_path) else: assert False, 'Cannot create {}.\n' \ 'Please lifecycle value : "video_info"'.format(new_file) if video_info is not None: metadata[opus].create_metadata_episode(video_info) metadata[opus].update_preprocessed_data(video_data=video_data, backup_path=origin_path) return metadata[opus]
def get_data_total(file_path): file_name, extension = os.path.splitext(file_path) metadata_dir = get_file_dir_path(file_name) # metadata 디렉토리에 있는 파일 읽기 # json > class 객체화 with open(metadata_dir + extension, 'rt', encoding="utf-8") as file: json_file = json.load(file) # json -> dict data = class_from_dict(json_file) # dict -> class(object) scenes, scene_count = data_split( data) # class로된 데이터 쪼개서 Episode별 Scene 객체들로 저장 lifecycle_data = data_rendering( scenes, scene_count) # Scene안의 plot에서 인물등장횟수를 Scene별로 체크하여 저장 data_print_to_file(metadata_dir, lifecycle_data, extension) # 인물 라이프 사이클 데이터를 파일 출력 return lifecycle_data
def get(self): lw_dict = [] lw_dir_list = get_file_dir_path(filename_new, metadata_path) for i, metadata_dir in enumerate(lw_dir_list): with open(metadata_dir, 'rt', encoding='UTF-8') as data_file: lw_dict.append(json.load(data_file)) res = reqparse.request.args if "current_time" in res.keys(): current_time = res["current_time"] list_of_data = return_metadata(current_time, lw_dict[0]) json_data = json.dumps(list_of_data, ensure_ascii=False, indent=2) else: json_data = json.dumps({"invalid": "invalid current time"}, ensure_ascii=False, indent=2) res = make_response(json_data) res.headers['Content-type'] = 'application/json' return res
def sum_metadata(result, file_name_list=None): # 합칠 json 파일들 if file_name_list is None: file_name_list = ["1001_v1_face.json", "1001_v1_action.json", "1001_v1_script.json", "1001_v3_sound.json"] file_dir_list = get_file_dir_path(file_name_list, ['metadata', 'lifecycle']) opus_obj_list = list() for file_dir in file_dir_list: file = open(file_dir, 'rt', encoding='UTF8') json_file = json.load(file) opus_obj_list.append(class_from_dict(json_file)) print(opus_obj_list) print("finishing load json") face_data, action_data, script_data, sound_data = opus_obj_list for e, episode in enumerate(result.seasons[0].episodes): if e >= 1: break # 각 에피소드 내의 sequence 배열 순환 for s, sequence in enumerate(episode.sequences): # 각 sequence 내에 scene 배열 순환 for sc, scene in enumerate(sequence.scenes): print("scene # : ", sc) # 각 scene 내에 shot 배열 순환 for sh, shot in enumerate(scene.shots): print("shot # : ", sh) result.seasons[0].episodes[e].sequences[s].scenes[sc].shots[sh] = \ set_conflict_avg(result.seasons[0].episodes[e].sequences[s].scenes[sc].shots[sh], keyframes_num=action_data.seasons[0].episodes[e].sequences[s].scenes[sc].shots[ sh].keyframes_num, faces=face_data.seasons[0].episodes[e].sequences[s].scenes[sc].shots[sh].faces, sounds=sound_data.seasons[0].episodes[e].sequences[s].scenes[sc].shots[ sh].sounds) print("end shot") result.seasons[0].episodes[e].sequences[s].scenes[sc] = \ set_conflict_avg(result.seasons[0].episodes[e].sequences[s].scenes[sc], script_scene_conflict=script_data.seasons[0].episodes[e].sequences[s].scenes[ sc].script_scene_conflict) print("end scene") return result
def get_opus_id(opus_name=None): """ read "opus_id_list.txt" and return opus_id related to opus_name if there is no opus_name in "opus_id_list.txt", create opus_id and return it """ assert opus_name is not None, "get_opus_id(): opus_name is required" opus_list_path = get_file_dir_path('opus_id_list.txt', 'metadata') with open(opus_list_path, 'rt') as f: opus_name_id_list = f.readlines() for opus_name_id in opus_name_id_list: opus_n, opus_id = opus_name_id.split() if opus_name == opus_n: return opus_id last_opus_name, last_opus_id = opus_name_id_list[-1].split() new_opus_id = str(int(last_opus_id) + 1) with open(opus_list_path, 'at') as f: f.write('\n{} {}'.format(opus_name, new_opus_id)) return new_opus_id
res = make_response(json.dumps(self.response_dict['data'], indent=2, ensure_ascii=False)) res.headers['Content-type'] = 'application/json' return res # API들을 설명하는 API api.add_resource(ApiDescription, '/') # 전처리 데이터를 저장하는 API # API to store preprocessing data api.add_resource(Preprocess, '/preprocess/json') # 메타데이터 구조 정보를 제어하는 API # API to control metadata structure information api.add_resource(Struct, '/<string:opus>/struct/json') # 메타데이터를 제어하는 API # API to control metadata api.add_resource(MetadataControlAPI, '/<string:opus>/<string:class_type>/json') # Scene, Shot 누적합을 return하는 API # API to return scene and shot cumulative sum api.add_resource(TotalSceneShotNum, '/<string:opus>/total_num') if __name__ == '__main__': opus_list_path = get_file_dir_path('opus_id_list.txt', ['metadata', 'input']) with open(opus_list_path, 'wt+') as f: opus_name_id_list = f.readlines() for line in opus_name_id_list: opus_name, _ = line.split() metadata[opus_name] = get_metadata_control(opus_name) # app.run(debug=True, host='0.0.0.0', port='15000') # 디버그 모드 실행 app.run(host='0.0.0.0', port='15000') # 외부에서 접속가능한 서버로 실행
from library.directory_control import get_file_dir_path def read_srt(srt_file_dir): srt_data = list() for srtFile in srt_file_dir: f = open(srtFile, 'r', encoding='UTF8') lines = f.readlines() f.close() srt_line = list() for idx, line in enumerate(lines): if (idx + 1) % 4 == 2: srt_line.append(line[0:12]) elif (idx + 1) % 4 == 3: srt_line.append(line[0:5]) elif (idx + 1) % 4 == 0: srt_data.append(srt_line) else: pass return srt_data if __name__ == '__main__': filename = ['misaeng.S1.E0001.srt'] file_path = get_file_dir_path(filename, ['metadata', 'lifecycle']) srtdata = read_srt(file_path) print(srtdata[0:5])
def set_type_metadata(deserial, value=1): # 계층구조 : opus > seasons > episodes > sequences > scenes > shots # seasons는 무조건 1개, episodes는 총 11개있음. type_dict = {1: "action", 2: "face", 3: "sound", 4: "script"} result = deserial if value == 1: # action metadata set for e, episode in enumerate(deserial.seasons[0].episodes): # 각 에피소드 내의 sequence 배열 순환 for s, sequence in enumerate(episode.sequences): # 각 sequence 내에 scene 배열 순환 for sc, scene in enumerate(sequence.scenes): # 각 scene 내에 shot 배열 순환 for sh, shot in enumerate(scene.shots): # 각 shot내에 keyframes_num 배열 순환 for k, keyframe in enumerate(shot.keyframes_num): # shot.keyframes_num[].keyframe_action = (str) # shot.keyframes_num[].keyframe_action_conflict = (1,2,3,4,5) # 아래에서 사용한 변수는 예시 result.seasons[0].episodes[e].sequences[s].scenes[sc].shots[sh].keyframes_num[ k].keyframe_action = 'example' result.seasons[0].episodes[e].sequences[s].scenes[sc].shots[sh].keyframes_num[ k].keyframe_action_conflict = 1 elif value == 2: # face metadata set for e, episode in enumerate(deserial.seasons[0].episodes): # 각 에피소드 내의 sequence 배열 순환 for s, sequence in enumerate(episode.sequences): # 각 sequence 내에 scene 배열 순환 for sc, scene in enumerate(sequence.scenes): # 각 scene 내에 shot 배열 순환 for sh, shot in enumerate(scene.shots): # 각 shot내에 faces변수에 Face 좌표리스트 삽입 face_list = list() # face_number : 각 shot에 나오는 face 좌표집단의 갯수 / 여기에는 예시로 3 face_number = 3 for i in range(face_number): # face_frame_num = (int) # face_time = (str) # face_xywh = list(float) # face_name = (str) # face_emotion = list(float) instance_face = Face(face_frame_num=1, face_time='00:00:00.000', face_xywh=[1.1, 1.2, 20.0, 30.0], face_name='example_name', face_emotion=[0.1, 0.5, 0.52, 0.3, 0.221, 0.12, 0.91], face_actor_name='', face_expression='', face_intensity=2.5) face_list.append(instance_face) result.seasons[0].episodes[e].sequences[s].scenes[sc].shots[sh].faces = face_list.copy() elif value == 3: srt_filename = ['misaeng.S1.E0001_2.srt'] srt_file_path = get_file_dir_path(srt_filename, ['metadata', 'lifecycle']) srt_data = read_srt(srt_file_path) srt_index = 0 # sound metadata set for e, episode in enumerate(deserial.seasons[0].episodes): if e >= 1: break # 각 에피소드 내의 sequence 배열 순환 for s, sequence in enumerate(episode.sequences): # 각 sequence 내에 scene 배열 순환 for sc, scene in enumerate(sequence.scenes): # 각 scene 내에 shot 배열 순환 for sh, shot in enumerate(scene.shots): # 각 shot내에 sounds변수에 Face 좌표리스트 삽입 sound_list = list() while True: # print(srtData[srtindex][0], shot.start_time_episode) if srt_data[srt_index][0] < shot.start_time_episode: pass elif shot.start_time_episode <= srt_data[srt_index][0] <= shot.end_time_episode: # sound_time = (str) # sound_frame_num = (str) / 소수점 2자리 # sound_conflict = (float) sound_time = srt_data[srt_index][0] sound_frame_num = get_sound_frame(sound_time) sound_conflict_value = round((float(srt_data[srt_index][1]) - 0.5) * 10.0, 3) instance_sound = Sound(sound_time=sound_time, sound_frame_num=sound_frame_num, sound_conflict=sound_conflict_value) sound_list.append(instance_sound) else: break srt_index += 1 result.seasons[0].episodes[e].sequences[s].scenes[sc].shots[sh].sounds = sound_list elif value == 4: # script metadata set for e, episode in enumerate(deserial.seasons[0].episodes): # 각 에피소드 내의 sequence 배열 순환 for s, sequence in enumerate(episode.sequences): # 각 sequence 내에 scene 배열 순환 for sc, scene in enumerate(sequence.scenes): # scenes[].script_scene_conflict = (float) # 아래에서 사용한 값은 예시 result.seasons[0].episodes[e].sequences[s].scenes[sc].script_scene_conflict = 1.0 else: print("value range : (1~4)") raise ValueError return result, type_dict[value]
""" Description Returns the frequency of each scene or episode of the characters in the video """ from flask import Flask, make_response, render_template from flask_restful import Resource, Api from library.directory_control import get_file_dir_path from utils.metadata_control import MetadataControl import json template_path = get_file_dir_path('lifecycle', 'templates') app = Flask(__name__, template_folder=template_path) api = Api(app) file_name = "1001_v3_sum.json" file_path = get_file_dir_path(file_name, ['metadata', 'lifecycle']) metadata = MetadataControl(in_filepath=file_path) data = metadata.get_life_cycle() class LifeCycle(Resource): def get(self, data_type=None): if data_type is None: api_description = '<h2>LifeCycle API Description</h2>' \ '<table border="1" style="border-collapse:collapse">' \ '<tr><td>/html</td><td>인물들의 lifecycle을 HTML 템플릿으로 출력</td></tr>' \ '<tr><td>/json</td><td>인물들의 lifecycle을 json 데이터로 출력</td></tr>' \ '</table>' res = make_response(api_description) res.headers['Content-type'] = 'text/html; charset=utf-8'
""" Description Returns metadata(time, place, script, person) corresponding to a specific time of video """ from flask import Flask, make_response, render_template from flask_restful import Resource, Api, reqparse from library.directory_control import get_file_dir_path import os import json import csv from collections import OrderedDict project_name = 'view' metadata_path = get_file_dir_path(project_name, 'metadata') template_path = get_file_dir_path(project_name, 'templates') app = Flask(__name__, template_folder=template_path, static_folder=metadata_path) api = Api(app) filename_list = os.listdir(metadata_path) filename = [file for file in filename_list if 'scenario' in file] filename_new = [file for file in filename_list if 'result' in file] file_num = len(filename) # json 파일 개수 metadata_dir_list = get_file_dir_path(filename, metadata_path) data = [{}] * file_num # scenario 전체 데이터 ACT = [{}] * file_num # 각 scenario의 ACT 전체 데이터 act_length = [0] * file_num # ACT 배열의 각 요소 별 개수 arr_keyword = [[] for _ in range(file_num)] # 각 scenario의 act별 keyword에 해당하는 dictionary 배열