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.
1 |
|
The stack consists of:
-
Matplotlib
to show the image, -
pyopencv
to access webcam -
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.
1 |
|
access_webcam_matplotlib_notebook.ipynb
I had so much fun, but let me Ctrl + C
it 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.
1 |
|
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.