OpenCV: Brief note on how you can access webcam in Google colab!

Arvina Kori
2 min readMay 20, 2020

--

Working in Google Colab with free GPU is the smartest option that a beginner can get. But it becomes headache when you are working with OpenCV.

Basically Google Colab is cloud service, and you can’t access your local hardware like webcam. But it is possible by running some Javascript code given by Google Colab, but it only captures photo not video.

So i have grabbed some code from this article and some my code, to access webcam.

Step 0: Open Google Colab.

Step 1: Make Directory where all our captured videos will be stored.

Clone Opencv repo where you can get haarcascade files.

!git clone https://github.com/opencv/opencv.git
!mkdir Video

Step 2: Import Javascript code to open webcam.

Step 3: Open captured video

videofile = get_video()with open(video_file_test, 'wb') as f:
f.write(videofile)

Step 4: Some OpenCV code

If you want to view entire video you can remove count from while loop.

Step 5: And you are all done:

Disadvantage: Using this method, the video will be shown as png format, means frame by frame. But you know what it works.

You can find the working Github repo here.

OpenColab.ipynb

--

--

Arvina Kori
Arvina Kori

Written by Arvina Kori

Deep Learning Practitioner. Trying to get best out of the data!

Responses (2)