#!/usr/bin/env python # coding: utf-8 # # CameraStream # A [CameraStream](api.rst#ipywebrtc.webrtc.CameraStream) is a [MediaStream](api.rst#ipywebrtc.webrtc.MediaStream) from an attached camera device or webcam. # In[1]: from ipywebrtc import CameraStream, ImageRecorder # ## With constraints # You can pass [constraints](api.rst#ipywebrtc.webrtc.CameraStream.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: # # * [CameraStream.facing_user](http://localhost:8000/api.rst#ipywebrtc.webrtc.CameraStream.facing_user) # * [CameraStream.facing_environment](http://localhost:8000/api.rst#ipywebrtc.webrtc.CameraStream.facing_environment) # 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.image.value)) # In[7]: im.filter(PIL.ImageFilter.BLUR) # In[8]: import numpy as np im_array = np.array(im) im_array # In[ ]: