Python OpenCV 避免视频源 -> 捕获源对话框

Python OpenCV avoid Video Source -> Capture source dialog

所以我试图在 python 2.7.5 上创建一个简单的程序,其中 window 显示用户选择时间的实时视频源。

import numpy as np
import cv2
import time


def Func_VideoCapture(Float_Time = 10):
    Float_WantedTime = time.time() + Float_Time
    Float_CurentTime = time.time()

    cap = cv2.VideoCapture(0)

    while Float_CurentTime <= Float_WantedTime:
        #get current time
        Float_CurentTime = time.time()

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

        # Display the resulting frame
        cv2.imshow('frame',frame)

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

if __name__ == "__main__":
    Float_Time = float(raw_input(">>> "))
    Func_VideoCapture(Float_Time)

但是当我 运行 它弹出一个对话框询问我的相机(视频源 -> 捕获源)。 我怎样才能对用户隐藏它? P.S。我也试过运行...

cap = cv2.VideoCapture(0)

...在主函数中但结果相同:

import numpy as np
import cv2
import time

def Func_VideoCapture(cap, Float_Time = 10):
    Float_WantedTime = time.time() + Float_Time
    Float_CurentTime = time.time()

while Float_CurentTime <= Float_WantedTime:
    Float_CurentTime = time.time()

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

    # Display the resulting frame
    cv2.imshow('frame',frame)

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

def main():
    cap = cv2.VideoCapture(0)
    Float_Time = float(raw_input(">>> "))
    Func_VideoCapture(cap, Float_Time)

if __name__ == "__main__":
    main()

P.S.#2 我在 Windows 但我不一定要使用此版本的 python 或 OpenCV

试试这个:

import numpy as np
import cv2
import time


def Func_VideoCapture(Float_Time = 10):
    Float_WantedTime = time.time() + Float_Time
    Float_CurentTime = time.time()

    cap = cv2.VideoCapture(0)



    while Float_CurentTime<=Float_WantedTime:
        #get current time
        Float_CurentTime = time.time()

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

        # Display the resulting frame
        cv2.imshow('frame',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

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

if __name__ == "__main__":
    Float_Time = float(raw_input(">>> "))
    Func_VideoCapture(Float_Time)