14
votes

I am trying to open a web camera in Google Colab. I've executed the following command:

cap = cv2.VideoCapture(0)

but this is not working: the web camera is not opening.

Is it possible to open a connection to my web camera on Google Colab?

2
It's a Jupyter notebook, so the code runs on the "cloud" (i.e. on some remote server). It's unlikely that you will be able to plug a camera to the server it's running on, so trying to have VideoCapture read the first local (local to where the code runs) camera won't work. - Dan Mašek

2 Answers

24
votes
  1. Go to Google Colaboratory

  2. Create a new notebook

  3. Click snippet panel

  4. Click Camera Capture. Click insert

  5. Run script: Shift + Enter

enter image description here

20
votes

Since Colab is running in your browser, you'll want to use web APIs to access local hardware like a camera.

Here's an example showing how to capture an image from your local webcam in Colab: https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi