从视频中提取帧时,我面临问题。视频几乎有90k帧,当提取25k帧时,代码将显示错误

发布于 2025-02-13 20:04:50 字数 1858 浏览 0 评论 0原文

代码在这里:

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()
        # 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 = 'E:/Fish  Project DataSet/06-30/D01_20210630002157.mp4'
    output_loc = 'E:/Fish  Project DataSet/06-30 pictures/D01_20210630002157  frames/'
    video_to_frames(input_loc, output_loc)

显示错误

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

Code is here:

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()
        # 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 = 'E:/Fish  Project DataSet/06-30/D01_20210630002157.mp4'
    output_loc = 'E:/Fish  Project DataSet/06-30 pictures/D01_20210630002157  frames/'
    video_to_frames(input_loc, output_loc)

Displaying error

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2025-02-20 20:04:51

由于您试图编写一个空的帧而引起的错误。

您必须确保两件事:

  1. 视频打开正确。
  2. 框架正在正确返回。

您可以使用以下代码。

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)
    print ("Converting video..n")
    # Start converting the video
    if cap.isOpened():
        count = 0
        # Extract the frame
        while True:
            ret, frame = cap.read()
            if ret:
                # Write the results back to output location.
                cv2.imwrite(f"{output_loc}{'/%#05d.jpg' % (count + 1)}", frame)
            else:
                print("Can't receive frame (stream end?). Exiting ...")
                break
            # If there are no more frames left
            count = count + 1
            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 = 'E:/Fish  Project DataSet/06-30/D01_20210630002157.mp4'
    output_loc = 'E:/Fish  Project DataSet/06-30 pictures/D01_20210630002157  frames/'
    video_to_frames(input_loc, output_loc)

请确保您的视频不会损坏。

This error is caused because you are trying to write a frame that is empty.

You have to ensure two things:

  1. The video is being opened correctly.
  2. The frame is being returned correctly.

You can use the following code.

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)
    print ("Converting video..n")
    # Start converting the video
    if cap.isOpened():
        count = 0
        # Extract the frame
        while True:
            ret, frame = cap.read()
            if ret:
                # Write the results back to output location.
                cv2.imwrite(f"{output_loc}{'/%#05d.jpg' % (count + 1)}", frame)
            else:
                print("Can't receive frame (stream end?). Exiting ...")
                break
            # If there are no more frames left
            count = count + 1
            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 = 'E:/Fish  Project DataSet/06-30/D01_20210630002157.mp4'
    output_loc = 'E:/Fish  Project DataSet/06-30 pictures/D01_20210630002157  frames/'
    video_to_frames(input_loc, output_loc)

Please ensure that your video is not corrupted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文