使用opencv打开相机——python

import cv2 as cv

def video_demo():
    # 0是代表摄像头编号,只有一个的话默认为0
    capture = cv.VideoCapture(0)
    while (True):
        # 调用摄像机
        ref, frame = capture.read()
        # 输出图像,第一个为窗口名字
        cv.imshow('frame', frame)
        # 10s显示图像,若过程中按“Esc”退出,若按“s”保存照片并推出
        c = cv.waitKey(10) & 0xff
        if c == 27:
            # 简单暴力释放所有窗口
            cv.destroyAllWindows()
            break
        elif c == ord('s'):
            # 储存照片
            cv.imwrite('./images/pic.png',frame)
            break


if __name__ == '__main__':
    cv.waitKey()
    video_demo()

参考文章

猜你喜欢

转载自blog.csdn.net/qq_39248122/article/details/88396993