一. 批量生成事件(通过视频序列)
- 打开终端。
$: ssim
$: roscd esim_ros
新建终端:
$: roscore
- 运行脚本 gene_event.py 生成事件 .bag文件。
import os
root_path="/media/**/Seagate\ Expansion\ Drive/OTB50/"
fileList = os.listdir('/media/**/Seagate Expansion Drive/OTB50/')
print(fileList)
# os.system('ssim')
# os.system('roscd esim_ros')
for file_ in fileList:
file_name=root_path+file_+'/img'
command1="python scripts/generate_stamps_file.py -i "+file_name+" -r 25.0"
print(command1)
os.system(command1)
command2="rosrun esim_ros esim_node \
--data_source=2 \
--path_to_output_bag={} \
--path_to_data_folder={} \
--ros_publisher_frame_rate=60 \
--exposure_time_ms=10.0 \
--use_log_image=1 \
--log_eps=0.1 \
--contrast_threshold_pos=0.15 \
--contrast_threshold_neg=0.15".format(root_path+'bag_result/'+file_+'.bag',file_name)
print(command2)
os.system(command2)
运行之后,在该文件夹下生成 bag_result文件夹,生成.bag文件存于其内。
- 运行bag_result文件夹内的脚本reader.py 将.bag转为.txt文件。 python2 reader.py
#!/usr/bin/env python
import rosbag
import os
from tqdm import tqdm
fileList = os.listdir('./')
bag_list = [n for n in fileList if 'bag' in n ]
for bag_name in bag_list:
bag=rosbag.Bag(bag_name)
with open(bag_name[:-3]+'txt','w') as f:
for i,(topic,msgs,t) in enumerate(bag.read_messages(topics=['/cam0/events'])):
for single_event in tqdm(msgs.events):
timestamp=single_event.ts.secs+single_event.ts.nsecs*1.0/1000000000
timestamp='%.9f'%(timestamp)
f.write(timestamp+' ')
f.write(str(single_event.x)+' ')
f.write(str(single_event.y)+' ')
if(single_event.polarity==True):
f.write("1")
else:
f.write("0")
f.write('\n')
print("Writing events between two pic, now is "+ str(i)+'!')
print("Writing is over!")
bag.close()
- 运行脚本,将事件数据进行堆叠
import cv2
import numpy as np
import os
def Read_Event(EventTxtFile, frame_timestamp):
#print(frame_timestamp)
Total_Event = []
Frame_Event = []
frame_len = len(frame_timestamp)
print('frame len', frame_len)
with open(EventTxtFile) as f:
frame_count = int(1)
while True:
#print('deal with image:', frame_count)
single_event = []
event_line = f.readline()
event = event_line.split()
#print(event)
if not event_line:
if current_timestamp >= frame_timestamp[-1]:
Total_Event.pop()
else:
Total_Event.append(Frame_Event)
print('break')
break
else:
current_timestamp = float(event[0])
single_event.append(int(event[1]))
single_event.append(int(event[2]))
single_event.append(float(event[0]))
single_event.append(int(event[3]))
Frame_Event.append(single_event)
#print(frame_count < frame_len, current_timestamp >= frame_timestamp[frame_count])
if(frame_count < frame_len):
if(current_timestamp >= frame_timestamp[frame_count]):
Total_Event.append(Frame_Event)
Frame_Event = []
frame_count = frame_count + 1
#return Total_Event
# print('got total event deal with image:', frame_count)
print('len of total_event', len(Total_Event))
return Total_Event
def save_event(Events, pic_shape, seq, image_name):
visual_pic = np.full(pic_shape, 255, dtype=np.uint8)
for single_event in Events:
event_x = int(round(single_event[0]))
event_y = int(round(single_event[1]))
p = int(single_event[3])
#print('=---->',event_x, event_y)
if event_x > 0 and event_y > 0 and event_x < pic_shape[1] and event_y < pic_shape[0]:
#if p == 0:
visual_pic[event_y][event_x] = int(0)
#else:
# visual_pic[event_y][event_x] = int(255)
save_path = '/data/GOT-10k/event_frame/' + seq
if not os.path.exists(save_path):
os.makedirs(save_path)
cv2.imwrite(save_path + '/' + image_name, visual_pic)
#print('write event to image:',image_name)
def pos_neg_counts(Events, pic_shape, seq, image_name):
visual_pic = np.full(pic_shape, 255, dtype=np.uint8)
pos_img = np.full(pic_shape, 0, dtype=np.float32)
neg_img = np.full(pic_shape, 0, dtype=np.float32)
for single_event in Events:
event_x = int(round(single_event[0]))
event_y = int(round(single_event[1]))
p = int(single_event[3])
if event_x > 0 and event_y > 0 and event_x < pic_shape[1] and event_y < pic_shape[0]:
#print('--------------------->no_value',p)
if p == 1 :
pos_img[event_y][event_x] = pos_img[event_y][event_x] + 1
else:
neg_img[event_y][event_x] = neg_img[event_y][event_x] + 1
#print('=---->',event_x, event_y)
# if event_x > 0 and event_y > 0 and event_x < pic_shape[1] and event_y < pic_shape[0]:
# #if p == 0:
# visual_pic[event_y][event_x] = int(0)
#else:
# visual_pic[event_y][event_x] = int(255)
save_pos_path = '/data/GOT-10k/pos_count/' + seq
save_neg_path = '/data/GOT-10k/neg_count/' + seq
if not os.path.exists(save_pos_path):
os.makedirs(save_pos_path)
if not os.path.exists(save_neg_path):
os.makedirs(save_neg_path)
# norm
pos_img = pos_img.astype(np.float32)
#pos_range = np.max(pos_img) - np.min(pos_img)
#pos_img = (pos_img - np.min(pos_img)) / pos_range
pos_img = pos_img * 255 / np.max(pos_img)
#pos_img = pos_img * 15
#neg_range = np.max(neg_img) - np.min(neg_img)
#neg_img = (neg_img - np.min(neg_img)) / neg_range
#neg_img = neg_img * 255
neg_img = neg_img * 255 / np.max(neg_img)
cv2.imwrite(save_pos_path + '/' + image_name, pos_img)
cv2.imwrite(save_neg_path + '/' + image_name, neg_img)
#print('write event to image:',image_name)
def pos_neg_timestamp(Events, pic_shape, seq, image_name):
visual_pic = np.full(pic_shape, 255, dtype=np.uint8)
pos_timestamp = np.full(pic_shape, 0, dtype=np.float32)
neg_timestamp = np.full(pic_shape, 0, dtype=np.float32)
for single_event in Events:
event_x = int(round(single_event[0]))
event_y = int(round(single_event[1]))
p = int(single_event[3])
#print('=---->',event_x, event_y)
if event_x > 0 and event_y > 0 and event_x < pic_shape[1] and event_y < pic_shape[0]:
if p == 1 :
pos_timestamp[event_y][event_x] = single_event[2]
else:
neg_timestamp[event_y][event_x] = single_event[2]
#if p == 0:
#else:
# visual_pic[event_y][event_x] = int(255)
save_pos_path = '/data/GOT-10k/pos_timestamp/' + seq
save_neg_path = '/data/GOT-10k/neg_timestamp/' + seq
if not os.path.exists(save_pos_path):
os.makedirs(save_pos_path)
if not os.path.exists(save_neg_path):
os.makedirs(save_neg_path)
# norm
#pos_range = np.max(pos_timestamp) - np.min(pos_timestamp)
#pos_timestamp = (pos_timestamp - np.min(pos_timestamp)) / pos_range
#pos_timestamp = pos_timestamp * 255
#neg_range = np.max(neg_timestamp) - np.min(neg_timestamp)
#neg_timestamp = (neg_timestamp - np.min(neg_timestamp)) / neg_range
#neg_timestamp = neg_timestamp * 255
pos_timestamp = pos_timestamp * 255 / np.max(pos_timestamp)
neg_timestamp = neg_timestamp * 255 / np.max(neg_timestamp)
cv2.imwrite(save_pos_path + '/' + image_name, pos_timestamp)
cv2.imwrite(save_neg_path + '/' + image_name, neg_timestamp)
#print('write event to image:',image_name)
def Deal_Event(EventTxtFile, frame_timestamp, pic_shape, seq, image_name):
#print(frame_timestamp)
Total_Event = []
Frame_Event = []
frame_len = len(frame_timestamp)
print('frame len', frame_len)
with open(EventTxtFile) as f:
frame_count = int(1)
while True:
#print('deal with image:', frame_count)
single_event = []
event_line = f.readline()
event = event_line.split()
#print(event)
# if not event_line:
# if current_timestamp >= frame_timestamp[-1]:
# Total_Event.pop()
# else:
# Total_Event.append(Frame_Event)
# print('break')
# break
if not event_line:
if current_timestamp <= frame_timestamp[-1]:
pos_neg_counts(Frame_Event, pic_shape, seq, image_name[frame_count])
pos_neg_timestamp(Frame_Event, pic_shape, seq, image_name[frame_count])
break
else:
current_timestamp = float(event[0].strip())
#print(current_timestamp)
single_event.append(int(event[1]))
single_event.append(int(event[2]))
single_event.append(float(event[0]))
single_event.append(int(event[3]))
Frame_Event.append(single_event)
#print(frame_count < frame_len, current_timestamp >= frame_timestamp[frame_count])
if(frame_count < frame_len):
if(current_timestamp >= frame_timestamp[frame_count]):
#Total_Event.append(Frame_Event)
if frame_count == 1:
pos_neg_counts(Frame_Event, pic_shape, seq, image_name[frame_count-1])
pos_neg_timestamp(Frame_Event, pic_shape, seq, image_name[frame_count-1])
pos_neg_counts(Frame_Event, pic_shape, seq, image_name[frame_count])
pos_neg_timestamp(Frame_Event, pic_shape, seq, image_name[frame_count])
Frame_Event = []
frame_count = frame_count + 1
#return Total_Event
print('got total event deal with image:', frame_count)
print('len of total_event', len(Total_Event))
def main():
rgb_path = '/data/GOT-10k/train/'
event_path = '/data/GOT-10k/3timestxt/'
seqlist_path = '/mnt/RENet/DATA/list.txt'
with open(seqlist_path, 'r') as fp:
seq_list = fp.read().splitlines()
for i,seq in enumerate(seq_list):
print(seq)
img_list = sorted([p for p in os.listdir(rgb_path+seq) if os.path.splitext(p)[1] == '.jpg'])
print(img_list[0])
stop_value = 0.04 * len(img_list)
frame_timestamp = np.linspace(0, stop_value, len(img_list), endpoint=True)
frame_timestamp = list(np.arange(0, stop_value, 0.04))
print(len(frame_timestamp))
first_image = cv2.imread(rgb_path+seq+'/00000001.jpg')
h = first_image.shape[0] // 3
w = first_image.shape[1] // 3
first_image = cv2.resize(first_image, (w,h))
pic_shape = first_image.shape[0:2]
#print(pic_shape)
Deal_Event(event_path+seq+'.txt', frame_timestamp, pic_shape, seq, img_list)
# Events = Read_Event(event_path+seq+'.txt', frame_timestamp, pic_shape, seq, img_list)
# for i in range(len(Events)):
# if i == 0:
# save_event(Events[i], pic_shape, seq, img_list[i])
# save_event(Events[i], pic_shape, seq, img_list[i+1])
if __name__ == "__main__":
main()
二. OpenGL Renderer