'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 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1