Search code examples
pythonopencvwebcam-capture

increase opencv webcam speed


i need to capture a video with my webcam. I would like to use open cv for my usage. The skript you can find down under needs a bunch of time to start the capturing. Does any of you know a solution to speed up this skript ?

I tried to decrease the webcam ratio to 640x480.

webcam = cv2.VideoCapture(0)
##Video Codec
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
width = 640
height = 480



video = VideoWriter(dir_path +"\\" + folder +"\\" + Name +".mp4",fourcc, 20.0, (width,height))



while (True):
    # lese aus Webcam
    ret, frame = webcam.read()
    if ret == False:
        print("Device not Found")
        break
    # Webcam Bild anzeigen
    cv2.imshow('Webcam', frame)
    #print("Aufnahme gestartet")
    # Videosequenz in Datei ablegen
    video.write(frame)
    #Erkennen, ob die Esc-Taste gedrückt wurde
    c = cv2.waitKey(1)
    if c == 27:
        break
## Alle Fenster schließen
cv2.destroyAllWindows()
## Video Aufnahme freigeben
webcam.release()
video.release()

Solution

  • The change is a webcam.read() in triplicate, effectively reading three frames, skiping first two frames, and only writing the third frame. Hope it will work

    webcam = cv2.VideoCapture(0)
    ##Video Codec
    fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    width = 640
    height = 480
    
    
    
    video = VideoWriter(dir_path +"\\" + folder +"\\" + Name +".mp4",fourcc, 20.0, (width,height))
    
    
    
    while (True):
        # lese aus Webcam
        ret, frame = webcam.read()
        ret, frame = webcam.read()
        ret, frame = webcam.read()
    
        if ret == False:
            print("Device not Found")
            break
        # Webcam Bild anzeigen
        cv2.imshow('Webcam', frame)
        #print("Aufnahme gestartet")
        # Videosequenz in Datei ablegen
        video.write(frame)
        #Erkennen, ob die Esc-Taste gedrückt wurde
        c = cv2.waitKey(1)
        if c == 27:
            break
    ## Alle Fenster schließen
    cv2.destroyAllWindows()
    ## Video Aufnahme freigeben
    webcam.release()
    video.release()