安装OpenCV

pip install opencv-python

python OpenCV 打开摄像头

import cv2

WIDTH  = 1080
HEIGHT = 720

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)

while True:
    ok,frame = cap.read()
    # print("ok", ok)
    # print("frame", frame)
    cv2.imshow("camera video", frame)

    if (cv2.waitKey(1) & 0xFF) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

参考

07-16 07:18