简体   繁体   English

每 10 秒后从视频文件中捕获一帧

[英]capture one frame from a video file after every 10 seconds

I want to capture from a video file one frame after every 10 seconds, So if anyone can help me for that i will be very thankful.我想每 10 秒后从视频文件中捕获一帧,所以如果有人可以帮助我,我将非常感激。 my python code is like that:我的python代码是这样的:

import cv2


print(cv2.__version__)
vidcap = cv2.VideoCapture('Standoff.avi')
vidcap.set(cv2.CAP_PROP_POS_MSEC,96000)  
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
  cv2.waitKey(200)
  count += 1

Here i am capturing frames after 10 conversion of frames.you can use time function and similarly capture frames in if condition statement在这里,我在 10 帧转换后捕获帧。您可以使用时间函数并在 if 条件语句中类似地捕获帧

import cv2

vidcap = cv2.VideoCapture('testing.mp4')
count = 0
success = True
fps = int(vidcap.get(cv2.CAP_PROP_FPS))

while success:
    success,image = vidcap.read()
    print('read a new frame:',success)
    if count%(10*fps) == 0 :
         cv2.imwrite('frame%d.jpg'%count,image)
         print('successfully written 10th frame')
    count+=1

If you can get the framerate of the video from the file the following should work (You may need to check the syntax as I have not tested it):如果您可以从文件中获取视频的帧率,则以下内容应该可以工作(您可能需要检查语法,因为我尚未对其进行测试):

import cv2

cap = cv2.VideoCapture('Standoff.avi')
framerate = cap.get(cv2.cv.CV_CAP_PROP_FPS)
framecount = 0

while(True):
    # Capture frame-by-frame
    success, image = cap.read()
    framecount += 1

    # Check if this is the frame closest to 10 seconds
    if framecount == (framerate * 10)
      framecount = 0
      cv2.imshow('image',image)

    # Check end of video
    if cv2.waitKey(1) & 0xFF == ord('q'):
          break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

You should use this code.您应该使用此代码。 Code gets fps and multiplies it by 10, that defines that, how many frames creates 10 seconds of video.代码获取 fps 并将其乘以 10,这定义了创建 10 秒视频的帧数。 Lets call that multiplier.让我们称之为乘数。 Script captures first frame and multiplies frame counter by multiplier.脚本捕获第一帧并将帧计数器乘以乘数。 Script captures frame every 10 second.脚本每 10 秒捕获一次帧。

import cv2 as cv
import os
from time import time

file = "videos/mouthwash.avi"

if not os.path.isfile(file):
print("File not found!")

# images forder name
folder_name = base=os.path.basename(file) + " frames"

# create folder for images in current path if not exists
current_path = os.getcwd()
folder_path = os.path.join(current_path, folder_name)

if not os.path.exists(folder_path):
    os.mkdir(folder_path)

cap = cv.VideoCapture(file)

total_frame = int(cap.get(cv.CAP_PROP_FRAME_COUNT))

# save frame every # seconds
seconds = 10
fps = cap.get(cv.CAP_PROP_FPS) # Gets the frames per second
# calculates number of frames that creates 10 seconds of video
multiplier = fps * seconds

# Check if camera opened successfully
if (cap.isOpened()== False):
    print("Error opening video stream or file")

frame_counter = 1

while frame_counter <= total_frame:

    cap.set(cv.CAP_PROP_POS_FRAMES, frame_counter)

    ret, frame = cap.read()

    # save frame
    # file path
    file_path = os.path.join(folder_path, str(time()) + ".jpg")
    cv.imwrite(file_path, frame)

    frame_counter += multiplier

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM