A CameraStream is a MediaStream from an attached camera device or webcam.
from ipywebrtc import CameraStream, ImageRecorder
You can pass constraints to the camera:
camera = CameraStream(constraints=
{'facing_mode': 'user',
'audio': False,
'video': { 'width': 640, 'height': 480 }
})
camera
CameraStream(constraints={'facing_mode': 'user', 'audio': False, 'video': {'width': 640, 'height': 480}})
Or use the two convenience methods:
# 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)
back_camera
CameraStream(constraints={'audio': False, 'video': {'facingMode': 'environment'}})
image_recorder = ImageRecorder(stream=camera)
image_recorder
ImageRecorder(image=Image(value=b''), stream=CameraStream(constraints={'facing_mode': 'user', 'audio': False, …
import PIL.Image
import PIL.ImageFilter
import io
im = PIL.Image.open(io.BytesIO(image_recorder.image.value))
im.filter(PIL.ImageFilter.BLUR)
import numpy as np
im_array = np.array(im)
im_array
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)