forked from Brain-Cog-Lab/Bullying10K
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaddata.py
204 lines (146 loc) · 5.39 KB
/
readdata.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from dv import AedatFile
import cv2,torch,os
from tqdm import tqdm
import numpy as np
import time
import cv2 as cv
def event2frame(event,index,frame):#[t,x,y,p] h w
if event is not None:
frame[index,event["polarity"],event["y"],event["x"]]=1
return frame#[n,h,w]
def readaedat4(filename):
frame=[]
whole=[]
start=0
empty=None
with AedatFile(filename) as f:
for index,e in enumerate(f["events"].numpy() ):
t=e
if t[0][0] -start>15000 and index>0:
whole.extend( [empty for i in range((t[0][0] -start)//10000-1)])
if (t[0][0] -start)%10000>5000:whole.append(empty )
whole.append(t )
start=t[0][0]
t=None
frame=np.zeros((len(whole),2,260,346),dtype=bool)
for index,e in enumerate(whole) :
if e is None :
frame=event2frame(t ,index,frame)
else:
frame= event2frame(e ,index,frame)
t=e
return frame
def readnp(filename):
frame=[]
whole=[]
start=0
empty=None
f=np.load(filename,allow_pickle=True)
for index,e in enumerate(f):
t=e
if t[0][0] -start>15000 and index>0:
whole.extend( [empty for i in range((t[0][0] -start)//10000-1)])
if (t[0][0] -start)%10000>5000:whole.append(empty )
whole.append(t )
start=t[0][0]
t=None
frame=np.zeros((len(whole),2,260,346),dtype=bool)
for index,e in enumerate(whole) :
if e is None :
frame=event2frame(t ,index,frame)
else:
frame= event2frame(e ,index,frame)
t=e
return frame
def readnplong(filename):
frame=[]
whole=[]
start=0
empty=None
f=np.load(filename,allow_pickle=True)
for index,e in enumerate(f):
t=e
if t[0][0] -start>15000 and index>0:
whole.extend( [empty for i in range((t[0][0] -start)//10000-1)])
if (t[0][0] -start)%10000>5000:whole.append(empty )
whole.append(t )
start=t[0][0]
t=None
frame=[]
for index,e in enumerate(whole) :
if e is None :
frame.append(t )
else:
frame.append(e )
t=e
return frame
def readevent(filename):
with AedatFile(filename) as f:
return [i for i in f["events"].numpy()]
def allfile(path):
fileall=[]
for curpath,dirlist,filelist in os.walk(path):
for i in filelist:
fileall.append(os.path.join(curpath, i))
return fileall
def aedat42np(root,length):
rawroot=os.path.join(root)
nproot=os.path.join("/data/datasets/Bullying10k_processed")
classes=os.listdir( rawroot)
for label,c in enumerate(classes):
craw=os.path.join(rawroot,c)
cnp=os.path.join(nproot,c)
for i in tqdm(allfile(craw)) :
s=os.path.join(cnp,*i.split("/")[-2:-1])
if not os.path.exists(s):os.makedirs(s)
s=os.path.join(s,i.split("/")[-1][:-7])
#print(s)
data=readaedat4(i)
if data.shape[0]>=length:
np.save(s,data)
def aedat42eventnp(root,length):
rawroot=os.path.join(root)
nproot=os.path.join("/data/datasets/Bullying10k_processed")
classes=os.listdir( rawroot)
for label,c in enumerate(classes):
craw=os.path.join(rawroot,c)
cnp=os.path.join(nproot,c)
for i in tqdm(allfile(craw)) :
s=os.path.join(cnp,*i.split("/")[-2:-1])
if not os.path.exists(s):os.makedirs(s)
s=os.path.join(s,i.split("/")[-1][:-7])
#print(s)
data=readevent(i)
# if data.shape[0]>=length:
np.save(s,data)
def readvideo(filename,s):
i=0
frame=None
allframe=None
with AedatFile(filename) as f:
# 获取视频帧的宽和高
w = 346
h = 260
# 获取视频总帧数和fps
fps = 24
# 视频保存
# fourcc = cv.VideoWriter_fourcc('P', 'I', 'M', '1')
# fourcc = cv.VideoWriter_fourcc(*'XVID')
# 视频编码格式
fourcc = cv.VideoWriter_fourcc(*'mp4v')
out = cv.VideoWriter(s+'.mp4', fourcc, 24, (int(w), int(h)), True)
i=0
tmp=None
for e in f["frames"] :#一张图10ms 4个组为一张
# if i >86:break
# out.write (e.image)
if i==0:
out.write(e.image)
i=e.timestamp
continue
for j in range( (e.timestamp-i)//50000):
out.write(e.image)
if (e.timestamp-i)//50000>5:break
if (e.timestamp-i)%50000>30000:
out.write(e.image)
i=e.timestamp