iT邦幫忙

0

Python cv2 播Video

Python cv2 播Video
如有誤,請指正。
VideoCapture Video file 請自選

import cv2
# open video file
cap = cv2.VideoCapture('rain.mp4')
#--- video information 
frame_width  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
FPS = int(cap.get(cv2.CAP_PROP_FPS))
frame_count  = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f'width {frame_width} height {frame_height}')
print(f'FPS: {FPS}  frame_count {frame_count}')
#--- setup window for show 
cv2.namedWindow('video',cv2.WINDOW_KEEPRATIO) 
cv2.resizeWindow('video', int(frame_width/3), int(frame_height/3))
cv2.moveWindow('video',300,200)

# read frame by frame
while(True):
    ret, frame = cap.read()
    cv2.imshow('video',frame)
    #播灰階 
    #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #cv2.imshow('video',gray)    
    #waitKey 單位msec  1000msec = 1sec
    #FPS=24 表示1sec播24張,所以1000msec/24=25msec
    #每張可停格 25msec=waitKey(25) 可設定20~25之間,肉眼無法區分快慢
    #Escape key to leave 
    if cv2.waitKey(20)== 27:
        break
#--- ending  
cap.release()
cv2.destroyAllWindows()
    

尚未有邦友留言

立即登入留言