我对这个平台还是个新手.我会尽我所能说清楚的.我的目标是将目录中的帧转换为视频.出于某种原因,它在每15帧中包含第一帧.以下是我的代码:

import cv2
import glob
from pathlib import Path

def play_frames(frames_dir, fps, output_video_path):
    # Get the list of frames in the directory
    frames = sorted(glob.glob(str(frames_dir / '*.png')))

    # Read the first frame to get the width and height
    frame = cv2.imread(frames[0])
    height, width, _ = frame.shape

    # Create the video writer object
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_writer = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))

    for frame_path in frames:
        # Read the frame
        frame = cv2.imread(frame_path)

        # Display the frame
        cv2.imshow('Frame', frame)
        cv2.waitKey(int(1000 / fps))

        # Write the frame to the video
        video_writer.write(frame)

    # Release the video writer
    video_writer.release()
    cv2.destroyAllWindows()

# Directory path
frames_dir = Path('./detected_objects')
fps = 15
output_video_path = 'vid2output.mp4'

# Play the frames and save as video
play_frames(frames_dir, fps, output_video_path)

我在这里try 了无数的代码,但所有的代码(我try 过的)都在每帧每秒添加第一帧.我做错了什么?有没有其他方法可以将图像转换为视频,而不需要在每个fps中添加第一帧?(我已经判断了目录中的帧,它们都是正确的顺序.)

感谢大家的帮助,并乐于提供更多的信息!

最新情况: 我在代码中添加了分辨率大小,但第一帧每秒都显示.

import cv2
import glob
import numpy as np
from pathlib import Path

def play_frames(frames_dir, fps, output_video_path):
    # Get the list of frames in the directory
    frames = sorted(glob.glob(str(frames_dir / '*.png')))

    # Read the first frame to get the width and height
    frame = cv2.imread(frames[0])
    height, width, _ = frame.shape

    # Calculate the maximum width and height among all frames
    max_width = width
    max_height = height

    for frame_path in frames[1:]:
        # Read each frame to get its dimensions
        frame = cv2.imread(frame_path)
        height, width, _ = frame.shape

        # Update the maximum width and height if necessary
        max_width = max(max_width, width)
        max_height = max(max_height, height)

    # Create the video writer object
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_writer = cv2.VideoWriter(output_video_path, fourcc, fps, (max_width, max_height))

    # Resize and pad each frame before writing to the video
    for frame_path in frames:
        frame = cv2.imread(frame_path)

        # Resize the frame to the maximum dimensions
        resized_frame = cv2.resize(frame, (max_width, max_height))

        # Write the resized frame to the video
        video_writer.write(resized_frame)

    # Release the video writer
    video_writer.release()

# Directory path
frames_dir = Path('./detected_objects')
fps = 15
output_video_path = 'vid1output.mp4'

# Play the frames and save as video
play_frames(frames_dir, fps, output_video_path)

推荐答案

sorted(),则在给出字符串列表时,对它们进行as strings排序,而不是作为数字.以下说明了这是一个问题的原因:

>>> ', '.join(sorted([str(i) for i in range(67)]))
'0, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 7, 8, 9'

看到了吧,我们得到的是0, 1, 10,而不是0, 1, 2?同样,我们在1920之间跳回到2;在2930之间跳到3;在3940之间跳到4;等等.很容易看出这看起来像是跳回第1帧,尽管它实际上并不是在做什么.


您需要填充您的文件名,使它们都具有相同的位数(001.png而不是1.png),或者告诉Python按数字而不是字符串对它们进行排序.

要执行后一种操作,请对sorted使用key参数,并将其作为参数传递给提取正确数字的函数.例如:

import os.path

def file_number(filename):
    return int(os.path.basename(filename).split('.')[0])

frames = sorted(glob.glob(str(frames_dir / '*.png')), key=file_number)

Python相关问答推荐

如何终止带有队列的Python进程?+ 队列大小的错误?

不允许AMBIMA API请求方法

Python中的负前瞻性regex遇到麻烦

如何使用没有Selenium的Python在百思买着陆页面上处理国家/地区 Select ?

@Property方法上的inspect.getmembers出现意外行为,引发异常

对某些列的总数进行民意调查,但不单独列出每列

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

两个pandas的平均值按元素的结果串接元素.为什么?

根据列值添加时区

isinstance()在使用dill.dump和dill.load后,对列表中包含的对象失败

在matplotlib中删除子图之间的间隙_mosaic

Flash只从html表单中获取一个值

巨 Python :逆向猜谜游戏

用两个字符串构建回文

根据客户端是否正在传输响应来更改基于Flask的API的行为

如何根据rame中的列值分别分组值

在第一次调用时使用不同行为的re. sub的最佳方式

极点替换值大于组内另一个极点数据帧的最大值

如何提高Pandas DataFrame中随机列 Select 和分配的效率?

按条件计算将记录拆分成两条记录