|
import numpy as np |
|
import cv2 |
|
import esim_py |
|
|
|
|
|
|
|
camera = cv2.VideoCapture('video.mp4') |
|
|
|
POS_THRESHOLD = 0.5 |
|
NEG_THRESHOLD = 0.5 |
|
REF_PERIOD = 0.000 |
|
|
|
esim = esim_py.EventSimulator(POS_THRESHOLD, NEG_THRESHOLD, REF_PERIOD, 1e-4, True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fps = cv2.CAP_PROP_FPS |
|
ts_s = 1 / fps |
|
ts_ns = ts_s * 1e9 |
|
|
|
is_init = False |
|
idx = 0 |
|
while True: |
|
_, frame_bgr = camera.read() |
|
frame_gray = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2GRAY) |
|
frame_log = np.log(frame_gray.astype("float32") / 255 + 1e-4) |
|
height, width = frame_log.shape[:2] |
|
|
|
current_ts_ns = idx * ts_ns |
|
|
|
if not is_init: |
|
esim.init(frame_log, current_ts_ns) |
|
is_init = True |
|
idx += 1 |
|
|
|
continue |
|
|
|
events = esim.generateEventFromCVImage(frame_log, current_ts_ns) |
|
x, y, t, p = events.T |
|
t = t.astype(dtype=np.float32) * 1e-6 |
|
|
|
last_timestamp = t[-1] |
|
|
|
event_frame = np.zeros((height, width, 3), dtype=np.float32) |
|
|
|
x = x.astype(dtype=np.int32) |
|
y = y.astype(dtype=np.int32) |
|
p = p.astype(dtype=np.int32) |
|
|
|
print(idx, events.shape) |
|
|
|
if last_timestamp <= 0: |
|
continue |
|
|
|
event_frame[y, x, 1 - p] = (last_timestamp - t) / (last_timestamp - t[0]) |
|
|
|
event_frame *= 255 |
|
event_frame = event_frame.astype(dtype=np.uint8) |
|
|
|
stack = np.hstack([frame_bgr, event_frame]) |
|
cv2.imwrite(f"outputs/stack_{idx}.png", stack) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
idx += 1 |