Display your live webcam feed in a Jupyter notebook using OpenCV

Arthur Bauville
1 min readJul 15, 2021

--

OpenCV is a popular and powerful computer vision library for Python. It allows the user to efficiently perform computation-intensive tasks on images and videos, including live webcam feed. By default, OpenCV displays images in its own independent window. However, it is often more practical to visualize results directly in the notebook. Here, I’ll demonstrate how to display the webcam feed in the notebook and how to set up Ipywidgets to interact with the feed.

Setup

Let’s start by importing the necessary libraries

Display the webcam in OpenCV’s own window

To start, here’s the standard way of displaying the webcam feed with OpenCV. It will open a separate window.

Display the webcam inside the notebook

Here I combine the display code proposed here with an Ipywidget button. Since the feed is updated inside a loop, the button is not updated by default, which renders it inactive. To fix that, I use the asynchronous widget update described here. Now, when clicked the button will stop the feed and remove the output.

Et voilà! The output looks like this:

webcam capture example
hmmm…. What an insightful blogpost

--

--

Responses (3)