python - 在 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'
解决方案
由于您cap = cv2.VideoCapture(0)
用于从网络摄像头捕获视频,并且 colab 正在浏览器中运行,因此您需要使用 Web API 来访问本地硬件,例如摄像头。
下面是一个示例,展示了如何从 Colab 中的本地网络摄像头捕获图像:https ://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi
推荐阅读
- xml - XSLT 从没有 xml 声明或 .xml 文件扩展名的文本文件中读取 xml 内容
- php - 如何在laravel eloquent中以30分钟为间隔获取一天的记录
- android - Android Mapbox SDK v10:归属位置;用户界面设置
- android - 重复使用 Xamarin.Essentials.MediaPicker.CapturePhotoAsync 在 Android 11 上重新启动我的应用程序
- angular - 如何以角度调整滚动条的大小?
- java - 使用 java 创建正确的 Stream API
- gitlab - 如何将 Jira 云与 VPN 背后的 Gitlab 集成
- python - ModuleNotFoundError:没有名为“en_core_web_sm”的模块
- python - 不同块矩阵的不同cmap
- python - 使用按钮操作后如何在 Flask 中处理表单参数