-
Notifications
You must be signed in to change notification settings - Fork 9
/
file_stream.py
33 lines (27 loc) · 1019 Bytes
/
file_stream.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
import datetime
import time
from aestream import FileInput
# Reads events from the example file, specifying it's shape (640, 480)
# By default, we send the tensors to the CPU with Numpy
# - if you have a PyTorch installation with a GPU, try changing this to "cuda"
s = 0
st = time.time()
with FileInput("sample.dat", (640, 480), device="cuda", ignore_time=True) as stream:
# In this case, we read() every 1ms
interval = 0.001
c = 0
t_0 = time.time()
# Loop forever
while stream.is_streaming():
# When 1 ms passed...
if t_0 + interval <= time.time():
# Grab a tensor of the events arriving during the past 1ms
frame = stream.read(backend="torch")
# Sum up the events and increment frame counter
s += frame.sum()
c += 1
# Reset the time so we're again counting to 1ms
t_0 = time.time()
print(
f"{c} frames of 1ms each read in {time.time() - st:.3}s containing a total of {s} events"
)