批量将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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值