-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdataset_preprocess.py
More file actions
131 lines (114 loc) · 5.11 KB
/
dataset_preprocess.py
File metadata and controls
131 lines (114 loc) · 5.11 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import re
import os
import cv2
import pdb
import glob
import pandas
import argparse
import numpy as np
from tqdm import tqdm
from functools import partial
from multiprocessing import Pool
def csv2dict(anno_path, dataset_type):
inputs_list = pandas.read_csv(anno_path)
if dataset_type == 'train':
broken_data = [2390]
inputs_list.drop(broken_data, inplace=True)
inputs_list = (inputs_list.to_dict()['id|folder|signer|annotation'].values())
info_dict = dict()
info_dict['prefix'] = anno_path.rsplit("/", 3)[0] + "/features/fullFrame-210x260px"
print(f"Generate information dict from {anno_path}")
for file_idx, file_info in tqdm(enumerate(inputs_list), total=len(inputs_list)):
fileid, folder, signer, label = file_info.split("|")
num_frames = len(glob.glob(f"{info_dict['prefix']}/{dataset_type}/{folder}"))
info_dict[file_idx] = {
'fileid': fileid,
'folder': f"{dataset_type}/{folder}",
'signer': signer,
'label': label,
'num_frames': num_frames,
'original_info': file_info,
}
return info_dict
def generate_gt_stm(info, save_path):
with open(save_path, "w") as f:
for k, v in info.items():
if not isinstance(k, int):
continue
f.writelines(f"{v['fileid']} 1 {v['signer']} 0.0 1.79769e+308 {v['label']}\n")
def sign_dict_update(total_dict, info):
for k, v in info.items():
if not isinstance(k, int):
continue
split_label = v['label'].split()
for gloss in split_label:
if gloss not in total_dict.keys():
total_dict[gloss] = 1
else:
total_dict[gloss] += 1
return total_dict
def resize_img(img_path, dsize='210x260px'):
dsize = tuple(int(res) for res in re.findall("\d+", dsize))
img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), cv2.IMREAD_COLOR)
img = cv2.resize(img, dsize, interpolation=cv2.INTER_LANCZOS4)
return img
def resize_dataset(video_idx, dsize, info_dict):
info = info_dict[video_idx]
img_list = glob.glob(f"{info_dict['prefix']}/{info['folder']}")
for img_path in img_list:
rs_img = resize_img(img_path, dsize=dsize)
rs_img_path = img_path.replace("210x260px", dsize)
rs_img_dir = os.path.dirname(rs_img_path)
if not os.path.exists(rs_img_dir):
os.makedirs(rs_img_dir)
cv2.imencode('.png', rs_img)[1].tofile(rs_img_path)
else:
cv2.imencode('.png', rs_img)[1].tofile(rs_img_path)
def run_mp_cmd(processes, process_func, process_args):
with Pool(processes) as p:
outputs = list(tqdm(p.imap(process_func, process_args), total=len(process_args)))
return outputs
def run_cmd(func, args):
return func(args)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Data process for Visual Alignment Constraint for Continuous Sign Language Recognition.')
parser.add_argument('--dataset', type=str, default='phoenix2014',
help='save prefix')
parser.add_argument('--dataset-root', type=str, default='../dataset/phoenix2014/phoenix-2014-multisigner',
help='path to the dataset')
parser.add_argument('--annotation-prefix', type=str, default='annotations/manual/{}.corpus.csv',
help='annotation prefix')
parser.add_argument('--output-res', type=str, default='256x256px',
help='resize resolution for image sequence')
parser.add_argument('--process-image', '-p', action='store_true',
help='resize image')
parser.add_argument('--multiprocessing', '-m', action='store_true',
help='whether adopts multiprocessing to accelate the preprocess')
args = parser.parse_args()
mode = ["dev", "test", "train"]
sign_dict = dict()
if not os.path.exists(f"./{args.dataset}"):
os.makedirs(f"./{args.dataset}")
for md in mode:
# generate information dict
information = csv2dict(f"{args.dataset_root}/{args.annotation_prefix.format(md)}", dataset_type=md)
np.save(f"./{args.dataset}/{md}_info.npy", information)
# update the total gloss dict
sign_dict_update(sign_dict, information)
# generate groudtruth stm for evaluation
generate_gt_stm(information, f"./{args.dataset}/{args.dataset}-groundtruth-{md}.stm")
# resize images
video_index = np.arange(len(information) - 1)
print(f"Resize image to {args.output_res}")
if args.process_image:
if args.multiprocessing:
run_mp_cmd(10, partial(resize_dataset, dsize=args.output_res, info_dict=information), video_index)
else:
for idx in tqdm(video_index):
run_cmd(partial(resize_dataset, dsize=args.output_res, info_dict=information), idx)
sign_dict = sorted(sign_dict.items(), key=lambda d: d[0])
save_dict = {}
for idx, (key, value) in enumerate(sign_dict):
save_dict[key] = [idx + 1, value]
np.save(f"./{args.dataset}/gloss_dict.npy", save_dict)