【发布时间】:2017-10-19 08:34:49
【问题描述】:
我在下面给出了一个代码。工作正常。即在几秒钟后需要几帧然后相机关闭,然后相机再次打开并拍摄几帧。每当我在树莓派上测试该代码时,下面给出的代码在我的桌面上工作正常for 循环的第一次迭代效果很好,但在 for 循环的第二次迭代中,它不会给 VideoCapture 返回 false
import cv2
import time
timeout=time.time()+10
for f in range(3):
cap = cv2.VideoCapture(camera_port)
count = 1
while True:
ret, img = cap.read()
print(ret)
cv2.imshow('image', img)
cv2.imwrite('Datasets/T' + str(f + 1) + '/' + str(count) + '.tif', img)
print(cap.get(cv2.CAP_PROP_FPS))
if time.time() > timeout:
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
count = count + 1
for _ in range(10):
cv2.destroyAllWindows()
if f!=2-1:
cap.release()
time.sleep(delay_after_one_video)
cap=cv2.VideoCapture(camera_port)
timeout=time.time()+10
【问题讨论】:
-
有什么理由释放并重新创建 videocapture() 对象?可能更容易保持连接并在需要图像时调用 read()
-
关闭相机我正在使用释放并重新创建对象...也保持缓冲区为空。
-
你为什么在循环中使用
cv2.destroyAllWindows?您只需调用该函数一次。 -
有时opencv不会破坏窗口,所以我在循环中使用....进入安全的一面