Skip to article frontmatterSkip to article content

5Usage: Python

This page describes how to use Faery in a Python script to process event-based data. Note that we assume you already installed Faery, see the installation instructions if you haven’t done so.

The Python code generally follows the same structure as the command line interface (CLI), where you have some input, some filtering/manipulation, and then an output.

import faery

input_stream = faery.SOME_INPUT(...)
filtered_stream = input_stream.SOME_FILTER(...)
filtered_stream.SOME_OUTPUT(...)

Which can be simplified to:

import faery
faery.SOME_INPUT(...) \
     .SOME_FILTER(...) \
     .SOME_OUTPUT(...)

Of course, there are many variations to this, and you can chain multiple filters and outputs together. Below, we will go through some common use cases and examples of how to use Faery in a Python script.

5.1Inputs: How to read data

Faery provides a set of input functions that lets you read event data from files, UDP streams, or other sources.

Here is a table with the available input functions:

Input FunctionDescription
faery.events_stream_from_array(...)Reads event data from a NumPy array
faery.events_stream_from_camera(...)Reads event data from a camera (e.g., Prophesee, Intel)
faery.events_stream_from_file(...)Reads event data from a file (e.g., AEDAT, RAW, DAT, ES, CSV)
faery.events_stream_from_udp(...)Reads event data from a UDP stream
faery.events_stream_from_stdin(...)Reads event data from standard input (stdin)

5.2Outputs: How to write data

Faery provides a set of output functions that lets you write event data to files, UDP streams, or other sources.

Here is a table with the available output functions:

Output FunctionDescription
stream.to_array(...)Writes event data to a NumPy array
stream.to_file(...)Writes event data to a file (e.g., AEDAT, RAW, DAT, ES, CSV)
stream.to_udp(...)Writes event data to a UDP stream
stream.to_stdout(...)Writes event data to standard output (stdout)

5.3Filters: How to process data

Faery provides a set of filter functions that lets you process event data in various ways. Note that the filters depend on the type of stream you are operating on, so we recommend you familiarize yourself with the different types of streams and their respective filters: Stream types in Faery.

Here are a few examples of filter functions for events:

Filter functionSupported stream typesDescription
stream.crop(...)EventStream, FiniteEventStream, RegularEventStream, FiniteRegularEventStreamRemoves events outside a given spatial range
stream.chunks(...)EventStream, FiniteEventStream, RegularEventStream, FiniteRegularEventStreamSplits the stream into parts of a given number of events
stream.map(...)EventStream, FiniteEventStream, RegularEventStream, FiniteRegularEventStreamApplies a function to each package in the stream
stream.time_slice(...)EventStream, FiniteEventStreamExtracts a time slice from the stream

And here are a few examples of filter functions for images:

Filter functionSupported stream typesDescription
stream.scale(...)FrameStream, FiniteFrameStream, RegularFrameStream, FiniteRegularFrameStreamScales the image to a given size
stream.annotate(...)FrameStream, FiniteFrameStream, RegularFrameStream, FiniteRegularFrameStreamAdds text annotations to the images
stream.add_timecode(...)FrameStream, FiniteFrameStream, RegularFrameStream, FiniteRegularFrameStreamAdds a textual timecode to the images
stream.add_overlay(...)FrameStream, FiniteFrameStream, RegularFrameStream, FiniteRegularFrameStreamAdds a visual overlay to the images

5.4Putting it all together

Once you get a feel for the inputs, filters, and outputs, working with Faery is easy! You can mix and match any filters and outputs as you want - if you make sure the type of the stream matches the type of operation you are doing. For instance, it doesn’t make sense to use stream.chunks(...) on a stream of frames. Luckily, Faery will let you know if you are using an unsupported operation on a stream of frames, so feel free to experiment!

Here is an example where we stream events from a file, crop them, and save the result to a CSV file:

(
    faery.events_stream_from_file(
        "dvs.es",
    )
    .crop(
        left=110,
        right=210,
        top=70,
        bottom=170,
    )
    .time_slice(
        start="00:00:00.400000",
        end="00:00:00.600000",
        zero=True,
    )
    .to_file(
        "dvs_crop_and_slice.csv",
    )
)

Here is another example where we stream events from an event camera to a UDP socket without using an intermediate filter (note that this requires the event-camera-drivers package:

(
    faery.events_stream_from_camera("Inivation")  # Open an Inivation camera
    .to_udp(
        host="localhost",
        port=5005,
    )
)

And here is an example where we stream events from a file, regularize them, render them to frames, and save the result to an MP4 video file:

(
    faery.events_stream_from_file(
        "dvs.es",
    )
    .regularize(frequency_hz=60.0)
    .render(
        decay="exponential",
        tau="00:00:00.200000",
        colormap=faery.colormaps.managua.flipped(),
    ) # This is now a stream of frames!
    .to_file("dvs.mp4")
)