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