2017-10-18 72 views
0

我有一個代碼如下。這工作fine.ie它需要一些幀後幾秒鐘然後相機關閉,並再次打開相機,並採取一對frames.The下面給出的代碼在我的桌面上工作正常,只要我測試代碼在樹莓pi的for循環效果很好,但在for循環它不給VideoCapture第二次重複第一次迭代返回falseraspberrypi上opencv間隔後拍攝的相機幀數

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 
+0

任何理由來轉發並重新創建videocapture()對象?當你需要一個圖像 –

+0

來關閉我正在使用的發行版nd重新創建對象...並保持緩衝區爲空時,可能更容易將它連接起來並調用read()。 –

+0

爲什麼在循環中使用'cv2.destroyAllWindows'?你只需要調用一次該函數。 – zindarod

回答

0

我不能評論,所以我發帖回答。

似乎你不能釋放相機資源(如果是這樣,你不能設置相機的屬性)嘗試自己構建OpenCV,記得檢查v4l庫。