Skip to main content

Display Live Webcam Feed in Jupyter Notebook using OpenCV and Python3 ?

Hi Guys! Welcome to the new blog. While working with OpenCV and Jupyter Notebook, have you came across with this thought that what if you will able to display your live webcam feed in a Jupyter notebook. Usually what happens is whenever you tries to run any computer vision program which requires live webcam feed. It opens in the separate window and I don't like this separate webcam window. So, I decided to write a simple python program which will help to display your live webcam feed in a Jupyter notebook using OpenCV and Python3.

So let's start and first we will talk about the prerequisites.

Prerequisites

Before we start writing the code we need to take about few prerequisites,

  • Python Interpreter - First you need to install the Python-3 interpreter, which you can download and install from python official website https://www.python.org.
  • Jupyter Lab/Notebook - Second requirement is, you need to install Jupyter Lab/Notebook IDE on your PC/Laptop. Installation instructions you can find on https://jupyter.org/install.
    • Jupyter Lab - Run pip install jupyterlab script in terminal to install the Jupyter Lab.
    • JupyterNotebook - Run pip install notebook script in terminal to install the Jupyter Notebook.
  • Webcam - Any descent quality USB webcam is will work.
  • Internet - Make sure you should have working internet so that you can install Python libraries and Jupyter Lab/Notebook IDE.
import cv2
import IPython.display as display
from IPython.display import Image

This piece of code imports necessary libraries for handling images and displaying them in a Jupyter Notebook environment.

import cv2
  • This imports the cv2 module from the OpenCV library, which is used for image processing, computer vision, and machine learning tasks.
import IPython.display as display
  • This imports the display module from IPython.display, which allows displaying rich media content, such as images, audio, and video, inside Jupyter Notebooks.
from IPython.display import Image
  • This imports the Image class from IPython.display, which is used to display images within a Jupyter Notebook.

Why Use IPython.display.Image?

  • OpenCV (cv2.imshow()) does not work directly in Jupyter Notebooks.
  • IPython.display.Image allows displaying images inline within a notebook.
def show_webcam():
    cap = cv2.VideoCapture(0)  # Open the default camera

    try:
        while True:
            ret, frame = cap.read()
            if not ret:
                break

            # Resize to speed up processing
            frame = cv2.resize(frame, (640, 480))

            # Convert frame to JPEG format for Jupyter display
            _, buffer = cv2.imencode('.jpg', frame)
            img_data = buffer.tobytes()

            # Display in Jupyter Notebook
            display.clear_output(wait=True)
            display.display(Image(data=img_data))

    except KeyboardInterrupt:
        print("Webcam stopped")

    finally:
        cap.release()

Now Let's break down above mentioned Python function line by line.

Function Definition

def show_webcam():
  • Defines a function named show_webcam() that captures and displays a real-time webcam feed in a Jupyter Notebook.

Opening the Webcam

cap = cv2.VideoCapture(0)  # Open the default camera
  • cv2.VideoCapture(0): Opens the default webcam (device index 0).
  • If you have multiple cameras, you can change 0 to 1 or another index to select a different camera.

Capturing Frames in a Loop

try:
    while True:
  • Starts an infinite loop (while True), continuously capturing frames from the webcam.
        ret, frame = cap.read()
  • cap.read(): Captures a single frame from the webcam.
  • ret: A boolean flag indicating whether the frame was successfully read.
  • frame: The actual image (numpy array).
        if not ret:
            break
  • If ret is False, it means there’s an issue capturing frames (e.g., webcam is disconnected), so the loop breaks.

Resizing the Frame

        frame = cv2.resize(frame, (640, 480))
  • Resizes the captured frame to 640x480 pixels.
  • Reducing the resolution speeds up processing and display.

Encoding the Frame as JPEG

        _, buffer = cv2.imencode('.jpg', frame)
  • cv2.imencode('.jpg', frame): Converts the image to a compressed JPEG format.
  • buffer: Stores the compressed image in memory.
        img_data = buffer.tobytes()
  • Converts the encoded image buffer into a byte array (img_data), which is needed for display in Jupyter Notebook.

Displaying the Frame in Jupyter Notebook

        display.clear_output(wait=True)
  • Clears the previous output to update the displayed frame in real-time without stacking multiple frames.
        display.display(Image(data=img_data))
  • Displays the latest webcam frame inside the Jupyter Notebook.

Handling Interruptions

except KeyboardInterrupt:
    print("Webcam stopped")
  • If the user presses Ctrl + C (or stops execution manually in Jupyter), the loop exits gracefully, and "Webcam stopped" is printed.

Releasing the Webcam Resource

finally:
    cap.release()
  • Ensures that the webcam resource is properly released when the function exits, preventing potential issues when accessing the webcam later.

In the End How It Works

  1. Opens the webcam.
  2. Continuously captures frames.
  3. Resizes frames for efficiency.
  4. Converts frames to JPEG format.
  5. Displays frames in a Jupyter Notebook.
  6. Clears previous frames for real-time updates.
  7. Stops gracefully on user interruption.

This function is specifically designed for Jupyter Notebooks, as OpenCV’s cv2.imshow() does not work inside them.

# Run the webcam function
show_webcam()

The line:

show_webcam()

simply calls the show_webcam() function, which we explained earlier.

Step-by-Step Explanation

  1. When show_webcam() is called, Python executes the function.
  2. Inside show_webcam(), the webcam is opened using OpenCV (cv2.VideoCapture(0)).
  3. loop starts, continuously capturing frames from the webcam.
  4. Each frame:
    • Is resized to 640x480 (for efficiency).
    • Is converted to JPEG format using cv2.imencode().
    • Is displayed inside a Jupyter Notebook using IPython.display.Image().
    • The previous frame is cleared to ensure smooth updates.
  5. The loop continues indefinitely until manually stopped (e.g., by pressing Ctrl + C).
  6. If an interruption occurs (KeyboardInterrupt), it prints "Webcam stopped".
  7. Finally, the webcam resource is released using cap.release().

Expected Behavior

  • Running show_webcam() will start live webcam streaming in a Jupyter Notebook.
  • It will continuously update the displayed image.
  • If you interrupt it manually, the webcam feed stops, and the program exits cleanly.

Final Code

import cv2
import IPython.display as display
from IPython.display import Image

def show_webcam():
    cap = cv2.VideoCapture(0)  # Open the default camera

    try:
        while True:
            ret, frame = cap.read()
            if not ret:
                break

            # Resize to speed up processing
            frame = cv2.resize(frame, (640, 480))

            # Convert frame to JPEG format for Jupyter display
            _, buffer = cv2.imencode('.jpg', frame)
            img_data = buffer.tobytes()

            # Display in Jupyter Notebook
            display.clear_output(wait=True)
            display.display(Image(data=img_data))

    except KeyboardInterrupt:
        print("Webcam stopped")

    finally:
        cap.release()

# Run the webcam function
show_webcam()

Output

Wrapping Up

In the End hopefully this code will help you to successfully integrate live webcam feed in a Jupyter notebook using OpenCV and Python3. Thank You!

Comments