CameraStream

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

[1]:
from ipywebrtc import CameraStream, ImageRecorder

With constraints

You can pass constraints to the camera:

[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:

[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)
[4]:
back_camera

Record images from the camera

[5]:
image_recorder = ImageRecorder(stream=camera)
image_recorder
[6]:
import PIL.Image
import PIL.ImageFilter
import io
im = PIL.Image.open(io.BytesIO(image_recorder.image.value))
[7]:
im.filter(PIL.ImageFilter.BLUR)
[7]:
_images/CameraStream_12_0.png
[8]:
import numpy as np
im_array = np.array(im)
im_array
[8]:
array([[[ 84,  76,  73, 255],
        [ 84,  76,  73, 255],
        [ 87,  80,  76, 255],
        ...,
        [ 55,  63,  65, 255],
        [ 61,  68,  70, 255],
        [ 64,  72,  73, 255]],

       [[ 84,  76,  73, 255],
        [ 86,  78,  76, 255],
        [ 86,  78,  76, 255],
        ...,
        [ 55,  62,  65, 255],
        [ 63,  71,  72, 255],
        [ 72,  79,  80, 255]],

       [[ 86,  78,  77, 255],
        [ 87,  79,  78, 255],
        [ 85,  77,  76, 255],
        ...,
        [ 60,  67,  70, 255],
        [ 66,  73,  75, 255],
        [ 70,  76,  78, 255]],

       ...,
       [[232, 255, 255, 255],
        [232, 255, 255, 255],
        [232, 255, 255, 255],
        ...,
        [ 37,  29,  30, 255],
        [ 36,  28,  29, 255],
        [ 36,  28,  29, 255]],

       [[231, 255, 255, 255],
        [231, 255, 255, 255],
        [231, 255, 255, 255],
        ...,
        [ 37,  29,  30, 255],
        [ 37,  29,  30, 255],
        [ 37,  29,  30, 255]],

       [[228, 252, 252, 255],
        [228, 252, 252, 255],
        [228, 252, 252, 255],
        ...,
        [ 36,  28,  29, 255],
        [ 37,  29,  30, 255],
        [ 37,  29,  30, 255]]], dtype=uint8)
[ ]: