File size: 2,796 Bytes
bfbecd6 |
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 |
"""
convert the event file to frame (npy file).
"""
import argparse
from pathlib import Path
import numpy as np
from tqdm import tqdm
import numba
import cv2 as cv
parser = argparse.ArgumentParser(description="Convert the event file to frame.")
parser.add_argument("--input", type=str, help="the path of npz files.")
parser.add_argument("--timestamp", type=str, help="the path of timestamp files.")
parser.add_argument("--output", type=str, help="the dir of out event.")
parser.add_argument("--rgb", type=str, help="the dir of rgb frames.")
parser.add_argument("--sort_by_num", action="store_true", help="sort by number.")
parser.add_argument("--winsz", type=int, default=5, help="the window size of event frame.")
args = parser.parse_args()
@numba.jit()
def accumulate_events(xs, ys, ps, size, ev_frame = None, resolution_level=1, polarity_offset=0):
if ev_frame is None:
ev_frame = np.zeros(size, dtype=np.float32)
for i in range(len(xs)):
x, y, p = xs[i], ys[i], ps[i]
ev_frame[y // resolution_level, x // resolution_level] += (p + polarity_offset)
return ev_frame
def get_info(timestamp_path, npz_path, rgb_path, sort_by_num=False):
timestamps = np.loadtxt(timestamp_path)
npz_list = list(Path(npz_path).iterdir())
image_list = list(Path(rgb_path).iterdir())
size = cv.imread(str(image_list[0])).shape[:2]
ev_list = [str(rgb.stem) for rgb in image_list]
if sort_by_num:
ev_list = sorted(ev_list, key=lambda x: int(x))
npz_list = sorted(npz_list, key=lambda x: int(x.stem))
else:
ev_list = sorted(ev_list)
npz_list = sorted(npz_list)
return timestamps, npz_list, ev_list, size
if __name__ == "__main__":
print(args)
out_path = Path(args.output)
out_path.mkdir(parents=True, exist_ok=True)
timestamps, npz_list, ev_list, size = get_info(args.timestamp, args.input, args.rgb, args.sort_by_num)
num_cam = len(ev_list)
print(f"=== start convert {args.input}, winsz {args.winsz}, {num_cam} frames, size {size}, npzs {len(npz_list)} ===")
for i in tqdm(range(num_cam-2, num_cam-1)): # [winsz, num_cam-1]
start_time = max(0, i - args.winsz) / (num_cam - 1)
end_time = i / (num_cam - 1)
assert start_time <= end_time
start = np.searchsorted(timestamps, start_time * timestamps.max())
end = np.searchsorted(timestamps, end_time * timestamps.max())
ev_frame = np.zeros(size, dtype=np.float32)
print(f"start {start}, end {end}, {end-start}")
for npz_path in npz_list[start:end]:
print(npz_path)
ev_data = np.load(npz_path)
ev_frame = accumulate_events(ev_data["x"], ev_data["y"], ev_data["p"], size, ev_frame)
np.save(out_path / str(ev_list[i]), ev_frame) |