Search code examples
pythonpython-2.7opencvvideowebcam

Python OpenCV avoid Video Source -> Capture source dialog


So I'm trying to create a simple program on python 2.7.5 where a window would show live video feed for a user chosen time.

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)

But when i run it a dialog asking for my camera pops-up(Video Source -> Capture source). How can i hide it from the user? P.S. I also tried to run the...

cap = cv2.VideoCapture(0)

...in the main function but the result is the same:

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 I'm on Windows but i don't have to necessarily use this version of python or OpenCV


Solution

  • Try this:

    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)