Real-time display of video acquired from webcam on Jupyter notebook (Python3)

Reference (Python2)

  1. Get video from webcam with OpenCV
  2. Convert color space from BGR to RGB
  3. Get jpeg binary with PIL
  4. Display image with IPython.display.display

It seems that png is quite slow. (Refer to the reference site)

from io import BytesIO
import os

import IPython
from PIL import Image
import cv2

#Convert image to jpeg binary and display with Ipython
def show(a, fmt='jpeg'):
    f = BytesIO()
    Image.fromarray(a).save(f, fmt) # (3)
    IPython.display.display(IPython.display.Image(data=f.getvalue())) # (4)

cap = cv2.VideoCapture(0)
assert cap.isOpened(), 'Could not open video device'

try:
    while(True):
        ret, frame = cap.read() # (1)

        if ret:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # (2)
            show(frame) # (3-4)
            IPython.display.clear_output(wait=True)

except KeyboardInterrupt:
    cap.release()
    print('Stream stopped')

Recommended Posts

Real-time display of video acquired from webcam on Jupyter notebook (Python3)
Display the graph of tensorBoard on jupyter
GPU check of PC on jupyter notebook
Display histogram / scatter plot on Jupyter Notebook
Install matplotlib and display graph on Jupyter Notebook
Simply display a line graph on Jupyter Notebook
Run Tensorflow from Jupyter Notebook on Bash on Ubuntu on Windows
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
3 Jupyter notebook (Python) tricks
Display and shoot webcam video with Python Kivy [GUI]
[Memo] Display Jupyter Notebook on PC in monospaced font (Mac)
Update monitoring of dmesg (/ dev / kmsg) from python on Linux
Unable to display tensorboard in jupyter notebook on docker (solved)
View dynamic graphs in Jupyter notebook. (Inline display of D3.js)
Display HTML in Jupyter notebook
Display PIL images on Jupyter
Real-time graphs on Plotly (Python)
High charts on Jupyter notebook
View PDF on Jupyter Notebook
Handling of python on mac
Run Jupyter Notebook on windows
python3.8 venv environment jupyter notebook
Building a Python development environment on Windows -From installing Anaconda to linking Atom and Jupyter Notebook-
Resolve garbled Japanese characters in matplotlib of Jupyter Notebook on Docker
Formatting with autopep8 on Jupyter notebook
Snippet settings for python jupyter notebook
Python memo Anaconda x Jupyter Notebook
Notes on using MeCab from Python
Operate an I2C-connected display from Python
Generate Jupyter notebook ".ipynb" in Python
Existence from the viewpoint of Python
Waveform display of audio in Python
Run azure ML on jupyter notebook
Environment construction of python3.8 on mac
Notes on accessing dashDB from python
Easy to use Jupyter notebook (Python3.5)
Separate display of Python graphs (memo)
Try running Jupyter Notebook on Mac
Call C / C ++ from Python on Mac
Update Python on Mac from 2 to 3
Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win
Immediate display, forced display, flash of print output result with python (mainly jupyter)
Display the result of video analysis using Cloud Video Intelligence API from Colaboratory.
Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)
Settings when reading S3 files with pandas from Jupyter Notebook on AWS
From re-environment construction of Python to graph drawing (on visual studio code)
Build a "Deep learning from scratch" learning environment on Cloud9 (jupyter miniconda python3)