cv2.VideoCapture() is not opening the web camera when executing the command in Google Colab because the code runs on the “cloud” (i.e. on a remote server). The processing happens on Google’s servers, they’ll have no physical access to your device. So when you try to have VideoCapture read the first local (local to where the code runs) camera won’t work.

Since Colab is running in your browser, you need to use web APIs to access local hardware like a camera. Google provided code to capture an image inside Google Colab but there is no code to capture the video there.

The following code lets you take videos inside Google Colab. It uses Javascript inside of colab to access the client computer’s camera.

from IPython.display import display, Javascript,HTML
from google.colab.output import eval_js
from base64 import b64decode

def record_video(filename):
  js=Javascript("""
    async function recordVideo() {
      const options = { mimeType: "video/webm; codecs=vp9" };
      const div = document.createElement('div');
      const capture = document.createElement('button');
      const stopCapture = document.createElement("button");
      
      capture.textContent = "Start Recording";
      capture.style.background = "orange";
      capture.style.color = "white";

      stopCapture.textContent = "Stop Recording";
      stopCapture.style.background = "red";
      stopCapture.style.color = "white";
      div.appendChild(capture);

      const video = document.createElement('video');
      const recordingVid = document.createElement("video");
      video.style.display = 'block';

      const stream = await navigator.mediaDevices.getUserMedia({audio:true, video: true});
    
      let recorder = new MediaRecorder(stream, options);
      document.body.appendChild(div);
      div.appendChild(video);

      video.srcObject = stream;
      video.muted = true;

      await video.play();

      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);

      await new Promise((resolve) => {
        capture.onclick = resolve;
      });
      recorder.start();
      capture.replaceWith(stopCapture);

      await new Promise((resolve) => stopCapture.onclick = resolve);
      recorder.stop();
      let recData = await new Promise((resolve) => recorder.ondataavailable = resolve);
      let arrBuff = await recData.data.arrayBuffer();
      
      // stop the stream and remove the video element
      stream.getVideoTracks()[0].stop();
      div.remove();

      let binaryString = "";
      let bytes = new Uint8Array(arrBuff);
      bytes.forEach((byte) => {
        binaryString += String.fromCharCode(byte);
      })
    return btoa(binaryString);
    }
  """)
  try:
    display(js)
    data=eval_js('recordVideo({})')
    binary=b64decode(data)
    with open(filename,"wb") as video_file:
      video_file.write(binary)
    print(f"Finished recording video at:{filename}")
  except Exception as err:
    print(str(err))

If you run OpenCV on Google Colab it will look for the camera on the server and not the client computer. Since this code runs in javascript it uses the client’s computer. Since Colab is running in your browser, you need to use web APIs to access local hardware like a camera.

video_path = "test.mp4"
record_video(video_path)
Colab Capture video

Play Video in Google Colab

If you want to play the video in Google Colab without downloading you need to compress the video file to play it in google colab, if the format is not supported.

from IPython.display import HTML
from base64 import b64encode

def show_video(video_path, video_width = 600):
  
  video_file = open(video_path, "r+b").read()

  video_url = f"data:video/mp4;base64,{b64encode(video_file).decode()}"
  return HTML(f"""<video width={video_width} controls><source src="{video_url}"></video>""")

show_video(video_path)
Play Video in Google Colab

Related Post

Run this code in Google Colab