使用带有opencv python的网络摄像头显示带有waitkey()的黑屏

时间:2015-01-14 16:39:52

标签: python opencv webcam

我正在尝试通过opencv python VideoCapture访问基本网络摄像头(Logitech c270)。不幸的是,每次运行程序时,显示屏都会显示为黑色。我知道相机有效,因为我可以通过他们的软件观看视频。我很清楚确保放入waitkey(x)所以这不是问题。我也有这个代码,以防索引发生变化:

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()

我为了长度而切出部分,这就是“......”的用途。

1 个答案:

答案 0 :(得分:0)

您应该在该循环后添加安全检查,以确保找到了某些内容。

现在,即使找不到任何内容,代码似乎仍在执行:

import sys
import cv2
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()