Slow camera initialization

Hi, trying to start simple code to show in the window video from webcam, but window with video appear only after 5 minutes, what is wrong?

import cv2

# Open camera
cap = cv2.VideoCapture(0)

# Changing size of slide
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

# Frame showing
while True:
    # Read frame
    ret, frame = cap.read()

    # If frame  exceed ->read it
    if ret:
        cv2.imshow('Webcam', frame)

    # break key
    key = cv2.waitKey(1)
    if key == 27: # press ESC to break
        break

# clear all
cap.release()
cv2.destroyAllWindows()

I find solution:
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

2 Likes