批量逐帧编辑视频

批量给视频添加大雾效果

- 前言:
一切起因都是为了训练模型,而进行的数据增强。这篇代码可以将输入的视频批量添加雾气。
- 代码:

import math
import os
import cv2
import math
from PIL import Image
import numpy as np


def del_files(path):
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".jpg"):  # 指定要删除的格式,这里是jpg 可以换成其他格式
                os.remove(os.path.join(root, name))
                print("Delete File: " + os.path.join(root, name))


def frame2video(im_dir, video_dir, fps):
    im_list = os.listdir(im_dir)
    im_list.sort(key=lambda x: int(x.replace("frame", "").split('.')[0]))  # 最好再看看图片顺序对不
    img = Image.open(os.path.join(im_dir, im_list[0]))
    img_size = img.size  # 获得图片分辨率,im_dir文件夹下的图片分辨率需要一致

    # fourcc = cv2.cv.CV_FOURCC('M','J','P','G') #opencv版本是2
    # fourcc = cv2.VideoWriter_fourcc(*'XVID')  # opencv版本是3
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
    videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size, True)
    # count = 1
    for i in im_list:
        im_name = os.path.join(im_dir + i)
        frame = cv2.imdecode(np.fromfile(im_name, dtype=np.uint8), -1)
        videoWriter.write(frame)
        # count+=1
        # if (count == 200):
        #     print(im_name)
        #     break
    videoWriter.release()
    print('finish')


def demo(input_img_path, save_path):
    img = cv2.imread(input_img_path)
    img_f = img / 255.0
    (row, col, chs) = img.shape

    A = 0.9  # 亮度
    beta = 0.05  # 雾的浓度
    size = math.sqrt(max(row, col))  # 雾化尺寸
    center = (row // 2, col // 2)  # 雾化中心
    for j in range(row):
        for l in range(col):
            d = -0.04 * math.sqrt((j - center[0]) ** 2 + (l - center[1]) ** 2) + size
            td = math.exp(-beta * d)
            img_f[j][l][:] = img_f[j][l][:] * td + A * (1 - td)

    # cv2.imshow("src", img)
    # cv2.imshow("dst", img_f)
    cv2.imwrite(save_path, img_f * 255)
    # cv2.waitKey()


def video2frame(videos_path, frames_save_path, time_interval, epoch):
    """
    :param videos_path: 视频的存放路径
    :param frames_save_path: 视频切分成帧之后图片的保存路径
    :param time_interval: 保存间隔
    :return:
    """
    vidcap = cv2.VideoCapture(videos_path)
    success, image = vidcap.read()
    # print('success:', success)
    count = 0
    while success:
        success, image = vidcap.read()
        count += 1
        # print('success:', success)
        # print(count)
        if count % time_interval == 0:
            if success:
                #  cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "\\frame%d_%d.jpg" % (epoch, count))
                cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "\\frame%d.jpg" % count)
                print(frames_save_path + "\\frame%d.jpg" % count)
                demo(frames_save_path + "\\frame%d.jpg" % count, frames_save_path + "\\frame%d.jpg" % count)


if __name__ == '__main__':
    # videos_path = 'D:\测试\测试视频01.mp4'
    endTimes = 3
    fps = 30
    for i in range(1, endTimes):
        # videos_path = 'D:\\finalData\\selectData\\aim\\aim(2).mp4'
        videos_path1 = 'D:\\finalData\\selectData\\aim\\aim('
        videos_path2 = str(i)
        videos_path3 = ').mp4'
        frames_save_path = 'D:\\finalData\\frame\\'
        videos_save_path1 = 'D:\\finalData\\fog\\aim\\fog_test'
        videos_save_path2 = str(i)
        videos_save_path3 = '.mp4'
        videos_save_path = videos_save_path1 + videos_save_path2 + videos_save_path3
        videos_path = videos_path1 + videos_path2 + videos_path3
        print("input video path:" + videos_path)
        print("output video path:" + videos_save_path)
        time_interval = 2  # 隔一帧保存一次
        epoch = i
        video2frame(videos_path, frames_save_path, time_interval, epoch)
        frame2video(frames_save_path, videos_save_path, fps)
        del_files(frames_save_path)

须要修改的只有四个参数:输入视频路径、视频帧存储路径、输出视频路径、中止次数
1、video_path1、video_path2、video_path3
2、frames_save_path
3、videos_save_path1、videos_save_path2、videos_save_path3
4、endTimes
- 写在后面:
代码来源于网络,模块名也没改,原作者看到我先致个谢,可以联系我加上出处

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值