• Pytorch将数据(张量)写入视频


    1. 安装与报错解决

    • 安装
    pip install PyAV
    
    • 1
    • 报错问题1的解决
      报错信息如下:
    ...tf2_py38\lib\site-packages\torchvision\io\video.py", line 41, in _check_av_available
        raise av
    ImportError: PyAV is not installed, and is necessary for the video operations in torchvision.
    See https://github.com/mikeboers/PyAV#installation for instructions on how to
    install PyAV on your system.
    
    
    Process finished with exit code 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 报错问题2解决
      至于怎么解决PyAV要求的conda的python版本问题
      可以参考资料【1】;

    • 报错问题3解决
      这个需要参考我这篇博客,哈哈哈

    stream = container.add_stream(video_codec, rate=fps)
      File "av\\container\\output.pyx", line 67, in av.container.output.OutputContainer.add_stream
      File "av\\codec\\codec.pyx", line 185, in av.codec.codec.Codec.__cinit__
      File "av\\codec\\codec.pyx", line 194, in av.codec.codec.Codec._init
    av.codec.codec.UnknownCodecError: libx264
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 最后,直接安装PyAV,安装成功。。。
    Collecting PyAV
      Downloading pyav-12.0.3-cp39-cp39-win_amd64.whl.metadata (2.5 kB)
    Downloading pyav-12.0.3-cp39-cp39-win_amd64.whl (19.2 MB)
       ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.2/19.2 MB 223.2 kB/s eta 0:00:00
    Installing collected packages: PyAV
    Successfully installed PyAV-12.0.3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2. torch.io程序

    给出一个写入tensor的demo程序,这个可以用于将tensor数据转换为视频。

    import torch
    import torchvision.io as io
    # tensor生成程序
    ...
    # 将tensor写入视频-设置写入路径
    video_path = "path/to/video2.mp4"
    
    fps = 1000
    # Write the tensor to the video file
    io.write_video(video_path, tensor, fps)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. cv2 img文件转视频

    实际上,有时候还是会报错,这里分享个从tensor生成的图片转视频的代码吧:

    import cv2
    import os
    
    # Path to the folder containing the images
    folder_path = r'C:\Users\86139\PycharmProjects\pythonProject_at_tsinghua\Event_based_camera_data_extraction\IEBCS-main\examples\00_video_2_events\img3'
    
    # Get a list of all image files in the folder
    image_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
    
    # Sort the image files in ascending order
    image_files.sort()
    
    # Define the video properties
    width, height = 0, 0  # Initialize width and height to 0
    fps = 1000  # Frame rate of 1000Hz
    
    # Create a VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_writer = None
    
    # Iterate over the image files and write each frame to the video file
    for image_file in image_files:
        # Read the image
        image_path = os.path.join(folder_path, image_file)
        frame = cv2.imread(image_path)
    
        # Get the width and height from the first frame
        if width == 0 and height == 0:
            height, width, _ = frame.shape
    
        # Create the VideoWriter object on the first iteration
        if video_writer is None:
            video_writer = cv2.VideoWriter('./output55.mp4', fourcc, fps, (width, height))
    
        # Write the frame to the video file
        video_writer.write(frame)
    
    # Release the VideoWriter object
    if video_writer is not None:
        video_writer.release()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    参考资料

    【1】csdn-conda怎么更新虚拟环境的python版本

  • 相关阅读:
    Kafka Log存储解析以及索引机制
    4、建造者模式
    76. 最小覆盖子串
    【CSS】笔记7-HTML5新特征\CSS3
    wininet,winhttp,xmlhttprequest,各版本区别 《转》
    C++笔记之vector的初始化以及assign()方法
    cadence SPB17.4 - orcad - Capture CIS export BOM
    波动数列(蓝桥杯)
    人体的神经系统图 分布,人体脑神经系统分布图
    【附源码】计算机毕业设计JAVA装修网站
  • 原文地址:https://blog.csdn.net/u013537270/article/details/136435551