Python之OpenCV(视频)

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()

猜你喜欢

转载自blog.csdn.net/weixin_38452632/article/details/84325538