import cv2#導入opencv包 video=cv2.VideoCapture(0)#打開攝像頭 fourcc = cv2.VideoWriter_fourcc(*'XVID')#視頻存儲的格式 fps = video.get(cv2.CAP_PROP_FPS)#幀率 #視頻的寬高 size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), \ int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter('video.avi', fourcc, fps, size)#視頻存儲 while out.isOpened(): ret,img=video.read()#開始使用攝像頭讀數據,返回ret為true,img為讀的圖像 if ret is False:#ret為false則關閉 exit() cv2.namedWindow('video',cv2.WINDOW_AUTOSIZE)#創建一個名為video的窗口 cv2.imshow('video',img)#將捕捉到的圖像在video窗口顯示 out.write(img)#將捕捉到的圖像存儲 #按esc鍵退出程序 if cv2.waitKey(1) & 0xFF ==27: video.release()#關閉攝像頭 break