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))
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-1-88478ab353f7> in <module>
2 import PIL.ImageFilter
3 import io
----> 4 im = PIL.Image.open(io.BytesIO(image_recorder.image.value))
~/checkouts/readthedocs.org/user_builds/ipywebrtc/conda/latest/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode, formats)
3022 warnings.warn(message)
3023 raise UnidentifiedImageError(
-> 3024 "cannot identify image file %r" % (filename if filename else fp)
3025 )
3026
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fdeb1019620>
[7]:
im.filter(PIL.ImageFilter.BLUR)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-886fedf7a729> in <module>
----> 1 im.filter(PIL.ImageFilter.BLUR)
NameError: name 'im' is not defined
[8]:
import numpy as np
im_array = np.array(im)
im_array
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-11fff624d0a1> in <module>
----> 1 import numpy as np
2 im_array = np.array(im)
3 im_array
ModuleNotFoundError: No module named 'numpy'
[ ]: