【问题标题】:Recording a video from images and the respective annotation files从图像和相应的注释文件中录制视频
【发布时间】:2021-07-22 14:13:57
【问题描述】:

我将图像以另一种格式存储在一个文件夹中。在一个单独的文件夹中,我有包含应该在图像上绘制的矩形坐标的文本文件。我使用read_file 函数从文件中读取 NumPy 数组。

从下面给出的脚本中,我可以成功地在图像上一个接一个地绘制坐标。但这只是一个接一个地生成图像,而我想要上述图像的视频,即由下面给出的脚本生成的图像。

执行上述操作的代码 sn-p:

for idx, val in enumerate(img_list):
    image = read_file(img_list[idx])
    with open(annot_list[idx], 'r') as f:
        lines = [idx.rstrip('\n') for idx in f.readlines()]
        annots = [list(map(int, idx.split(','))) for idx in lines]
    plt.figure()
    plt.imshow(image, cmap='gray')
    for jdx, annot in enumerate(annots):
        x1 = annots[jdx][0]
        y1 = annots[jdx][1]
        w = annots[jdx][2] - x1
        h = annots[jdx][3] - y1
        rect = patches.Rectangle((x1, y1), w, h, linewidth=3, edgecolor='r', facecolor='none')
        plt.gca().add_patch(rect)
    plt.show()

这里,img_listannot_list变量包含图像文件的名称和各自注释文件的名称。

这会一个接一个地生成一组图像。如何生成这些帧的视频?我应该调整什么?我看过这个thread,但我似乎无法弄清楚我需要更改什么才能生成视频。我不一定要保存视频,也不想保存帧列表中的帧,因为我有大约 5000 张图像。

谢谢你,我很感激我能得到任何帮助。

【问题讨论】:

  • opencv 有一个 VideoWriter 类。您可以将一个图像一个接一个地推送(假设帧速率恒定)。
  • 我看到了一些与此相关的线程以及 Python 的文档,但就是不知道该怎么做。你能帮忙吗?

标签: python numpy opencv matplotlib


【解决方案1】:

首先,在循环之前定义一个视频编写器。在循环中,显示图像后,定义一个图形和一个随机图,以便将图像保存到一个数组中。最后,将每张图片写入视频写入器。

out = cv2.VideoWriter('output.avi', -1, 20.0, (640, 480)) # Change the 640 and 480 to the dimensions of your images

for idx, val in enumerate(img_list):
    image = read_file(img_list[idx])
    with open(annot_list[idx], 'r') as f:
        lines = [idx.rstrip('\n') for idx in f.readlines()]
        annots = [list(map(int, idx.split(','))) for idx in lines]
    plt.figure()
    plt.imshow(image, cmap='gray')
    for jdx, annot in enumerate(annots):
        x1 = annots[jdx][0]
        y1 = annots[jdx][1]
        w = annots[jdx][2] - x1
        h = annots[jdx][3] - y1
        rect = patches.Rectangle((x1, y1), w, h, linewidth=3, edgecolor='r', facecolor='none')
        plt.gca().add_patch(rect)
    plt.show()

    fig = plt.figure() # Define figure
    fig.add_subplot(111) # Random plot
    fig.canvas.draw()
    img = np.fromstring(fig.canvas.tostring_rgb(), dtype='uint8', sep='') # Get image array
    out.write(img) # Write to video writer

out.release() # End the video writer

如果这部分不起作用:

    fig = plt.figure() # Define figure
    fig.add_subplot(111) # Random plot
    fig.canvas.draw()
    img = np.fromstring(fig.canvas.tostring_rgb(), dtype='uint8', sep='') # Get image array
    out.write(img) # Write to video writer

你可以试试:

    plt.savefig("temp.png")
    img = cv2.imread("temp.png")
    out.write(img) # Write to video writer

【讨论】:

  • 我在尝试建议的解决方案时收到以下错误:Traceback(最近一次调用最后一次):文件“D:/FH-AACHEN/Thesis/labelImg_test_Annotation/test_annotations.py”,第 41 行,在 img = np.fromstring(fig.canvas.tostring_rgb(), dtype='uint8', sep='') 文件 "C:\Users\DELL\AppData\Roaming\Python\Python37\site-packages\matplotlib \backends\backend_agg.py",第 415 行,在 tostring_rgb 返回 self.renderer.tostring_rgb() AttributeError: 'FigureCanvasTkAgg' 对象没有属性 'renderer' OpenCV: FFMPEG: format avi / AVI (Audio Video Interleaved)
  • @duddal 我进行了编辑;添加fig.canvas.draw()
  • 是的,我在查看 [这里]stackoverflow.com/questions/20051160/… 后尝试了这个,但它仍然不起作用。看起来,它无法读取图像的 NumPy 数组
  • 图像数组也是 (512,1536) 所以它不是 RGB 图像。 tostring_rgb() 有问题吗?
  • 保存的输出视频为0KB。因此我认为它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 2010-12-15
相关资源
最近更新 更多