简体   繁体   中英

Facecam Video does not open with cv2.VideoCapture() and cannot be read

I'm new to OpenCV and just started learning I am using macOS, python version 3.8.1

My code:

import cv2
# takes facecam video
cap = cv2.VideoCapture(-1)

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    if cap.isOpened() == True:
        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Display the resulting frame
        cv2.imshow('frame',gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
         cap.open(-1)

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

This works for me

import cv2
cap = cv2.VideoCapture(0)

while True:
    ne, frame = cap.read()

    cv2.imshow("Webcam Capture", frame)
    

    if cv2.waitKey(1) & 0xFF == ord("A"):
        break


cap.release()
cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM