Streaming FURY with WebRTC/MJPEG using the Widget Object#

The Widget Object simplifies the process of streaming your data visualization with WebRTC or MJPEG. Encoding. Other users can view and interact with your data visualization in real-time using a web-browser.

By default, the widget will open a local server on port 8000. With the encoding parameter you can choose between mjpeg or webrtc. WebRTC is a more robust option and can be used to perform a live streaming with a low-latency connection. However, to use webRTC you need to install the aiortc library.

pip install aiortc

In addition, if you don’t have ffmpeg installed, you need to install it.

Linux

apt install libavdevice-dev libavfilter-dev libopus-dev libvpx-dev pkg-config

OS X

brew install ffmpeg opus libvpx pkg-config

Notes#

For this example your python version should be 3.8 or greater

import asyncio
import platform
import time

import numpy as np

import fury

interactive = False
window_size = (720, 500)
N = 4
centers = np.random.normal(size=(N, 3))
colors = np.random.uniform(0.1, 1.0, size=(N, 3))
actors = fury.actor.sphere(centers, opacity=0.5, radii=0.4, colors=colors)
scene = fury.window.Scene()
scene.add(actors)
showm = fury.window.ShowManager(scene, size=(window_size[0], window_size[1]))
/opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/sphinx_gallery/gen_rst.py:722: UserWarning: We'll no longer accept the way you call the __init__ function in future versions of FURY.

Here's how to call the Function __init__: __init__(self_value, scene='value', title='value', size='value', png_magnify='value', reset_camera='value', order_transparent='value', interactor_style='value', stereo='value', multi_samples='value', max_peels='value', occlusion_ratio='value')

  exec(self.code, self.fake_main.__dict__)

Create a stream widget

widget = fury.stream.Widget(showm, port=8000)

# if you want to use webRTC, you can pass the argument to choose this encoding
# which is a more robust option.
# `widget = Widget(showm, port=8000, encoding='webrtc')`

time_sleep = 1000 if interactive else 1

If you want to use the widget in a Windows environment without the WSL you need to use the asyncio version of the widget.

if platform.system() == "Windows":

    async def main():
        widget.start()
        await asyncio.sleep(time_sleep)
        widget.stop()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
else:
    # Running the widget in a normal Python environment in Linux or MacOS
    # we can use the normal version of the widget.
    widget.start()
    time.sleep(time_sleep)
    widget.stop()

fury.window.record(showm.scene, size=window_size, out_path="viz_widget.png")
viz widget
/Users/skoudoro/devel/fury/fury/decorators.py:176: UserWarning: We'll no longer accept the way you call the start function in future versions of FURY.

Here's how to call the Function start: start(self_value, ms='value', use_asyncio='value')

  result = func(*args, **kwargs)
/Users/skoudoro/devel/fury/fury/decorators.py:158: UserWarning: We'll no longer accept the way you call the _start_fury_client function in future versions of FURY.

Here's how to call the Function _start_fury_client: _start_fury_client(self_value, use_asyncio='value')

  return func(*args, **kwargs)
url: http://localhost:7088?iframe=1&encoding=mjpeg
/opt/homebrew/Caskroom/miniforge/base/envs/py39/lib/python3.9/site-packages/sphinx_gallery/gen_rst.py:722: UserWarning: We'll no longer accept the way you call the record function in future versions of FURY.

Here's how to call the Function record: record(scene='value', cam_pos='value', cam_focal='value', cam_view='value', out_path='value', path_numbering='value', n_frames='value', az_ang='value', magnification='value', size='value', reset_camera='value', screen_clip='value', stereo='value', verbose='value')

  exec(self.code, self.fake_main.__dict__)

Total running time of the script: (0 minutes 1.595 seconds)

Gallery generated by Sphinx-Gallery