Note

This Python sample is available with all Metavision Intelligence Plans. The corresponding C++ sample is available only with our Professional plan.

Optical Flow Sample using Python

The Python bindings of Metavision Computer Vision API can be used to compute the optical flow of objects moving in front of the camera. The optical flow is computed in a sparse way: flow information is generated on clusters of events and not for each event.

The sample metavision_sparse_optical_flow.py shows how to use the python bindings of Metavision CV SDK to implement a pipeline for computing the sparse optical flow.

Expected Output

The sample visualizes events and the output optical flow with arrows indicating direction and magnitude of motion:

How to start

To start the sample, you need to provide recorded data with the full path to a RAW file (here, we use a file from our Sample Recordings):

Linux

python3 metavision_sparse_optical_flow.py -i pedestrians.raw

Windows

python metavision_sparse_optical_flow.py -i pedestrians.raw

To check for additional options:

Linux

python3 metavision_sparse_optical_flow.py -h

Windows

python metavision_sparse_optical_flow.py -h

Code Overview

Pipeline

Metavision Optical Flow sample implements the following pipeline:

../../../../_images/flow_pipeline.png

Spatio Temporal Contrast Filter

metavision_sdk_cv.SpatioTemporalContrastAlgorithm is applied to reduce the noise in the events stream.

The filtered events are then sent to both the optical flow and frame generation stages.

Sparse Optical Flow

metavision_sdk_cv.SparseOpticalFlowAlgorithm is applied on the events stream and produces metavision_sdk_cv.EventOpticalFlow events for each internally detected events cluster. Events are clustered together based on their speed and directions: events with a similar speed and direction are clustered and flow is computed for them.

Frame Generation

metavision_sdk_cv.FlowFrameGeneratorAlgorithm generates a frame that will be used later on in the display stage to visualize the result of the metavision_sdk_cv.SparseOpticalFlowAlgorithm by rendering the estimated flows on the top of the events.

Display

When ready, the output frame is displayed on the screen:

Expected Output from Metavision Optical Flow Sample