【问题标题】:Capturing frame from webcam, it return only 1 frame in opencv cv2.VideoCapture()从网络摄像头捕获帧,它在 opencv cv2.VideoCapture() 中仅返回 1 帧
【发布时间】:2021-04-08 09:21:06
【问题描述】:

我是图像处理的新手。

我正在尝试按照 Python OpenCV 中的一篇论文构建交通灯检测。

但我遇到了一个我无法理解的错误。

这里是代码。


# TL_Detection.py

import cv2
import numpy as np 


def Video():
    try:
        cap = cv2.VideoCapture(0)
        # cap = cv2.VideoCapture('/home/aicar/Downloads/tf_test.mp4')

    except:
        print('no cam error')
        return

    # cap.set(3, 480)
    # cap.set(4, 320)

    frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    size = (frameWidth, frameHeight)
    cap.set(3, frameWidth)
    cap.set(4, frameHeight)

    cnt = 0

    # while cap.isOpened():
    while True:
        ret, frame = cap.read()
        # if not cap.isOpened():
        #     cap.open('/home/aicar/Downloads/tf_test.mp4')
        cv2.imshow('frame', frame)

        print(ret, cnt)
        if not ret:
            print('no ret error')
            break


        cnt += 1
        cap.release()
        cv2.destroyAllWindows()
        
Video()

此代码返回如下。

True 0
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/aicar/opencv/opencv-3.4.0/modules/highgui/src/window.cpp, line 339
Traceback (most recent call last):
  File "/home/aicar/codes_juyeong/TL_detection.py", line 53, in <module>
    Video()
  File "/home/aicar/codes_juyeong/TL_detection.py", line 33, in Video
    cv2.imshow('frame', frame)
cv2.error: /home/aicar/opencv/opencv-3.4.0/modules/highgui/src/window.cpp:339: error: (-215) size.width>0 && size.height>0 in function imshow

为什么只获得第一帧后无法获得帧? 网络摄像头连接正确。

需要你的帮助。谢谢。

【问题讨论】:

  • cap.release() cv2.destroyAllWindows() 应该在外面 while True: loop

标签: python c++ opencv


【解决方案1】:

你过早释放你的上限,它处于真正的循环中。所以把它从这个循环中解脱出来,你的程序就可以毫无问题地运行了。

如果这个回答对你有帮助,请采纳。

【讨论】:

    【解决方案2】:

    要逐帧捕获,请尝试以下代码:

    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    
    while(True):
    # Capture frame-by-frame
       ret, frame = cap.read()
    
    # Our operations on the frame come here
       gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # Display the resulting frame
       cv2.imshow('frame',gray)
       if cv2.waitKey(1) & 0xFF == ord('q'):
           break
    
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    

    【讨论】:

      【解决方案3】:
      # while cap.isOpened():
      while True:
          ret, frame = cap.read()
          # if not cap.isOpened():
          #     cap.open('/home/aicar/Downloads/tf_test.mp4')
          cv2.imshow('frame', frame)
      
          print(ret, cnt)
          if not ret:
              print('no ret error')
              break
      
      
          cnt += 1
      
      # take it out of while loop
      cap.release()
      cv2.destroyAllWindows()
      

      【讨论】:

      • 多么亲切的回答。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      相关资源
      最近更新 更多