使用cv2.VideoCapture没有来自相机的图像

时间:2018-03-02 12:38:35

标签: python opencv

我使用WebCam(Genius FaceCam 1020),Python(3.6.4),OpenCV(3.4.0.12)和Opencv文档代码,这一个:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

运行没有错误,但图像不会出现在窗口中。相机上的指示灯亮起。 我试过复制opencv_ffmpeg.dll,以这种方式重命名opencv_ffmpeg.dll:

对于OpenCV版本X.Y.Z

opencv_ffmpeg.dll ==> opencv_ffmpegXYZ.dll

对于64位OpenCV版本X.Y.Z

opencv_ffmpeg.dll ==> opencv_ffmpegXYZ_64.dll

opencv_ffmpeg34012.dllopencv_ffmpeg34012_64.dll。但它没有帮助。

1 个答案:

答案 0 :(得分:0)

使用凸轮可以在下面用调试代码完成另一次围绕灌木丛的跳动。异常挂钩应该是垃圾邮件错误代码,您通常不会看到脚本是否遇到障碍并以静默方式终止。我在编辑中使用它。

import sys, cv2, time


def my_exception_hook(exctype, value, traceback):
    # Print the error and traceback
    print(exctype, value, traceback)
    # Call the normal Exception hook after
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

def runcam():

    camera = cv2.VideoCapture(0)

    for i in range(10):
        time.sleep(1)  # implemented for cams with long image aquisition time. Its 1 sec.
                       # delay before the next step is repeated until range has finished.
                       # From first image to last each image becomes brighter.
        return_value, image = camera.read()
        cv2.imwrite('opencv'+str(i)+'.png', image)
        print ' taking image %s' % (i+1)


    del(camera)

    # Back up the reference to the exceptionhook
    sys._excepthook = sys.excepthook

    # Set the exception hook to our wrapping function
    sys.excepthook = my_exception_hook

def check_cam_index():
    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()


if __name__ == '__main__':
    check_cam_index()
    runcam()