Codementor Events

Webcam based image processing in Jupyter notebooks

Published Apr 09, 2018
Webcam based image processing in Jupyter notebooks

So you made an image filter and want to quickly test it? You have a Deep Learning model that detects human faces? Who best to test it on than your face through your webcam?

One such need arose for me too and I put this together. While there are two methods to achieve it, let me start by discussing the older method, which works on all versions of matplotlib.

The first piece we need is a magic command that makes the images appear in the output cells in our Jupyter notebook, rather than getting a floating window. Add this line to the top cell in your notebook.

%matplotlib inline

The stack consists of:

  1. Matplotlib to show the image,
  2. pyopencv to access webcam
  3. IPython.display to clear and redraw static images in a cell.

access_webcam_matplotlib_inline.ipynb


A better approach would be to use another magic command. This one is faster and produces a widget to show the images, letting us interact with it.

%matplotlib notebook

access_webcam_matplotlib_notebook.ipynb

I had so much fun, but let me Ctrl + Cit now, stop this thing, and go play something else. Wait, how do I stop it without it throwing an error in my face?

While capturing the KeyboardInterrupt using try and except statements is a simple way to stop an infinite loop, it doesn’t work sometimes. I often faced a C++ error thrown by opencv and I didn’t like it much.

A more robust way is to capture the SIGINT signal raised by Jupyter on interrupt. As Matt J writes here, this can be accomplished using the signal library.

import signal
import sys

def signal_handler(signal, frame): 
    print('You pressed Ctrl+C!') 
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()

Here is the complete implementation using the above idea.

access_webcam_matplotlib_notebook_sigint.ipynb

That’s all folks. Now you can work on images right from your webcam.

Discover and read more posts from Prashanth Gandhiraj
get started