-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_KTH.py
108 lines (93 loc) · 3.23 KB
/
read_KTH.py
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
# source: https://github.com/tejaskhot/KTH-Dataset/blob/master/prepare_data.py
import os
import sys
import subprocess
import shutil
import errno
def make_directory(file_name):
try:
os.mkdir(file_name)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
pass
data_path = '/media/peter/Elements/recognition_of_human_action_data'
video_files=os.listdir(data_path + '/data/videos')
video_files.sort()
# extract frames from video clips
# args=['ffmpeg', '-i']
# for video in video_files:
# print(video)
# video_name = video[:-11] # remove '_uncomp.avi' from name
# # print 'video name is: ', video_name
# frame_name = 'frame_%d.jpg' # count starts from 1 by default
# # os.makedirs(data_path + '/data/frames/'+video_name)
# make_directory(data_path + '/data/frames/'+video_name)
# args.append(data_path + '/data/videos/'+video)
# args.append(data_path + '/data/frames/'+video_name+'/'+frame_name)
# ffmpeg_call = ' '.join(args)
# # print ffmpeg_call
# # print args
# subprocess.call(ffmpeg_call, shell=True) # execute the system call
# args=['ffmpeg', '-i']
# if (video_files.index(video) + 1) % 50 == 0:
# print 'Completed till video : ', (video_files.index(video) + 1)
# print '[MESSAGE] Frames extracted from all videos'
# make_directory(data_path + '/data/' + 'TRAIN')
# make_directory(data_path + '/data/' + 'VALIDATION')
# make_directory(data_path + '/data/' + 'TEST')
train = [11, 12, 13, 14, 15, 16, 17, 18]
validation =[19, 20, 21, 23, 24, 25, 1, 4]
test = [22, 2, 3, 5, 6, 7, 8, 9, 10]
# read file line by line and strip new lines
lines = [line.rstrip('\n').rstrip('\r') for line in open('valid_frames.txt')]
# remove blank entries i.e. empty lines
lines = filter(None, lines)
# split by tabs and remove blank entries
lines = [filter(None, line.split('\t')) for line in lines]
lines.sort()
success_count=0
error_count=0
for line in lines:
vid = line[0].strip(' ')
subsequences = line[-1].split(',')
person = int(vid[6:8])
if person in train:
move_to = 'TRAIN'
elif person in validation:
move_to = 'VALIDATION'
else:
move_to = 'TEST'
for seq in subsequences:
try:
limits=seq.strip(' ').split('-')
seq_path=data_path + '/data/' + move_to + '/' + vid + '_frame_' + limits[0] + '_' + limits[1]
make_directory(seq_path)
except:
print '-----------------------------------------------------------'
print '[ERROR MESSAGE]: '
print 'limits : ', limits
print 'seq_path : ', seq_path
print '-----------------------------------------------------------'
continue
error_flag=False
for i in xrange(int(limits[0]), int(limits[1])+1):
src = data_path + '/data' + '/frames/' + vid + '/frame_' + str(i) + '.jpg'
# print i, src, limits
dst = seq_path
try:
shutil.copy(src, dst)
except Exception as inst:
error_flag = True
print(type(inst))
print(inst.args)
print(inst)
if error_flag:
print "[ERROR]: ", seq_path
error_count+=1
if (lines.index(line) + 1) % 50 == 0:
print 'Completed till video : ', (lines.index(line) + 1)
success_count+=1
print '[ALERT] Total error count is : ', error_count
print '[MESSAGE] Data split into train, validation and test'
# move the