【发布时间】: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