简体   繁体   English

将网络摄像头与opencv python一起使用时显示带有w / waitkey()的黑屏

[英]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. 我正在尝试通过opencv python VideoCapture访问基本的网络摄像头(Logitech c270)。 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. 我很清楚要确保放入waitkey(x),所以这不是问题。 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. 但是每次都返回0索引。 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(). 问题不在于是否有麻烦,因为我有一部分代码告诉我相机是否能够检索帧,所以问题可能出在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. 出于长度考虑,我切出了一些部分,这就是“ ...”的含义。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM