批量将seq文件转换成avi文件

文件夹结构
├─src
│ ├─子文件夹1
│ │ ├─xxx.seq
│ │ ├─xxx.seq
│ ├─子文件夹2
│ │ ├─xxx.seq
│ │ ├─xxx.seq

├─dst
会自动生成dst文件夹,且dst文件夹结构与src文件夹结构一致

import os
import cv2
import struct
import numpy as np

def read_header(ifile):
    feed = ifile.read(4)
    norpix = ifile.read(24)
    version = struct.unpack('@i', ifile.read(4))
    length = struct.unpack('@i', ifile.read(4))
    assert (length != 1024)
    descr = ifile.read(512)
    params = [struct.unpack('@i', ifile.read(4))[0] for i in range(0, 9)]
    fps = struct.unpack('@d', ifile.read(8))
    # skipping the rest
    ifile.read(432)
    image_ext = {100: 'raw', 102: 'jpg', 201: 'jpg', 1: 'png', 2: 'png'}
    return {'w': params[0], 'h': params[1],
            'bdepth': params[2],
            'ext': image_ext[params[5]],
            'format': params[5],
            'size': params[4],
            'true_size': params[8],
            'num_frames': params[6]}


def read_seq(path):
    ifile = open(path, 'rb')
    params = read_header(ifile)
    bytes = open(path, 'rb').read()

    # this is freaking magic, but it works
    extra = 8
    s = 1024
    seek = [0] * (params['num_frames'] + 1)
    seek[0] = 1024

    images = []

    # this crashes in the last iteration, so we reduce it by one iteration
    for i in range(0, params['num_frames'] - 1):
        tmp = struct.unpack_from('@I', bytes[s:s + 4])[0]
        s = seek[i] + tmp + extra
        if i == 0:
            val = struct.unpack_from('@B', bytes[s:s + 1])[0]
            if val != 0:
                s -= 4
            else:
                extra += 8
                s += 8
        seek[i + 1] = s
        nbytes = struct.unpack_from('@i', bytes[s:s + 4])[0]
        I = bytes[s + 4:s + nbytes]

        img = cv2.imdecode(np.frombuffer(I, dtype=np.uint8), cv2.IMREAD_COLOR)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        images.append(img)
    return images


def convert_seq_to_avi(seq_path, dest_path):
    image_list = read_seq(seq_path)
    sample_img = image_list[0]
    h, w, _ = sample_img.shape

    video = cv2.VideoWriter(dest_path, cv2.VideoWriter_fourcc(*'XVID'), 25, (w, h), True)
    for img in image_list:
        video.write(img)
    video.release()


def convert_olympic_dataset(dataset_root, dataset_converted_root):
    if not os.path.exists(dataset_converted_root):
        print('Creating new folder for converted dataset')
        os.makedirs(dataset_converted_root)

    for root, dirs, files in os.walk(dataset_root):
        for filename in files:
            if filename.endswith('.seq'):
                seq_path = os.path.join(root, filename)
                relative_path = os.path.relpath(seq_path, dataset_root)
                dest_path = os.path.join(dataset_converted_root, os.path.splitext(relative_path)[0] + '.avi')

                dest_dir = os.path.dirname(dest_path)
                if not os.path.exists(dest_dir):
                    os.makedirs(dest_dir)

                print('Converting', seq_path, 'to', dest_path)
                convert_seq_to_avi(seq_path, dest_path)
# 设置输入和输出文件夹路径
dataset_root = 'src'
dataset_converted_root = 'dst'

convert_olympic_dataset(dataset_root, dataset_converted_root)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Python中,要读取seq文件,你可以使用file对象的read()方法或者readlines()方法来实现。首先,你需要使用open()函数打开seq文件并创建一个file对象,然后使用read()方法来读取整个文件内容,或者使用readlines()方法逐行读取文件内容。下面是读取seq文件的示例代码: ``` file_object = open('seq文件路径', 'r') try: # 读取整个文件内容 all_the_text = file_object.read() # 或者逐行读取文件内容 lines = file_object.readlines() finally: file_object.close() ``` 在这个示例中,`file_object`是一个file对象,`'seq文件路径'`是你要读取的seq文件的路径。通过调用`read()`方法,你可以将整个文件内容作为一个字符串存储在`all_the_text`变量中。而使用`readlines()`方法,你可以逐行读取文件内容,并将每一行作为一个字符串存储在`lines`列表中。 请注意,在读取完文件后,记得使用`close()`方法关闭文件对象,以释放资源。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [pyhtslib:使用htslib从Python文件读取HTS](https://download.csdn.net/download/weixin_42118701/18892547)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python进阶(二十五)Python读写文件](https://blog.csdn.net/sunhuaqiang1/article/details/69389116)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值