EDVR模型
视频超分辨的训练以及测试都是分解成帧,利用单帧图像进行深度学习,而并非直接利用整个视频实现
1.Colab使用教程
2. 在Colab中加载盘
from google.colab import drive
drive.mount('/content/drive/')
3. 测试EDVR
- 运行
develop
# 指定当前的工作文件夹
import os
# 此处为google drive中的文件路径,drive为之前指定的工作根目录,要加上
os.chdir("/content/drive/My Drive/app/edvr_master/codes/models/archs/dcn")
!python3 setup.py develop
- 安装需要的库
!pip install numpy opencv-python lmdb pyyaml
!pip install tb-nightly future
-
下载测试数据集
Vid4Google Drive 该数据集已经分帧了,所以直接移入
./datasets文件夹即可 -
执行测试文件
import os
os.chdir("/content/drive/My Drive/app/edvr_master/codes/")
!python "test_Vid4_REDS4_with_GT.py"
4. 利用Matlab实现视频的分帧与合成
4.1 帧合成视频
function CreatVideoFromPic(dn, picformat,aviname)
% CreatVideoFromPic(dn, picformat,aviname)
% 将某个文件夹下某种格式的所有图片合成为视频文件
% dn : 存储图片的文件夹
% picformat : 要读取的图片的格式,如png、jpg等形式,字符串数组
% aviname : 存储的视频的文件名
% example : CreatVideoFromPic( './', 'png','presentation.avi');
if ~exist(dn, 'dir')
error('dir not exist!!!!');
end
picname=fullfile( dn, strcat('*.',picformat));
picname=dir(picname);
aviobj = VideoWriter(aviname);
open(aviobj);
for i=1:length(picname)
picdata=imread( fullfile(dn, (picname(i,1).name)));
if ~isempty( aviobj.Height)
if size(picdata,1) ~= aviobj.Height || size(picdata,2) ~= aviobj.Width
close(aviobj);
delete( aviname )
error('所有图片的尺寸要相同!!');
end
end
writeVideo(aviobj,picdata);
end
close(aviobj);
end
4.2 视频分帧
function Video2Img(dn, storage, type)
% 该函数用于将视频分帧成图像
% dn为视频路径,如 'E:\PRACTICE\some\video.avi'
% storage是图像存储路径, 如 'E:\PRACTICE\some\save\'
% type是图片类型,如 'png' 'jpg' 等
videoObj = VideoReader(dn);%导入视频,注意路径下命名不要加扩展名。
nframes = get(videoObj, 'NumberOfFrames');%获取视频文件帧个数
if(nframes > 60)
nframes = 60; % 如果视频超过60帧,只取前60帧
end
for k = 1 : nframes
currentFrame = read(videoObj, k);
imwrite(currentFrame,strcat(storage, num2str(k,'%08d'),'.',type),type); % 保存帧
end
5.Matlab下采样
function generate_LR_Vimeo90K(scales, source_root, target_root)
%% 下采样图片
% scales 为采样倍数 e.g 4
% source_root 源文件夹 e.g './drone/'
% target_root 下采样后的目标文件夹 e.g './drone/x4'
up_scale = scales;
mod_scale = scales;
idx = 0;
filepaths = dir(strcat(source_root,'*.png'));
for i = 1 : length(filepaths)
[~,imname,ext] = fileparts(filepaths(i).name);
folder_path = filepaths(i).folder;
save_LR_folder = target_root;
if ~exist(save_LR_folder, 'dir')
mkdir(save_LR_folder);
end
if isempty(imname)
disp('Ignore . folder.');
elseif strcmp(imname, '.')
disp('Ignore .. folder.');
else
idx = idx + 1;
str_rlt = sprintf('%d\t%s.\n', idx, imname);
fprintf(str_rlt);
% read image
img = imread(fullfile(folder_path, [imname, ext]));
img = im2double(img);
% modcrop
img = modcrop(img, mod_scale);
% LR
im_LR = imresize(img, 1/up_scale, 'bicubic');
if exist('save_LR_folder', 'var')
imwrite(im_LR, fullfile(save_LR_folder, [imname, '.png']));
end
end
end
end
%% modcrop
function img = modcrop(img, modulo)
if size(img,3) == 1
sz = size(img);
sz = sz - mod(sz, modulo);
img = img(1:sz(1), 1:sz(2));
else
tmpsz = size(img);
sz = tmpsz(1:2);
sz = sz - mod(sz, modulo);
img = img(1:sz(1), 1:sz(2),:);
end
end
6.测试过程
获取高清视频
将高清视频分帧为
GT图像,即为Groud True图像,可理解为基准图像吧,到时候用超分结果比较将GT图像下采样
利用EDVR官方模型超分辨得到4倍超分辨图像,利用
PNSR和SSIM评价超分结果人眼观察: 可单帧看,也可合成视频查看
7.遇到的问题
RuntimeError: shape '[1, 7, -1, 90, 170]' is invalid for input of size 13789440
我觉得这和输入的数据集有关,观察下Vid4数据的图片长度和宽带,为:
180*144 176*144 180*120 , 而我的数据集为:341*180,显然341不是偶数倍
所以,我决定对GT图像进行裁剪,是其除以4后可以为整数倍,裁剪代码如下:
%% crop the im into 1360*720
clear;clc;
file_path = 'D:\PycharmDOC\Video_SR\EDVR\datasets\Vid4\GT\drone\'; % 设定你存放图片的目录
img_path_list = dir(strcat(file_path, '*.png')); % 选后缀为 .png 的图片
img_num = length(img_path_list); %获得图片数量
for j = 1:img_num
image_name = img_path_list(j).name;
image = imread(strcat(file_path, image_name));
crop_image = imcrop(image, [0, 0, 1360, 720]); % 使用 imcrop() 函数来裁剪图片,第二个参数的格式为 [XMIN YMIN WIDTH HEIGHT]
imwrite(crop_image, strcat('D:\PycharmDOC\Video_SR\EDVR\datasets\Vid4\GT\drones\', image_name)); % 保存文件
end
果然是图片输入有问题,修改后成功测试了自己的图像!

write by Gqq

浙公网安备 33010602011771号