import cv2 as cv
import datetime
import dv_processing as dv

# Open the camera
camera = dv.io.camera.open()

# Initialize visualizer instance which generates event data preview
visualizer = dv.visualization.EventVisualizer(camera.getEventResolution())

# Create the preview window
cv.namedWindow("Preview", cv.WINDOW_NORMAL)


def preview_events(event_slice):
    cv.imshow("Preview", visualizer.generateImage(event_slice))
    cv.waitKey(2)


# Create an event slicer, this will only be used events only camera
slicer = dv.EventStreamSlicer()
slicer.doEveryTimeInterval(datetime.timedelta(milliseconds=33), preview_events)

# start read loop
while camera.isRunning():
    # Get events
    events = camera.getNextEventBatch()

    # If no events arrived yet, continue reading
    if events is not None:
        slicer.accept(events)
