将逐帧图片生成mp4格式视频——pyhton实现

import cv2
img = cv2.imread('/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/s0.png')
imgInfo = img.shape
size = (imgInfo[1],imgInfo[0])
print(size)
# 参数:1 file name 2 编码器 3 帧率 4 size
videoWrite = cv2.VideoWriter('mot.mp4',cv2.VideoWriter_fourcc(*'mp4v'),10,size)# 写入对象

# 0-197张图片
for i in range(0,198):
    fileName = '/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/s'+str(i)+'.png'
    img = cv2.imread(fileName)
    videoWrite.write(img) # 将图片写入视频
videoWrite.release()
print('end!')


# import cv2
# import glob
# img_array = []
# for filename in glob.glob('/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/*.png'):
#     # 需要将路径更改为自己存储图片的路径
#     img = cv2.imread(filename)
#     height, width, layers = img.shape
#     size = (width, height)
#     img_array.append(img)
# out = cv2.VideoWriter('mot.avi', cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
# for i in range(len(img_array)):
#     out.write(img_array[i])
# out.release()

********************************************文件名补0操作***********************************************

import cv2
# img = cv2.imread('/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/s0.png')
img = cv2.imread('F:/MOT/3D-Multi-Object-Tracker/evaluation/data/gt/davis/davis_unsupervised_val/bike-packing/00000.png')
imgInfo = img.shape
size = (imgInfo[1],imgInfo[0])
print(size)
# 参数:1 file name 2 编码器 3 帧率 4 size
videoWrite = cv2.VideoWriter('mot.mp4',cv2.VideoWriter_fourcc(*'mp4v'),10,size)# 写入对象

# 1-198张图片
for i in range(0,69):
    num = "%05d" % i    # 补0操作(5位数补0)
    # fileName = '/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/s'+str(num)+'.png'
    fileName = 'F:/MOT/3D-Multi-Object-Tracker/evaluation/data/gt/davis/davis_unsupervised_val/bike-packing/' + str(num) + '.png'
    img = cv2.imread(fileName)
    videoWrite.write(img) # 将图片写入视频
videoWrite.release()
print('end!')


# import cv2
# import glob
# img_array = []
# for filename in glob.glob('/media/lc/LENOVO_USB_HDD/MOT/SimpleTrack/SimpleTrack/res_img/*.png'):
#     # 需要将路径更改为自己存储图片的路径
#     img = cv2.imread(filename)
#     height, width, layers = img.shape
#     size = (width, height)
#     img_array.append(img)
# out = cv2.VideoWriter('mot.avi', cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
# for i in range(len(img_array)):
#     out.write(img_array[i])
# out.release()

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Transformer是一种基于自注意力机制的神经网络模型,广泛应用于自然语言处理任务中,如机器翻译、文本生成等。下面是Transformer预测Python实现的简要介绍: 1. 导入所需的库和模块: ```python import torch import torch.nn as nn import torch.optim as optim from torchtext.data import Field, TabularDataset, BucketIterator ``` 2. 定义Transformer模型的编码器和解码器: ```python class Encoder(nn.Module): def __init__(self, input_dim, hid_dim, n_layers, n_heads, pf_dim, dropout, device): super().__init__() # 编码器的实现 class Decoder(nn.Module): def __init__(self, output_dim, hid_dim, n_layers, n_heads, pf_dim, dropout, device): super().__init__() # 解码器的实现 class Transformer(nn.Module): def __init__(self, encoder, decoder, src_pad_idx, trg_pad_idx, device): super().__init__() self.encoder = encoder self.decoder = decoder self.src_pad_idx = src_pad_idx self.trg_pad_idx = trg_pad_idx self.device = device ``` 3. 定义模型的前向传播方法: ```python def forward(self, src, trg): # 前向传播的实现 ``` 4. 定义模型的初始化方法: ```python def init_weights(self): # 权重初始化的实现 def init_weights(m): for name, param in m.named_parameters(): if 'weight' in name: nn.init.normal_(param.data, mean=0, std=0.01) else: nn.init.constant_(param.data, 0) ``` 5. 定义模型的训练和评估方法: ```python def train_model(model, iterator, optimizer, criterion, clip): # 模型训练的实现 def evaluate_model(model, iterator, criterion): # 模型评估的实现 ``` 6. 定义模型的超参数和优化器: ```python INPUT_DIM = len(SRC.vocab) OUTPUT_DIM = len(TRG.vocab) HID_DIM = 256 ENC_LAYERS = 3 DEC_LAYERS = 3 ENC_HEADS = 8 DEC_HEADS = 8 ENC_PF_DIM = 512 DEC_PF_DIM = 512 ENC_DROPOUT = 0.1 DEC_DROPOUT = 0.1 enc = Encoder(INPUT_DIM, HID_DIM, ENC_LAYERS, ENC_HEADS, ENC_PF_DIM, ENC_DROPOUT, device) dec = Decoder(OUTPUT_DIM, HID_DIM, DEC_LAYERS, DEC_HEADS, DEC_PF_DIM, DEC_DROPOUT, device) model = Transformer(enc, dec, SRC_PAD_IDX, TRG_PAD_IDX, device).to(device) model.apply(init_weights) optimizer = optim.Adam(model.parameters()) criterion = nn.CrossEntropyLoss(ignore_index = TRG_PAD_IDX) ``` 这只是一个简单的Transformer预测Python实现的示例,具体实现还需要根据具体任务和数据进行调整和优化。如果你有更具体的问题或者需要更详细的代码实现,请提供更多的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值