Search code examples
pythonopencvwebcam

Using webcam with opencv python shows a black screen w/waitkey()


I'm trying to access a basic webcam (Logitech c270) through opencv python VideoCapture. Unfortunately though, every time I run the program the display screen comes up black. I know the camera works because I can view the video through their software. I am well aware of making sure to put in the waitkey(x) so that's not the issue. I also have this code just in case the index changes:

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

But it's returned a 0 index each time. And it's the issue is not that it's having trouble finding it either because I have a part of code to tell me if the camera was able to retrieve a frame, so maybe the problem is with read(). Finally, maybe the issue is that my wait key is indented so much into my code, maybe about four indexes in, that it's not able to reference the the waitkey each time. Here is the chunks of my code that are involved in this. I'm new so I'm sure the optimization and technique is very bad.

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

I cut out portions for length sake so that's what the "..." are for.


Solution

  • You should add a safety check after that loop to make sure it found something.

    Right now it seems that the code is still executing even when nothing was found:

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