forked from adluu/i3dkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
177 lines (147 loc) · 6.76 KB
/
convert.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import cv2
import math
import scipy.misc
import os, sys
import imageio
#imageio.imwrite('filename.jpg', array)
data_root='/content/'
videos_root =os.path.join(data_root,'Anomaly-Videos-Part-2')
images_root=os.path.join(data_root,'images')
def make_list(split):
start=True
with open('/content/i3dkin/list/ucfcrime_list/train.list', "w") as f:
sp=open(os.path.join(data_root,'Action_Regnition_splits','train_00%d.txt'%split))
labels=open(os.path.join(data_root,'Action_Regnition_splits','ClassIDs.txt'))
labarr=[]
for line in labels:
labarr.append(line.split(' ')[0])
for line in sp:
file_dir=line.strip('\n').strip(' ')
labind=(labarr.index(line.strip('\n').strip(' ').split('/')[0]))
if not start:
f.write("\n")
else:
start=False
f.write(os.path.join(images_root,file_dir[:-4])+" %d"%labind)
f.close()
start=True
with open('/content/i3dkin/list/ucfcrime_list/test.list', "w") as f:
sp=open(os.path.join(data_root,'Action_Regnition_splits','test_00%d.txt'%split))
labels=open(os.path.join(data_root,'Action_Regnition_splits','ClassIDs.txt'))
labarr=[]
for line in labels:
labarr.append(line.split(' ')[0])
for line in sp:
file_dir=line.strip('\n').strip(' ')
labind=(labarr.index(line.strip('\n').strip(' ').split('/')[0]))
if not start:
f.write("\n")
else:
start=False
f.write(os.path.join(images_root,file_dir[:-4])+" %d"%labind)
f.close()
def extract_splits(split):
trfile=open(os.path.join(data_root,'Action_Regnition_splits','train_00%d.txt'%split))
for line in trfile.readlines():
file_dir=line.strip('\n').strip(' ')
videoFile = os.path.join(videos_root,file_dir)
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'i')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'i'))
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'x')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'x'))
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'y')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'y'))
if os.listdir(os.path.join(images_root,file_dir[:-4],'i'))==[]:
cap1 = cv2.VideoCapture(videoFile)
num=0
cap1.set(5,20)
fps = cap1.get(5)
print("extracting ", file_dir)
while(cap1.isOpened()):
frameId = cap1.get(1) #current frame number
ret, frame = cap1.read()
if (ret != True):
break
if (frameId % math.floor(fps) == 0):
save_img=os.path.join(images_root,file_dir[:-4],'i','%d.jpg'%num)
num+=1
imageio.imwrite(save_img,frame)
#scipy.misc.imsave(save_img,frame)
#filename ="test%d.jpg" % count;count+=1
#cv2.imwrite(filename, frame)
cap1.release()
tefile=open(os.path.join(data_root,'Action_Regnition_splits','test_00%d.txt'%split))
for line in tefile.readlines():
file_dir=line.strip('\n').strip(' ')
videoFile = os.path.join(videos_root,file_dir)
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'i')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'i'))
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'x')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'x'))
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'y')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'y'))
if os.listdir(os.path.join(images_root,file_dir[:-4],'i'))==[]:
cap2 = cv2.VideoCapture(videoFile)
num=0
cap2.set(5,20)
fps = cap2.get(5)
print("extracting ",file_dir)
while(cap2.isOpened()):
frameId = cap2.get(1) #current frame number
ret, frame = cap2.read()
if (ret != True):
break
if (frameId % math.floor(fps) == 0):
save_img=os.path.join(images_root,file_dir[:-4],'i','%d.jpg'%num)
num+=1
imageio.imwrite(save_img,frame)
#scipy.misc.imsave(save_img,frame)
#filename ="test%d.jpg" % count;count+=1
#cv2.imwrite(filename, frame)
cap2.release()
def extract_all():
for cls_names in os.listdir(videos_root):
cls_path=os.path.join(videos_root,cls_names)
for video_ in os.listdir(cls_path):
count = 0
file_dir = os.path.join(cls_names,video_) #in format of Label/Vidname_x264.mp4
videoFile = os.path.join(videos_root,file_dir)
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'i')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'i'))
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'x')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'x'))
if not os.path.exists(os.path.join(images_root,file_dir[:-4],'y')):
os.makedirs(os.path.join(images_root,file_dir[:-4],'y'))
if os.listdir(os.path.join(images_root,file_dir[:-4],'i'))==[]:
cap = cv2.VideoCapture(videoFile)
num=0
cap.set(5,20)
fps = cap.get(5)
print("extracting ",file_dir)
while(cap.isOpened()):
frameId = cap.get(1) #current frame number
ret, frame = cap.read()
if (ret != True):
break
if (frameId % math.floor(fps) == 0):
save_img=os.path.join(images_root,file_dir[:-4],'i','%d.jpg'%num)
num+=1
imageio.imwrite(save_img,frame)
#scipy.misc.imsave(save_img,frame)
#filename ="test%d.jpg" % count;count+=1
#cv2.imwrite(filename, frame)
cap.release()
'''def get_video_list():
video_list=[]
for cls_names in os.listdir(videos_root):
cls_path=os.path.join(videos_root,cls_names)
for video_ in os.listdir(cls_path):
video_list.append(video_)
video_list.sort()
return video_list,len(video_list)'''
extract_splits(1)
make_list(1)
#extract_all()
#vl, l=get_video_list()
#print("vl= ",vl)
#print("len= ",l)