所以我遵循这个教程,但它似乎没有做任何事情。只是没有。它等待几秒钟并关闭程序。这段代码有什么问题?
import cv2
vidcap = cv2.VideoCapture('Compton.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
success,image = vidcap.read()
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
if cv2.waitKey(10) == 27: # exit if Escape is hit
break
count += 1
此外,在评论中它说这限制帧到1000?为什么?
编辑:
我试着先做success = True,但没有帮助。它只创建了一个0字节的图像。
这是函数,它将大多数视频格式转换为视频中的帧数。它可以在Python3和OpenCV 3+上工作
import cv2
import time
import os
def video_to_frames(input_loc, output_loc):
"""Function to extract frames from input video file
and save them as separate frames in an output directory.
Args:
input_loc: Input video file.
output_loc: Output directory to save the frames.
Returns:
None
"""
try:
os.mkdir(output_loc)
except OSError:
pass
# Log the time
time_start = time.time()
# Start capturing the feed
cap = cv2.VideoCapture(input_loc)
# Find the number of frames
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
print ("Number of frames: ", video_length)
count = 0
print ("Converting video..\n")
# Start converting the video
while cap.isOpened():
# Extract the frame
ret, frame = cap.read()
if not ret:
continue
# Write the results back to output location.
cv2.imwrite(output_loc + "/%#05d.jpg" % (count+1), frame)
count = count + 1
# If there are no more frames left
if (count > (video_length-1)):
# Log the time again
time_end = time.time()
# Release the feed
cap.release()
# Print stats
print ("Done extracting frames.\n%d frames extracted" % count)
print ("It took %d seconds forconversion." % (time_end-time_start))
break
if __name__=="__main__":
input_loc = '/path/to/video/00009.MTS'
output_loc = '/path/to/output/frames/'
video_to_frames(input_loc, output_loc)
它支持。mts和普通文件,如。mp4和。avi。在.mts文件上尝试和测试。工作就像一个咒语。
之前的答案已经失去了第一帧。把图片存储在文件夹里会很好。
# create a folder to store extracted images
import os
folder = 'test'
os.mkdir(folder)
# use opencv to do the job
import cv2
print(cv2.__version__) # my version is 3.1.0
vidcap = cv2.VideoCapture('test_video.mp4')
count = 0
while True:
success,image = vidcap.read()
if not success:
break
cv2.imwrite(os.path.join(folder,"frame{:d}.jpg".format(count)), image) # save frame as JPEG file
count += 1
print("{} images are extacted in {}.".format(count,folder))
顺便说一下,你可以通过VLC检查帧率。进入“windows ->媒体信息->编解码器详细信息”
在稍微不同的情况下扩展这个问题(@user2700065的回答),如果有人不想提取每一帧,但想每一秒提取一帧。所以1分钟的视频会有60帧(图像)。
import sys
import argparse
import cv2
print(cv2.__version__)
def extractImages(pathIn, pathOut):
count = 0
vidcap = cv2.VideoCapture(pathIn)
success,image = vidcap.read()
success = True
while success:
vidcap.set(cv2.CAP_PROP_POS_MSEC,(count*1000)) # added this line
success,image = vidcap.read()
print ('Read a new frame: ', success)
cv2.imwrite( pathOut + "\\frame%d.jpg" % count, image) # save frame as JPEG file
count = count + 1
if __name__=="__main__":
a = argparse.ArgumentParser()
a.add_argument("--pathIn", help="path to video")
a.add_argument("--pathOut", help="path to images")
args = a.parse_args()
print(args)
extractImages(args.pathIn, args.pathOut)
我通过Anaconda的Spyder软件使用Python。使用@ gshock在这个线程的问题中列出的原始代码,代码不起作用(python不会读取mp4文件)。所以我下载了OpenCV 3.2,从bin文件夹中拷贝了“opencv_ffmpeg320.dll”和“opencv_ffmpeg320_64.dll”。我将这两个dll文件粘贴到Anaconda的“dll”文件夹。
蟒蛇也有一个“pckgs”文件夹…我复制并粘贴了整个“OpenCV 3.2”文件夹,我下载到Anaconda“pckgs”文件夹。
最后,Anaconda有一个“Library”文件夹,其中有一个“bin”子文件夹。我将“opencv_ffmpeg320.dll”和“opencv_ffmpeg320_64.dll”文件粘贴到该文件夹。
关闭并重新启动Spyder后,代码正常工作。我不确定这三种方法中哪一种有效,我也懒得回去研究。但它确实有效,干杯!
这是对python 3之前答案的一个调整。x来自@ gsurprised,我想把它贴在评论里,但是没有足够的声誉
import sys
import argparse
import cv2
print(cv2.__version__)
def extractImages(pathIn, pathOut):
vidcap = cv2.VideoCapture(pathIn)
success,image = vidcap.read()
count = 0
success = True
while success:
success,image = vidcap.read()
print ('Read a new frame: ', success)
cv2.imwrite( pathOut + "\\frame%d.jpg" % count, image) # save frame as JPEG file
count += 1
if __name__=="__main__":
print("aba")
a = argparse.ArgumentParser()
a.add_argument("--pathIn", help="path to video")
a.add_argument("--pathOut", help="path to images")
args = a.parse_args()
print(args)
extractImages(args.pathIn, args.pathOut)