-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv2img.py
119 lines (107 loc) · 4.47 KB
/
csv2img.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
import os
import pdb
import csv
import numpy as np
import cv2
import torch
import pandas as pd
from tqdm import tqdm
data_path = '/media/oem/xiao_20T/CelexHAR_benchmark/csv_files/'
save_path = '/media/oem/xiao_20T/CelexHAR_benchmark/event2img_splited'
#action_004_20220219_101957781_EI_70M
if __name__=='__main__':
cls_dirs = os.listdir(data_path)
for cls_ID in range(len(cls_dirs)):
cls = cls_dirs[cls_ID]
# print(videoName[7:10])
fileLIST = os.listdir(os.path.join(data_path,cls))
save_cls_path = os.path.join(save_path,cls)
if not os.path.exists(save_cls_path):
os.makedirs(save_cls_path)
for FileID in tqdm(range(len(fileLIST))):
csv_Name = fileLIST[FileID]
video_save_path = os.path.join(save_cls_path,csv_Name.split('.')[0])
if os.path.exists(video_save_path):
continue
if not os.path.exists(video_save_path):
os.makedirs(video_save_path)
read_path = os.path.join(data_path,cls,csv_Name)
#action_003_20220215_172542477_0_E_100M.csv
if '_E_' in csv_Name and len(csv_Name.split('_')[-3])>2:
recordMODE = "E"
dt = pd.read_csv(read_path, dtype=np.int32, delimiter=",",usecols=(0, 1, 2)) ## all_events.shape (36267950, 5)
dt = np.array(dt)
dt = torch.tensor(dt, dtype=torch.int)
p = torch.ones(dt.shape[0])
p = p.unsqueeze(1)
p = p.to(torch.int)
y, x, t = torch.chunk(dt, 3, dim=1)
all_events = torch.cat((x, y, p, t), dim=1)
elif '_E_' in csv_Name and len(csv_Name.split('_')[-3])<=2:
recordMODE = "E"
dt = pd.read_csv(read_path, dtype=np.int32, delimiter=",", usecols=(0, 1, 2, 3) )
dt = np.array(dt)
dt = torch.tensor(dt, dtype=torch.int)
x, y, p, t = torch.chunk(dt, 4, dim=1)
all_events = torch.cat((x, y, p, t), dim=1)
elif '_EI_' in csv_Name and len(csv_Name.split('_')[-3])>2:
recordMODE = "EI"
dt = pd.read_csv(read_path, dtype=np.int32, delimiter=",", usecols=(0, 1, 3, 4) )
dt = np.array(dt)
dt = torch.tensor(dt, dtype=torch.int)
# dt = dt.to(device)
y, x, p, t = torch.chunk(dt, 4, dim=1)
mask = torch.where(p != 0)
t = t[mask].unsqueeze(1)
x = x[mask].unsqueeze(1)
y = y[mask].unsqueeze(1)
p = p[mask].unsqueeze(1)
all_events = torch.cat((x, y, p, t), dim=1)
else:
recordMODE = "EI"
dt = pd.read_csv(read_path, dtype=np.int32, delimiter=",", usecols=(0, 1, 2, 3) )
dt = np.array(dt)
dt = torch.tensor(dt, dtype=torch.int)
x, y, p, t = torch.chunk(dt, 4, dim=1)
all_events = torch.cat((x, y, p, t), dim=1)
all_events = all_events.numpy()
frameRATE = 30
width,height = 1280,800
finalTIME_stamp = int(all_events[all_events.shape[0]-1][3])
# firstTIME_stamp = int(all_events[all_events.shape[0]][4])
time_length = all_events[-1,3] - all_events[0,3]
eventMODnum = 50000
frameNUM = int(5000000//33333)
start_idx = []
# deltaT = 33333 #finalTIME_stamp//33333
deltaT = int(time_length/30)
i = 1
for j in range(len(all_events)):
if all_events[j][-1]-all_events[0][-1] > deltaT * i:
start_idx.append(j)
i += 1
################################################################################
### save the Event Image
################################################################################
start_time_stamp = 0
saved_event_timeStamp = []
count_csvsample = 0
count_IMG = 0
assert len(start_idx)!=0,'{} get 0 img!'.format(csv_Name)
for imgID in range(len(start_idx)-1):
event_frame = 255 * np.ones((height, width, 3), dtype=np.uint8)
start_time_stamp = start_idx[imgID]
end_time_stamp = start_idx[imgID+1]
saved_event_timeStamp.append([start_time_stamp, end_time_stamp])
if recordMODE == "E":
event = all_events[start_time_stamp:end_time_stamp]
event_frame[height - 1 - event[:, 1], event[:, 0], :] = [10, 10, 255] * 1
cv2.imwrite(os.path.join(video_save_path, '{:04d}'.format(imgID+1)+'.png'), event_frame)
else:
event = all_events[start_time_stamp:end_time_stamp]
on_idx = np.where(event[:, 2] == 1)
off_idx = np.where(event[:, 2] == -1)
event_frame[height - 1 - event[:, 1][on_idx], event[:, 0][on_idx], :] = [10, 10, 255] * event[:, 2][on_idx][:, None]
event_frame[height - 1 - event[:, 1][off_idx], event[:, 0][off_idx], :] = [120, 255, 255] * (event[:, 2][off_idx])[:, None]
cv2.imwrite(os.path.join(video_save_path, '{:04d}'.format(count_IMG)+'.png'), event_frame)
count_IMG += 1