Python之OpenCV(視訊)
阿新 • • 發佈:2018-12-03
import numpy as np import cv2 cap = cv2.VideoCapture(0) while(True): ret,frame = cap.read()#read讀取攝像頭資料 gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) cv2.imshow("frame",gray)#顯示圖片 if cv2.waitKey(1) & 0xff == ord('q'): break cap.release() cv2.destroyAllWindows() #用來開啟MP4 cap = cv2.VideoCapture("C:/Users/Administrator/Desktop/cats.mp4") fps = cap.get(cv2.CAP_PROP_FPS) frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) print(fps) print(frame_width) print(frame_height) while(True): ret,frame = cap.read() if ret != True: break #gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) cv2.imshow("frame",frame) if cv2.waitKey(25) & 0xff == ord('q'): break cap.release() cv2.destroyAllWindows() #用來開啟MP4 cap = cv2.VideoCapture("C:/Users/Administrator/Desktop/cats.mp4") fps = cap.get(cv2.CAP_PROP_FPS) frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) print(fps) print(frame_width) print(frame_height) #cap.release() #cv2.destroyAllWindows() fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('video/output.avi',fourcc,fps,(frame_width,frame_height)) while(True): ret,frame = cap.read() if ret == True: frame = cv2.flip(frame,1) out.write(frame) cv2.imshow('frame',frame) if cv2.waitKey(25) & 0xff == ord('q'): break else: break; out.release() cap.release() cv2.destroyAllWindows()