You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Colab中访问本地摄像头实时视频流?

解决Google Colab访问本地摄像头的问题

你的代码无法运行有两个核心原因:

  1. Colab是云端运行环境,cv2.VideoCapture(0)会尝试访问云端服务器的摄像头,而非你本地电脑的设备;
  2. cv2.imshow依赖本地桌面窗口环境,Colab的云端环境不支持该显示方式。

要在Colab中访问本地摄像头,需要通过浏览器的媒体API获取摄像头流,再传递到Colab环境中处理。以下是两种可行的实现方案:

方案1:实时获取并显示本地摄像头画面

from IPython.display import display, Javascript, clear_output
from google.colab.output import eval_js
import cv2
import numpy as np
from base64 import b64decode
import PIL.Image

# 定义获取摄像头流的JS函数
def get_video_stream():
    js = Javascript('''
        async function getVideoStream() {
            const video = document.createElement('video');
            video.style.display = 'none';
            const stream = await navigator.mediaDevices.getUserMedia({video: true});
            document.body.appendChild(video);
            video.srcObject = stream;
            await video.play();

            const canvas = document.createElement('canvas');
            canvas.width = video.videoWidth;
            canvas.height = video.videoHeight;
            const ctx = canvas.getContext('2d');

            setInterval(() => {
                ctx.drawImage(video, 0, 0);
            }, 100);

            return canvas.toDataURL('image/jpeg', 0.8);
        }
    ''')
    display(js)
    return eval_js('getVideoStream()')

# 处理摄像头帧(可自定义逻辑)
def process_frame(frame_data):
    # 解码base64数据为OpenCV格式帧
    img_data = b64decode(frame_data.split(',')[1])
    np_arr = np.frombuffer(img_data, np.uint8)
    frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
    
    # 示例:添加自定义处理(如转灰度图)
    # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # 转换为RGB格式用于显示
    return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

# 主循环:实时捕获并显示
try:
    while True:
        frame_data = get_video_stream()
        processed_frame = process_frame(frame_data)
        clear_output(wait=True)
        display(PIL.Image.fromarray(processed_frame))
except KeyboardInterrupt:
    print('已停止摄像头捕获')

方案2:捕获单张本地摄像头图片(适合非实时场景)

from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
import PIL.Image
import cv2
import numpy as np

def take_photo(filename='local_photo.jpg', quality=0.8):
    js = Javascript('''
        async function takePhoto(quality) {
            const div = document.createElement('div');
            const captureBtn = document.createElement('button');
            captureBtn.textContent = '点击捕获图片';
            div.appendChild(captureBtn);

            const video = document.createElement('video');
            video.style.display = 'block';
            const stream = await navigator.mediaDevices.getUserMedia({video: true});

            document.body.appendChild(div);
            div.appendChild(video);
            video.srcObject = stream;
            await video.play();

            // 等待用户点击按钮完成捕获
            await new Promise(resolve => captureBtn.onclick = resolve);

            const canvas = document.createElement('canvas');
            canvas.width = video.videoWidth;
            canvas.height = video.videoHeight;
            canvas.getContext('2d').drawImage(video, 0, 0);
            stream.getVideoTracks()[0].stop();
            div.remove();
            return canvas.toDataURL('image/jpeg', quality);
        }
    ''')
    display(js)
    data = eval_js(f'takePhoto({quality})')
    img_data = b64decode(data.split(',')[1])
    with open(filename, 'wb') as f:
        f.write(img_data)
    return filename

# 调用函数捕获并显示图片
try:
    saved_filename = take_photo()
    print(f"图片已保存到Colab环境:{saved_filename}")
    
    # 读取并显示图片
    img = cv2.imread(saved_filename)
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    display(PIL.Image.fromarray(img_rgb))
except Exception as err:
    print(f"捕获失败:{str(err)}")

关键注意事项

  • 运行代码时,浏览器会弹出摄像头权限请求,必须点击允许才能正常获取流;
  • 实时流方案可通过Ctrl+C终止捕获;
  • 如需复杂帧处理,直接修改process_frame函数内的逻辑即可。

内容的提问来源于stack exchange,提问作者Abdul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 20:02:39