2015-01-14 167 views
1

我想通過opencv python VideoCapture訪問基本的網絡攝像頭(羅技c270)。不幸的是,每次我運行程序時,顯示屏都會變黑。我知道相機的作品,因爲我可以通過他們的軟件查看視頻。我很清楚確定要放入waitkey(x),所以這不是問題。我也有這個代碼,以防索引改變:使用網絡攝像頭與opencv蟒蛇顯示黑屏w/waitkey()

for i in range(4): 
     capture = cv2.VideoCapture(i) 
     if not capture: 
      print "UNABLE TO CAPTURE CAMERA" 
     else: 
      print "taken camera from index: ", i 
      break 

但它每次都返回一個0索引。問題不在於它無法找到它,要麼是因爲我有一部分代碼告訴我相機是否能夠檢索幀,所以問題可能在於read()。 最後,也許問題在於我的等待鍵在我的代碼中縮進了很多,可能大約有四個索引,它無法每次都引用waitkey。這是我參與的代碼塊。我是新人,所以我確定優化和技術非常糟糕。

class ColourTracker(object): 
    def __init__(self): 
    #cv2.namedWindow("ColourTrackerWindow", cv2.CV_WINDOW_AUTOSIZE) 
    self.scale_down = 4 
    self.start = time.time() 

    def run(self): 
    for i in range(4): 
     capture = cv2.VideoCapture(i) 
     if not capture: 
      print "UNABLE TO CAPTURE CAMERA" 
     else: 
      print "taken camera from index: ", i 
      break 
    ... 

    while True:  
     marker = marker + 1 
     if marker % 100 == 0: 
      print marker 
     f, orig_img = capture.read() 
     if not f: 
      print "Not read" 
      break 
     orig_img = cv2.flip(orig_img, 1) 
     cv2.imshow('orgImage', orig_img) 

     ... 

     largest_contour = None 
     for idx, contour in enumerate(contours): 
     area = cv2.contourArea(contour) 
     if area > max_area: 
      max_area = area 
      largest_contour = contour 
     if not largest_contour == None: 

      ... 

      #create an array of coordinates 
      if marker % 10 == 0: 
       cycle = [cx,cy,timer] 
       coordinates.append(cycle) 
      f = h5py.File(fileName, 'a') 
      if moment["m00"] > 1000/self.scale_down: 
       rect = cv2.minAreaRect(largest_contour) 
       rect = ((rect[0][0] * self.scale_down, rect[0][1] * self.scale_down), (rect[1][0] * self.scale_down, rect[1][1] * self.scale_down), rect[2]) 
       box = cv2.cv.BoxPoints(rect) 
       box = np.int0(box) 
       cv2.drawContours(orig_img,[box], 0, (0, 0, 255), 2) 
       cv2.imshow("ColourTrackerWindow", orig_img) 
       #out.write(orig_img) 
       if cv2.waitKey(20) == 27:      
        cv2.destroyAllWindows() 

        ... 

        self.capture.release() 
        #out.release() 
        f.close() # be CERTAIN to close the file 
        #testing_matrix.close() 
        break 
if __name__ == "__main__": 
    colour_tracker = ColourTracker() 
    colour_tracker.run() 

爲了長度的緣故,我剪掉了部分,這就是「...」的意思。

+0

今後請提供[最小的,完整的和可覈查的示例](https://stackoverflow.com/幫助/ MCVE)。色彩追蹤器的內容與問題無關,因此您應該留下所有額外的代碼以簡化問題並最大限度地獲得幫助。當他們的問題清晰簡明時,幫助他們會更容易。 – karlphillip

+1

一次*打開VideoCapture *,而不是連續4次。 – berak

回答

0

您應該在該循環之後添加安全檢查以確保它找到了某些東西。

現在看來,當發現沒有代碼仍在執行甚至:

import sys 

i = 0 
found = False 
for i in range(4): 
     capture = cv2.VideoCapture(i) 
     if not capture: 
      print "UNABLE TO CAPTURE CAMERA" 
     else: 
      found = True 
      print "taken camera from index: ", i 
      break 

if found == False: 
    print "!!! No camera was found." 
    sys.exit() 
+0

這在你的代碼中顯然是一個問題,但也可能有其他問題。祝你好運! – karlphillip

相關問題