Opencv在Colab笔记本中赢得了视频。 CV.VIDECAPTURE对象不返回形状

发布于 2025-02-13 09:16:22 字数 2376 浏览 1 评论 0原文

我正在尝试遵循 tutorial> tutorial 视频中的人使用jupyter笔记本来运行代码Opencv-Python和OpenCV-Contrib-Python。我试图在Pycharm中导入CV2,但显然OpenCV在Python 3.10.5上不起作用,并且我不愿意降级。

,我决定改用COLAB笔记本中运行代码,而是以下内容:

import cv2 as cv
from google.colab.patches import cv2_imshow
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

,当然是第二个区块:

#Read the image
img = cv.imread("/content/opencvimages/catimages/cat.jpg")
cv2_imshow(img)

#This image displays properly when the block is ran.

这个特定的块是给我问题的块:

capture = cv.VideoCapture("/content/opencvimages/dogvideo/Puppy4740.mp4")

while True:
  isTrue, frame = capture.read()

  if not isTrue: 
    break

  cv2_imshow(capture) #<-------------- The problem seems to be this method.

  if cv.waitKey(20) & 0xFF==ord('d'):
    break

capture.release()

,所以当我尝试运行时代码:它返回attributeError:

AttributeError                            Traceback (most recent call last)
<ipython-input-28-845b4263fabf> in <module>()
      7     break
      8 
----> 9   cv2_imshow(capture)
     10 
     11   if cv.waitKey(20) & 0xFF==ord('d'):

/usr/local/lib/python3.7/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: 'cv2.VideoCapture' object has no attribute 'clip'

有点sus

capture = cv.VideoCapture("/content/opencvimages/dogvideo/Puppy4740.mp4")
print(capture.shape)

我认为

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-c75dc112b69d> in <module>()
      1 capture = cv.VideoCapture("/content/opencvimages/dogvideo/Puppy4740.mp4")
----> 2 print(capture.shape)

AttributeError: 'cv2.VideoCapture' object has no attribute 'shape'

这是 由于某种原因,CV2。视频时,VIDEOCAPTURE并未完成工作。我检查了,仔细检查,甚至进行了三次检查,以确保将正确的文件和路径放置在方法上,但是它无法正确处理视频,因此我无法通过教程前进。另外,您可以看到对象本身不会返回形状。不确定这里是什么问题。

I'm trying to follow along a tutorial on OpenCV, in which the person in the video uses a Jupyter notebook to run the code but when I tried to run it Jupyter didn't recognize cv2, even after attempting to pip install opencv-python and opencv-contrib-python. I tried to import cv2 in PyCharm but apparently openCV doesn't work on python 3.10.5 and I'm not willing to downgrade.

So I decided to run the code in a Colab Notebook instead, which is the following:

import cv2 as cv
from google.colab.patches import cv2_imshow
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

And of course here is the second block:

#Read the image
img = cv.imread("/content/opencvimages/catimages/cat.jpg")
cv2_imshow(img)

#This image displays properly when the block is ran.

This particular block is the one giving me issues:

capture = cv.VideoCapture("/content/opencvimages/dogvideo/Puppy4740.mp4")

while True:
  isTrue, frame = capture.read()

  if not isTrue: 
    break

  cv2_imshow(capture) #<-------------- The problem seems to be this method.

  if cv.waitKey(20) & 0xFF==ord('d'):
    break

capture.release()

So when I try to run the code: it returns an AttributeError:

AttributeError                            Traceback (most recent call last)
<ipython-input-28-845b4263fabf> in <module>()
      7     break
      8 
----> 9   cv2_imshow(capture)
     10 
     11   if cv.waitKey(20) & 0xFF==ord('d'):

/usr/local/lib/python3.7/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: 'cv2.VideoCapture' object has no attribute 'clip'

I thought that was a little sus so I ran a separate block of code placed before the previous one:

capture = cv.VideoCapture("/content/opencvimages/dogvideo/Puppy4740.mp4")
print(capture.shape)

Which returned this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-c75dc112b69d> in <module>()
      1 capture = cv.VideoCapture("/content/opencvimages/dogvideo/Puppy4740.mp4")
----> 2 print(capture.shape)

AttributeError: 'cv2.VideoCapture' object has no attribute 'shape'

So for some reason, cv2.VideoCapture is not doing its job when it comes to the video. I checked, double-checked, and even triple-checked to make sure the right file and path was placed on the method but it is not processing the video correctly and so I can't move forward with the tutorial. Also, as you can see the object itself is not returning a shape. Not sure what the issue is here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮颜 2025-02-20 09:16:23

您似乎将视频关注对象(思考:VR)与读取图像混淆...

您想显示从捕获中检索的图像,而不是捕获实例本身,因此::

cv2_imshow(frame)

you seem to confuse the VideoCapture object (think: a VR) with an image read from it ...

you want to show the image retrieved from the capture, not the capture instance itself, so:

cv2_imshow(frame)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文