-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_vcdb_dataset.py
More file actions
131 lines (96 loc) · 3.7 KB
/
prepare_vcdb_dataset.py
File metadata and controls
131 lines (96 loc) · 3.7 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
import pandas as pd
import os
from sklearn.model_selection import train_test_split
import shutil
import torch
import torch.utils.data as data
from PIL import Image
import numpy as np
from numpy.random import randint
'''
Load videos and generate the data.csv as format [video_name,classIDx]
Argus:
The root video path
Returns:
data.csv file
'''
def generate_data(video_path):
# load category file
label_file = r'data\vcdb\category.csv'
df = pd.read_csv(label_file)
data = list()
for item in df.itertuples():
sub_path = item.class_name
IDx = item.classIDx
vid_path = os.path.join(video_path, sub_path)
# Note: This would include all the files and directories
directory_list = os.listdir(vid_path)
for file in directory_list:
case = {'video_name':file, 'classIDx':IDx}
data.append(case)
df = pd.DataFrame(data)
output_file = r'data\vcdb\data.csv'
df.to_csv(output_file, columns=['video_name', 'classIDx'], header=True, index=False)
def split_train_val_test(root_path, data_file):
groundtruth_file = os.path.join(root_path, data_file)
df = pd.read_csv(groundtruth_file)
train_size = 0.7
data = df.drop(columns = ['classIDx']).copy()
lables = df['classIDx']
x_train, x_remain, y_train, y_remain = train_test_split(data, lables, train_size= train_size)
test_size = 0.5
x_valid, x_test, y_valid, y_test = train_test_split(x_remain, y_remain, test_size = test_size)
train_ds = pd.concat([x_train, y_train], axis=1)
val_ds = pd.concat([x_valid, y_valid], axis=1)
test_ds = pd.concat([x_test, y_test], axis=1)
train_file = os.path.join(root_path, 'train.csv')
val_file = os.path.join(root_path, 'val.csv')
test_file = os.path.join(root_path, 'test.csv')
train_ds.to_csv(train_file, columns=['video_name', 'classIDx'], index=False, header=True, encoding="utf-8-sig")
val_ds.to_csv(val_file, columns=['video_name', 'classIDx'], index=False, header=True, encoding="utf-8-sig")
test_ds.to_csv(test_file, columns=['video_name', 'classIDx'], index=False, header=True, encoding="utf-8-sig")
def copy_video():
train_file = r'data\vcdb\train.csv'
val_file = r'data\vcdb\val.csv'
test_file = r'data\vcdb\test.csv'
df_train = pd.read_csv(train_file)
df_val = pd.read_csv(val_file)
df_test = pd.read_csv(test_file)
original_path = r'D:\pvcd_core'
train_path = r'D:\vcdb_split\train'
val_path = r'D:\vcdb_split\val'
test_path = r'D:\vcdb_split\test'
for item in df_train.itertuples():
video_file = item.video_name
old_path = os.path.join(original_path, video_file)
new_path = os.path.join(train_path, video_file)
shutil.copyfile(old_path, new_path)
for item in df_val.itertuples():
video_file = item.video_name
old_path = os.path.join(original_path, video_file)
new_path = os.path.join(val_path, video_file)
shutil.copyfile(old_path, new_path)
for item in df_test.itertuples():
video_file = item.video_name
old_path = os.path.join(original_path, video_file)
new_path = os.path.join(test_path, video_file)
shutil.copyfile(old_path, new_path)
def main():
'''
step 1: generate the all of video files and correspoding classIDxs
'''
root_video_path = r'D:\video\VCDB\pvcd_core'
# generate_data(root_video_path)
'''
split data for train / val / test parts
'''
root_path = r'data\vcdb'
data_file = r'data.csv'
# split_train_val_test(root_path, data_file)
'''
Copy videos to train / val / test folders
'''
# copy_video()
print()
if __name__ == '__main__':
main()