OpenCV讀取視頻錄像分解抽幀,Python

import cv2
import os

if __name__ == "__main__":
    vc = cv2.VideoCapture('test.mp4')

    if vc.isOpened():  # 是否正常打開
        print("打開ok")
    else:
        print("打開失敗,程序退出")
        exit(-1)

    savedpath = 'image/'
    isExists = os.path.exists(savedpath)
    if not isExists:
        os.makedirs(savedpath)
        print("創建存儲路徑")
    else:
        print("路徑已經存在")

    c = 1
    while True:
        ok, frame = vc.read()
        if ok:
            cv2.imwrite(savedpath+str(c) + '.jpg', frame)  # 存儲爲圖片文件。
            c = c + 1
        else:
            print("讀取失敗")
            break

    print("結束")
    vc.release()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章