CameraStream

A CameraStream is a MediaStream from an attached camera device or webcam.

In [1]:
from ipywebrtc import CameraStream, ImageRecorder

With constraints

You can pass constraints to the camera:

In [2]:
camera = CameraStream(constraints=
                      {'facing_mode': 'user',
                       'audio': False,
                       'video': { 'width': 640, 'height': 480 }
                       })
camera

Front and back camera

Or use the two convenience methods:

In [3]:
# this is a shorter way to get the user facing camera
front_camera = CameraStream.facing_user(audio=False)
# or the back facing camera
back_camera = CameraStream.facing_environment(audio=False)
In [4]:
back_camera

Record images from the camera

In [5]:
image_recorder = ImageRecorder(stream=camera)
image_recorder
In [6]:
import PIL.Image
import PIL.ImageFilter
import io
im = PIL.Image.open(io.BytesIO(image_recorder.get_record().value))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-5288f20d8ec1> in <module>()
      2 import PIL.ImageFilter
      3 import io
----> 4 im = PIL.Image.open(io.BytesIO(image_recorder.get_record().value))

AttributeError: 'ImageRecorder' object has no attribute 'get_record'
In [7]:
im.filter(PIL.ImageFilter.BLUR)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-886fedf7a729> in <module>()
----> 1 im.filter(PIL.ImageFilter.BLUR)

NameError: name 'im' is not defined
In [8]:
import numpy as np
im_array = np.array(im)
im_array
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-8-11fff624d0a1> in <module>()
----> 1 import numpy as np
      2 im_array = np.array(im)
      3 im_array

ModuleNotFoundError: No module named 'numpy'