Did you know that there is a python library that allows you to to capture both a webcam stream or a single webcam image? Did you know that this works on every OS? This is what I want to share in this post: A tutorial on how to use imageio to access your webcam on Linux, Windows, or MacOS that works in either a Python script or a Jupyter Notebook. No OpenCV needed 🙂
Before we begin, a caveat for Jupyter: While the notebook is displayed on your current machine, and widgets run locally, your kernel (that runs the python code) may be hosted on a remote server, docker container, virtual machine, … depending on your setup. If this is the case for you, please note that IPython can only access the remote server’s webcam, not your local one.
Installation
pip install imageio[ffmpeg]
Get a single Image/Screenshot
Get a Video as a Sequence of Frames
Full example (record a short MP4)
That’s it.
If you happen to run into problems, you have three options to ask for help:
- Ask a question on StackOverflow and tag it with imageio (I monitor the tag)
- Comment on this post
- Create a New Issue / Bug Report on GitHub.
Thanks for reading and Happy Coding!
amazing! can you tell me the location where the picture get stored?
LikeLike
Are you talking about the last example? For that, the video will be stored in your current working directory. For the others the video remains fully in-memory and you’d write it to file using something like iio.imwrite(“desired/path/to/file.png”, frame)
LikeLike
Hi.
Would love to know how to set resolution for WebCam grabbing: https://stackoverflow.com/questions/72948277/set-webcam-grabbing-resolution-with-imageio-in-python
Thank
LikeLiked by 1 person