Skip to content

The webcam with p5 in isolated kernels ​

After drawing, interactions and images, Basthon's p5 module gains the webcam in isolated kernels: createCapture(VIDEO) works as with the legacy kernel. The video can be shown below the drawing, or drawn on the canvas with image.

python
from p5 import *

capture = None

def setup():
    global capture
    createCanvas(320, 240)
    capture = createCapture(VIDEO)
    # the video is drawn on the canvas, not shown below
    capture.hide()

def draw():
    background(255)
    # the size of the video is only known once the camera is open
    if capture.width > 0:
        image(capture, 0, 0, width, width * capture.height / capture.width)

run()

Play with it!

To protect students, Basthon asks for a confirmation at each call of createCapture, on top of the browser's usual permission: a program cannot turn the camera on behind its user's back. The camera turns off when the drawing is replaced by another one or when the kernel restarts.

Playing videos (createVideo) and sound (p5.sound) are still reserved to the legacy kernel for now.