首页 > 解决方案 > 在 Google colab 中尝试此代码,但不知道如何修复它

问题描述

我用过cv2_imshow

import cv2
from google.colab.patches import cv2_imshow
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)
while True:
    success, img = cap.read()
    cv2_imshow(img)
    if cv2.waitKey(1) and 0xFF == ord('q'):
        break

得到这个错误

AttributeError                            Traceback (most recent call last)
<ipython-input-17-8302024201cc> in <module>()
     10 while True:
     11     img = cap.read()
---> 12     cv2_imshow(img)
     13     if cv2.waitKey(1) and 0xFF == ord('q'):
     14         break

/usr/local/lib/python3.6/dist-packages/google/colab/patches/__init__.py in cv2_imshow(a)
     20       image.
     21   """
---> 22   a = a.clip(0, 255).astype('uint8')
     23   # cv2 stores colors as BGR; convert to RGB
     24   if a.ndim == 3:

AttributeError: 'tuple' object has no attribute 'clip'

标签: pythongoogle-colaboratory

解决方案


由于您cap = cv2.VideoCapture(0)用于从网络摄像头捕获视频,并且 colab 正在浏览器中运行,因此您需要使用 Web API 来访问本地硬件,例如摄像头。

下面是一个示例,展示了如何从 Colab 中的本地网络摄像头捕获图像:https ://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi


推荐阅读