Skip to content

8 ‐ Chaining

Balint Laczko edited this page Feb 4, 2022 · 3 revisions

A simple workflow for using the Musical Gestures Toolbox can be the following:

    1. Creating an MgVideo which loads a video file and optionally applies some preprocessing to it.
    1. Calling a process on the MgVideo.
    1. Viewing the result.

Something like this:

source = musicalgestures.MgVideo('/path/to/source/video.avi', starttime=5, endtime=15, skip=3) # load video, apply preprocessing
source.motion() # call a process on it
source.show(key='motion') # view the result

This is convenient if you want to apply several different processes on the same input video.

source = musicalgestures.MgVideo('/path/to/source/video.avi', starttime=5, endtime=15, skip=3) # load and preprocess video
source.motion() # render a motion video from the source video
source.history() # render a history video from the source video
source.average() # render an average image from the source video

Simple chaining

The Musical Gestures Toolbox also offers an alternative workflow in case you want to apply a process on the result of a previous process. Although show() is not really a process (ie. it does not yield a file as a result) it can provide a good example of the use of chaining:

# this...
source.motion().show()

# ...is the equivalent of this!
source.motion()
source.show(key='motion')

It also works with images:

source.average().show()

One-liners

But chaining can go further than this. How about reading (and preprocessing) a video, rendering its motion video, the motion history and the average of the motion history, with showing the _motion_history_average.png at the end - all as a one-liner?!

musicalgestures.MgVideo('/path/to/source/video.avi', skip=4, crop='auto').motion().history().average().show()

# equivalent without chaining
source = musicalgestures.MgVideo('/path/to/source/video.avi', skip=4, crop='auto')
motionvideo = source.motion()
motionhistory = motionvideo.history()
motionhistory.average()
motionhistory.show(key='average')

Examples

# rendering and viewing the motion video 
musicalgestures.MgVideo('/path/to/source/video.avi', skip=4).motion().show()

# rendering the motion video, the motion history video, and viewing the latter
musicalgestures.MgVideo('/path/to/source/video.avi', skip=3).motion().history(normalize=True).show()

# rendering the motion video, the motion average image, and viewing the latter
musicalgestures.MgVideo('/path/to/source/video.avi', skip=15).motion().average().show()