Ricardo's Place Robotics, machine learning, or simply random thoughts!

Direct access to your webcam and microphone inside Google Colab notebook

Ok, this is a straight to the point post! In previous posts I explained how to save an image directly from your webcam. However, that method was using OpenCV and it can only access hardware connected to the host (where the Jupyter notebook server is running). One classic example where you can’t access a webcam directly is Google Colaboratory. As I said at the beginning, you can only access the hardware from host, so the microphone also will not be available. Javascript to the rescue! For the first problem (capture image from your webcam) I modified something I found online (I couldn’t find the author! here it is, but it probably came from this example). You can see the final notebook below (and you should be able to test it!)

The microphone demanded something a little bit more hacky and painful, so I had to dig deeper into modern Javascript to make something that would work. It turned out I had to use ffmpeg because Chrome uses matroska/webm and I couldn’t find a way to decode it manually.

That’s it! I hope you all will be able to create some cool stuff using Google Colaboratory now.

UPDATE (17/03/2019): Apparently, it doesn’t work with iOS versions of Chrome and Firefox, and iOS Safari needed some cursor style to work.

UPDATE (26/12/2020): I forgot to update here, but I created a python library that has everything in one place!