10 어노테이션 데이터(한명에 해당하는 데이터)에서 행동별로 클립을 생성하는 기능
11 v21 어노테이션 버전에 대해서만 동작함.
13 anno_list (list[dict]): 어노테이션 데이터 리스트(한명에 대한 정보), action_id에 action 키가 존재 해야함, 해당 키에 해당 프레임에서의 클래스 정보가 입력되어 있어야함
14 image_ids (list(int)): 어노테이션 데이터의 이미지 아이디 리스트
15 frame_num (int): 어노테이션 데이터의 총 프레임 수
16 return (dict): 카테고리별 클래스의 클립 리스트, 클립은 클립의 시작 프레임 인덱스(start), 끝 프레임 인덱스(end), 카테고리의 클래스 번호(label), 중복방지를 위한 순번(seq)을 포함하고 있음
20 for anno
in anno_list:
21 action_label = anno[
'action_id'][
'action']
24 if action_label
not in action_list:
25 action_list.append(action_label)
29 for label
in action_list:
31 max_interval = 15
if label == 0
else 30
37 for anno
in anno_list:
38 action_label = anno[
'action_id'][
'action']
39 image_idx = image_ids.index(anno[
'image_id'])
42 if label != action_label:
46 if len(idx_list[-1]) == 0:
47 idx_list[-1].append(image_idx)
50 if image_idx - idx_list[-1][-1] < max_interval:
51 idx_list[-1].append(image_idx)
53 idx_list.append([image_idx])
56 for i, idxs
in enumerate(idx_list):
59 start, end = idxs[0], idxs[-1]
68 clip_len = end - start
69 margin = max( (90 - clip_len)//2, 30)
70 start = max(0, start - margin)
71 end = min(frame_num-1, max(start + 60, end + margin))
78 clip = dict(start=start, end=end, label=label, seq=i)
79 clip_list.append(clip)
109 어노테이션 데이터(한명에 해당하는 데이터)에서 행동별로 클립을 생성하는 기능
111 anno_list (list[dict]): 어노테이션 데이터 리스트(한명에 대한 정보), 데이터에는 카테고리별 클래스가 설정된 'action_id'키를 포함하여야함
112 image_ids (list(int)): 어노테이션 데이터의 이미지 아이디 리스트
113 frame_num (int): 어노테이션 데이터의 총 프레임 수
114 action_train_frame (int): 행동 인식의 학습 데이터 생성에 사용되는 프레임 수
115 pose_train_frame (int): 포즈 인식의 학습 데이터 생성에 사용되는 프레임 수
116 return (dict): 카테고리별 클래스의 클립 리스트, 클립은 클립의 시작 프레임 인덱스(start), 끝 프레임 인덱스(end), 카테고리의 클래스 번호(label), 중복방지를 위한 순번(seq)을 포함하고 있음
121 cat_list=[
'action_upper',
'action_lower',
'pose',
'hand',
'foot']
123 cat_info[cat] = dict()
125 cat_info[
'action_upper'][
'info'] = cvt_labelmap_v22_action_upper
126 cat_info[
'action_lower'][
'info'] = cvt_labelmap_v22_action_lower
127 cat_info[
'pose'][
'info'] = cvt_labelmap_v22_action_pose
128 cat_info[
'hand'][
'info'] = cvt_labelmap_v22_action_hand
129 cat_info[
'foot'][
'info'] = cvt_labelmap_v22_action_foot
130 cat_info[
'action_upper'][
'train_frame'] = action_train_frame
131 cat_info[
'action_lower'][
'train_frame'] = action_train_frame
132 cat_info[
'pose'][
'train_frame'] = pose_train_frame
133 cat_info[
'hand'][
'train_frame'] = pose_train_frame
134 cat_info[
'foot'][
'train_frame'] = pose_train_frame
154 clip_info[cat] = dict()
155 label_list = label_info[cat]
156 for label
in label_list:
157 clip_info[cat][label] = [[]]
161 for anno
in anno_list:
162 image_idx = image_ids.index(anno[
'image_id'])
166 label = anno[
'action_id'][cat]
168 if len(clip_info[cat][label][-1]) == 0:
169 clip_info[cat][label][-1].append(image_idx)
171 if image_idx - clip_info[cat][label][-1][-1] < clip_interval:
172 clip_info[cat][label][-1].append(image_idx)
174 clip_info[cat][label].append([image_idx])
185 clip_list[cat] = dict()
186 label_list = label_info[cat]
187 for label
in label_list:
188 clip_list[cat][label] = []
191 for cat, cat_clip_info
in clip_info.items():
192 for label, clip_frame_list
in cat_clip_info.items():
195 if label == -1
or label
is None:
200 for frame_list
in clip_frame_list:
203 start, end = frame_list[0], frame_list[-1]
206 clip_len = end - start +1
207 if clip_len < cat_info[cat][
'info'][label][3]:
211 if cat_info[cat][
'info'][label][4]:
213 margin = max( (90 - clip_len)//2, 30)
214 start = max(0, start - margin)
215 end = min(frame_num-1, max(start + 60, end + margin))
218 if (end - start +1) < cat_info[cat][
'train_frame']:
222 clip = dict(start=start, end=end, label=label, seq=seq)
223 clip_list[cat][label].append(clip)
229def save_clip_v21(clip_list, input_anno_list, image_ids, base_anno, save_folder, json_name):
231 한사람에 대해 행동별로 생성된 클립을 저장하는 기능
232 v21 어노테이션 버전에 대해서만 동작함
234 clip_list (list[dict]): 카테고리별 클래스의 클립정보(한명에 대한 정보), 클립의 시작, 끝, 클래스 저장되어 있음
235 input_anno_list (list[dict]): 한사람에 대한 어노테이션 데이터
236 image_ids (list[str]): 이미지 아이디 리스트
237 base_anno : 어노테이션 데이터 원본, 파일을 읽었을때의 초기 상태
238 save_folder (str): 클립 데이터를 저장할 폴더
239 json_name (str): 원본 클립의 이름
244 for i, anno
in enumerate(input_anno_list):
245 image_idx = image_ids.index(anno[
'image_id'])
246 idx_link[image_idx] = i
249 for clip
in clip_list:
250 start = clip[
'start']
252 label = clip[
'label']
257 for image_idx
in range(start, end+1):
259 anno = input_anno_list[idx_link[image_idx]]
if image_idx
in idx_link
else None
263 anno_list.append(anno)
266 base_anno[
'annotations'] = anno_list
267 base_anno[
'info'][
'start'] = start
268 base_anno[
'info'][
'end'] = end
271 track_id = anno_list[0][
'track_id']
272 new_name = json_name.replace(
'.json', f
'T{track_id:02d}L{label:02d}S{seq:02d}.json')
273 save_path = os.path.join(save_folder, f
'{label:02d}', new_name)
274 save_json(base_anno, save_path)
275 print(
' saved : ', save_path)
277def save_clip_v22(clip_list, input_anno_list, image_ids, base_anno, save_folder, json_name):
279 한사람에 대해 행동별로 생성된 클립을 저장하는 기능
281 clip_list (dict): 카테고리별 클래스의 클립정보(한명에 대한 정보)
284 '카테고리 명': {클래스 번호: [ {클립정보(start, end, lable, seq)} ]}
287 {'action_upper': {0: [{'start': 20, 'end': 60, 'label':0, 'seq':0}]},
288 'action_lower': {0: []},
290 'hand': {0: [], 1: []},
292 input_anno_list (list[dict]): 한사람에 대한 어노테이션 데이터
293 image_ids (list[str]): 이미지 아이디 리스트
294 base_anno : 어노테이션 데이터 원본, 파일을 읽었을때의 초기 상태
295 save_folder (str): 클립 데이터를 저장할 폴더
296 json_name (str): 원본 클립의 이름
301 for i, anno
in enumerate(input_anno_list):
302 image_idx = image_ids.index(anno[
'image_id'])
303 idx_link[image_idx] = i
306 for cat, label_clip_info
in clip_list.items():
307 for label, clip_list
in label_clip_info.items():
308 for clip
in clip_list:
309 start = clip[
'start']
311 label = clip[
'label']
316 for image_idx
in range(start, end+1):
318 anno = input_anno_list[idx_link[image_idx]]
if image_idx
in idx_link
else None
322 anno_list.append(anno)
325 base_anno[
'annotations'] = anno_list
326 base_anno[
'info'][
'start'] = start
327 base_anno[
'info'][
'end'] = end
330 track_id = anno_list[0][
'track_id']
331 new_name = json_name.replace(
'.json', f
'T{track_id:02d}L{label:02d}S{seq:02d}.json')
332 save_path = os.path.join(save_folder, cat, f
'{label:02d}', new_name)
333 save_json(base_anno, save_path)
334 print(
' saved : ', save_path)
336def generate_clip(json_folder, save_folder):
338 어노테이션 데이터들을 개별 행동 클립으로 분할하는 기능
340 json_folder (str): 어노테이션 파일이 저장된 폴더
341 save_folder (str): 클립 데이터를 저장할 폴더
345 json_name_list, json_path_list = search_file(json_folder,
'.json')
348 data_manager = AnnotationDataManager()
351 N= len(json_name_list)
352 for i, (json_name, json_path)
in enumerate(zip(json_name_list, json_path_list)):
353 print(f
'{i}/{N} {json_path}')
356 data_manager.load_annotation(json_path)
357 data_manager.init_annotation()
360 base_anno = copy.deepcopy(data_manager.dataset)
363 labelmap_version = int(base_anno[
'info'][
'label_map_version']*10)
364 frames = data_manager.get_number_of_image()
367 track_data = data_manager.get_track_anno_data(filter=
True)
370 for track_id, anno_list
in track_data.items():
371 if labelmap_version == 21:
372 simple_anno_list = cvt_labelmap_v21_pose_and_action(anno_list, cvt_labelmap_v21_action_to_simple_ver2, cvt_labelmap_v21_pose_to_simple_ver2)
374 save_clip_v21(clip_list, simple_anno_list, data_manager.image_ids, base_anno, save_folder, json_name)
375 elif labelmap_version == 22:
376 simple_anno_list = cvt_labelmap_v22(anno_list)
378 save_clip_v22(clip_list, anno_list, data_manager.image_ids, base_anno, save_folder, json_name)