[英]extracting frames from video
我正在嘗試從視頻中提取幀,我正在使用以下代碼:
import cv2
vidcap = cv2.VideoCapture('video_01.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
success,image = vidcap.read()
print('Read a new frame: ', success)
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
count += 1
這返回
讀取新幀:錯誤
我在 SO 中關注了有關此的各種帖子。 我安裝了 ffmepg, av 但沒有任何效果。
我在 Windows 上運行它,通過 Anaconda 安裝了 opencv。
什么是解決方法? 感謝您的幫助
我也試過
import cv2
import numpy as np
import os
# Playing video from file:
cap = cv2.VideoCapture('example.mp4')
try:
if not os.path.exists('data'):
os.makedirs('data')
except OSError:
print ('Error: Creating directory of data')
currentFrame = 0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Saves image of the current frame in jpg file
name = './data/frame' + str(currentFrame) + '.jpg'
print ('Creating...' + name)
cv2.imwrite(name, frame)
# To stop duplicate images
currentFrame += 1
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
但是,這會保存空圖像
如何將第二個代碼(第 15 行)中的 while 循環更改為此
while(cap.isOpened()):
來自
while(True):
看來你的代碼沒有問題,請嘗試運行以下命令:
pip install opencv-python
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.