File size: 2,606 Bytes
15bc41b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import cv2
import esim_py


# camera = cv2.VideoCapture(0)
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)  

# # generate events from list of images and timestamps
# events_list_of_images = esim.generateFromStampedImageSequence(
#     list_of_image_files,   # list of absolute paths to images
#     list_of_timestamps     # list of timestamps in ascending order
# )

fps = cv2.CAP_PROP_FPS
ts_s = 1 / fps
ts_ns = ts_s * 1e9 # convert s to ns

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 # convert ns to milliseconds
    
    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) 
    
    # cv2.imwrite("frame.png", frame_bgr)

    # input(idx)
    #     #     t, x, y, p = event
    #     # x, y = x.astype(dtype=np.int32), y.astype(dtype=np.int32)

    #     # events = np.hstack([x[..., None], y[..., None], t[..., None], p[..., None]])
    #     # event_labels = segmentation[y, x].astype(dtype=np.uint8)

    #     # write_frame = False
    #     # show_frame = False

    #     # if write_frame or show_frame:
    #     #     ts, xs, ys, ps = event
    #     #     h, w = frame_color.shape[:2]
    #     #     event_bgr = np.zeros((h, w, 3), dtype=np.uint8)
    #     #     for x, y, p in zip(xs, ys, ps):
    #     #         event_bgr[y, x, 0 if p == -1 else 2] = 255
                
    #     #     image_path = image_paths[frame_keys[frame_index]]
    #     #     rgb_image = cv2.imread(image_path)


    # cv2.imshow("Frame", frame)
    # cv2.waitKey(1)

    idx += 1