#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
gap = 60 #間隔60幀抽取保存一幀。
while True:
ok, frame = vc.read()
if not ok:
print("讀取失敗")
break
else:
print("讀取成功")
c=c+1
if (c % 60 == 0):
cv2.imwrite(savedpath+str(c) + '.jpg', frame) # 存儲爲圖片文件。
print("結束")
vc.release()