Python中呼叫OpenCV介面儲存攝像頭或視訊中的影象幀到視訊檔案
阿新 • • 發佈:2018-12-29
Python中呼叫OpenCV介面儲存攝像頭或視訊中的影象幀到視訊檔案
import cv2 cap = cv2.VideoCapture(0) # 開啟攝像頭 size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, size) while(1): ret, frame = cap.read() if ret==True: out.write(frame) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv2.destroyAllWindows()
效果圖: