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.
- Jupyter Lab - Run
- 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 fromIPython.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 fromIPython.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 index0
).- If you have multiple cameras, you can change
0
to1
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
isFalse
, 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
- Opens the webcam.
- Continuously captures frames.
- Resizes frames for efficiency.
- Converts frames to JPEG format.
- Displays frames in a Jupyter Notebook.
- Clears previous frames for real-time updates.
- 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
- When
show_webcam()
is called, Python executes the function. - Inside
show_webcam()
, the webcam is opened using OpenCV (cv2.VideoCapture(0)
). - A loop starts, continuously capturing frames from the webcam.
- 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.
- Is resized to
- The loop continues indefinitely until manually stopped (e.g., by pressing
Ctrl + C
). - If an interruption occurs (
KeyboardInterrupt
), it prints"Webcam stopped"
. - 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
Post a Comment