Shady.Video Sub-module
This submodule is not imported by default by the parent package.
Import it for its side-effect: it enables the video
property
of the Shady.Stimulus
class.
If s
is a Stimulus
instance and s.video
is equal to its
default value of None
, then saying:
s.video = 'fish.mp4'
implicitly creates a VideoSource
object with the source
property equal to 'fish.mp4'
. (If s.video
was previously
already a VideoSource
, object then its source
property is
simply updated accordingly.)
The third-party package cv2
(installable with
pip install opencv-python
) is required for video support.
Any source readable by cv2.VideoCapture
is acceptable:
use an integer to open a live camera stream, or a string to
specify a video file name.
If you want to record the video to disk while rendering, there
are two approaches. One way is to capture frames in a way that
is time- (or frame-) locked to the animation of the World
:
in this case, see the doc for the VideoRecording
class, which
can capture the content of any Stimulus
regardless of
whether its source is a VideoSource
, or even capture an entire
World
. The other way is to record a VideoSource
that is
already attached to a Stimulus
, sub-sampling frames at an
independent pace in a background thread: in this case, simply
call s.video.Record(filename)
.
If you want to record a video to disk without attaching it to
a Shady.Stimulus
, or even without having a Shady.World
running at all. Let’s say you want to record from camera 0:
from Shady.Video import VideoSource
s = VideoSource(0).Record('foo')
# ...
s.Close()
An acquisition thread will continually read frames from the
camera until you call s.Close()
. A separate recording thread
will continually write frames to the specified file (foo.avi
in this example) until you call either s.Close()
or
s.StopRecording()
.
Note that reading frames from a camera or file, capturing stimulus content into memory, and recording video frames to disk all use RAM and place a burden on the CPU—they are not intended for applications in which precise timing is critical.