2020-11-18 10:44:18 +08:00
|
|
|
import os
|
|
|
|
|
import cv2
|
|
|
|
|
import torch
|
|
|
|
|
import argparse
|
|
|
|
|
import numpy as np
|
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
from torch.nn import functional as F
|
2020-11-20 12:23:56 +08:00
|
|
|
import warnings
|
2020-11-20 19:08:47 +08:00
|
|
|
import _thread
|
2020-11-22 14:41:07 +08:00
|
|
|
import skvideo.io
|
2020-11-22 19:39:03 +08:00
|
|
|
from queue import Queue, Empty
|
2020-11-25 02:10:31 -04:00
|
|
|
import moviepy.editor
|
|
|
|
|
import shutil
|
2020-12-02 17:51:42 +08:00
|
|
|
warnings.filterwarnings("ignore")
|
2020-11-25 02:10:31 -04:00
|
|
|
|
|
|
|
|
def transferAudio(sourceVideo, targetVideo):
|
|
|
|
|
tempAudioFileName = "./temp/audio.mp3"
|
|
|
|
|
try:
|
|
|
|
|
# split audio from original video file and store in "temp" directory
|
|
|
|
|
if True:
|
|
|
|
|
# extract audio from video
|
|
|
|
|
if True:
|
|
|
|
|
video = moviepy.editor.VideoFileClip(sourceVideo)
|
|
|
|
|
audio = video.audio
|
|
|
|
|
# clear old "temp" directory if it exits
|
|
|
|
|
if os.path.isdir("temp"):
|
|
|
|
|
# remove temp directory
|
|
|
|
|
shutil.rmtree("temp")
|
|
|
|
|
# create new "temp" directory
|
|
|
|
|
os.makedirs("temp")
|
|
|
|
|
# write audio file to "temp" directory
|
|
|
|
|
audio.write_audiofile(tempAudioFileName)
|
|
|
|
|
os.rename(targetVideo, "noAudio_"+targetVideo)
|
|
|
|
|
# combine audio file and new video file
|
|
|
|
|
os.system("ffmpeg -y -i " + "noAudio_"+targetVideo + " -i " + tempAudioFileName + " -c copy " + targetVideo)
|
|
|
|
|
# remove audio-less video
|
|
|
|
|
os.remove("noAudio_"+targetVideo)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
# remove temp directory
|
|
|
|
|
shutil.rmtree("temp")
|
|
|
|
|
|
2020-11-18 10:44:18 +08:00
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
|
torch.set_grad_enabled(False)
|
|
|
|
|
torch.backends.cudnn.enabled = True
|
|
|
|
|
torch.backends.cudnn.benchmark = True
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Interpolation for a pair of images')
|
|
|
|
|
parser.add_argument('--video', dest='video', required=True)
|
|
|
|
|
parser.add_argument('--montage', dest='montage', action='store_true', help='montage origin video')
|
|
|
|
|
parser.add_argument('--skip', dest='skip', action='store_true', help='whether to remove static frames before processing')
|
|
|
|
|
parser.add_argument('--fps', dest='fps', type=int, default=None)
|
2020-11-20 19:08:47 +08:00
|
|
|
parser.add_argument('--png', dest='png', action='store_true', help='whether to vid_out png format vid_outs')
|
|
|
|
|
parser.add_argument('--ext', dest='ext', type=str, default='mp4', help='vid_out video extension')
|
2020-11-19 11:07:37 +08:00
|
|
|
parser.add_argument('--exp', dest='exp', type=int, default=1)
|
2020-11-18 10:44:18 +08:00
|
|
|
args = parser.parse_args()
|
2020-11-19 11:07:37 +08:00
|
|
|
assert (args.exp == 1 or args.exp == 2)
|
|
|
|
|
args.exp = 2 ** args.exp
|
2020-11-18 10:44:18 +08:00
|
|
|
|
|
|
|
|
from model.RIFE import Model
|
|
|
|
|
model = Model()
|
2020-12-02 18:17:06 +08:00
|
|
|
model.load_model('./train_log', -1)
|
2020-11-18 10:44:18 +08:00
|
|
|
model.eval()
|
|
|
|
|
model.device()
|
|
|
|
|
|
|
|
|
|
videoCapture = cv2.VideoCapture(args.video)
|
2020-11-23 12:15:29 +08:00
|
|
|
fps = videoCapture.get(cv2.CAP_PROP_FPS)
|
2020-11-22 14:41:07 +08:00
|
|
|
tot_frame = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
|
2020-11-23 11:15:03 +08:00
|
|
|
videoCapture.release()
|
2020-11-18 10:44:18 +08:00
|
|
|
if args.fps is None:
|
2020-11-25 02:10:31 -04:00
|
|
|
fpsNotAssigned = True
|
2020-11-19 11:07:37 +08:00
|
|
|
args.fps = fps * args.exp
|
2020-11-25 02:10:31 -04:00
|
|
|
else:
|
|
|
|
|
fpsNotAssigned = False
|
2020-11-22 14:41:07 +08:00
|
|
|
videogen = skvideo.io.vreader(args.video)
|
|
|
|
|
lastframe = next(videogen)
|
|
|
|
|
h, w, _ = lastframe.shape
|
2020-11-18 10:44:18 +08:00
|
|
|
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
|
|
|
|
|
if args.png:
|
2020-11-20 19:08:47 +08:00
|
|
|
if not os.path.exists('vid_out'):
|
|
|
|
|
os.mkdir('vid_out')
|
2020-11-18 10:44:18 +08:00
|
|
|
else:
|
|
|
|
|
video_path_wo_ext, ext = os.path.splitext(args.video)
|
2020-11-20 19:08:47 +08:00
|
|
|
vid_out = cv2.VideoWriter('{}_{}X_{}fps.{}'.format(video_path_wo_ext, args.exp, int(np.round(args.fps)), args.ext), fourcc, args.fps, (w, h))
|
2020-11-18 10:44:18 +08:00
|
|
|
|
2020-11-23 11:41:08 +08:00
|
|
|
def clear_buffer(user_args, buffer):
|
2020-11-22 19:39:03 +08:00
|
|
|
cnt = 0
|
2020-11-22 10:32:13 +08:00
|
|
|
while True:
|
2020-11-23 11:54:25 +08:00
|
|
|
item = buffer.get()
|
2020-11-23 11:49:08 +08:00
|
|
|
if item is None:
|
|
|
|
|
break
|
2020-11-20 19:08:47 +08:00
|
|
|
if user_args.png:
|
2020-11-22 14:41:07 +08:00
|
|
|
cv2.imwrite('vid_out/{:0>7d}.png'.format(cnt), item[:, :, ::-1])
|
2020-11-20 19:45:08 +08:00
|
|
|
cnt += 1
|
2020-11-20 19:08:47 +08:00
|
|
|
else:
|
2020-11-22 14:41:07 +08:00
|
|
|
vid_out.write(item[:, :, ::-1])
|
2020-11-20 19:08:47 +08:00
|
|
|
|
2020-11-18 10:44:18 +08:00
|
|
|
if args.montage:
|
|
|
|
|
left = w // 4
|
|
|
|
|
w = w // 2
|
|
|
|
|
ph = ((h - 1) // 32 + 1) * 32
|
|
|
|
|
pw = ((w - 1) // 32 + 1) * 32
|
|
|
|
|
padding = (0, pw - w, 0, ph - h)
|
|
|
|
|
print('{}.{}, {} frames in total, {}FPS to {}FPS'.format(video_path_wo_ext, args.ext, tot_frame, fps, args.fps))
|
|
|
|
|
pbar = tqdm(total=tot_frame)
|
|
|
|
|
skip_frame = 1
|
|
|
|
|
if args.montage:
|
2020-11-22 14:44:27 +08:00
|
|
|
lastframe = lastframe[:, left: left + w]
|
2020-11-22 10:32:13 +08:00
|
|
|
buffer = Queue()
|
2020-11-23 11:41:08 +08:00
|
|
|
_thread.start_new_thread(clear_buffer, (args, buffer))
|
2020-11-22 14:41:07 +08:00
|
|
|
for frame in videogen:
|
|
|
|
|
if args.montage:
|
|
|
|
|
frame = frame[:, left: left + w]
|
|
|
|
|
I0 = torch.from_numpy(np.transpose(lastframe, (2,0,1))).to(device, non_blocking=True).unsqueeze(0).float() / 255.
|
|
|
|
|
I1 = torch.from_numpy(np.transpose(frame, (2,0,1))).to(device, non_blocking=True).unsqueeze(0).float() / 255.
|
|
|
|
|
I0 = F.pad(I0, padding)
|
|
|
|
|
I1 = F.pad(I1, padding)
|
|
|
|
|
p = (F.interpolate(I0, (16, 16), mode='bilinear', align_corners=False)
|
|
|
|
|
- F.interpolate(I1, (16, 16), mode='bilinear', align_corners=False)).abs().mean()
|
|
|
|
|
if p < 5e-3 and args.skip:
|
|
|
|
|
if skip_frame % 100 == 0:
|
|
|
|
|
print("Warning: Your video has {} static frames, skipping them may change the duration of the generated video.".format(skip_frame))
|
|
|
|
|
skip_frame += 1
|
2020-11-18 10:44:18 +08:00
|
|
|
pbar.update(1)
|
2020-11-22 14:41:07 +08:00
|
|
|
continue
|
|
|
|
|
if p > 0.2:
|
|
|
|
|
mid1 = lastframe
|
|
|
|
|
mid0 = lastframe
|
|
|
|
|
mid2 = frame
|
|
|
|
|
else:
|
|
|
|
|
mid1 = model.inference(I0, I1)
|
|
|
|
|
if args.exp == 4:
|
|
|
|
|
mid = model.inference(torch.cat((I0, mid1), 0), torch.cat((mid1, I1), 0))
|
|
|
|
|
mid1 = (((mid1[0] * 255.).byte().cpu().detach().numpy().transpose(1, 2, 0)))
|
|
|
|
|
if args.exp == 4:
|
|
|
|
|
mid0 = (((mid[0] * 255.).byte().cpu().detach().numpy().transpose(1, 2, 0)))
|
|
|
|
|
mid2 = (((mid[1] * 255.).byte().cpu().detach().numpy().transpose(1, 2, 0)))
|
|
|
|
|
if args.montage:
|
|
|
|
|
buffer.put(np.concatenate((lastframe, lastframe), 1))
|
|
|
|
|
if args.exp == 4:
|
|
|
|
|
buffer.put(np.concatenate((lastframe, mid0[:h, :w]), 1))
|
|
|
|
|
buffer.put(np.concatenate((lastframe, mid1[:h, :w]), 1))
|
|
|
|
|
if args.exp == 4:
|
|
|
|
|
buffer.put(np.concatenate((lastframe, mid2[:h, :w]), 1))
|
|
|
|
|
else:
|
|
|
|
|
buffer.put(lastframe)
|
|
|
|
|
if args.exp == 4:
|
|
|
|
|
buffer.put(mid0[:h, :w])
|
|
|
|
|
buffer.put(mid1[:h, :w])
|
|
|
|
|
if args.exp == 4:
|
|
|
|
|
buffer.put(mid2[:h, :w])
|
|
|
|
|
pbar.update(1)
|
|
|
|
|
lastframe = frame
|
2020-11-18 10:44:18 +08:00
|
|
|
if args.montage:
|
2020-11-22 10:32:13 +08:00
|
|
|
buffer.put(np.concatenate((lastframe, lastframe), 1))
|
2020-11-18 10:44:18 +08:00
|
|
|
else:
|
2020-11-22 10:32:13 +08:00
|
|
|
buffer.put(lastframe)
|
2020-12-02 17:51:42 +08:00
|
|
|
|
2020-11-23 11:54:25 +08:00
|
|
|
import time
|
|
|
|
|
while(not buffer.empty()):
|
|
|
|
|
time.sleep(0.1)
|
2020-11-18 10:44:18 +08:00
|
|
|
pbar.close()
|
2020-11-20 21:07:26 +08:00
|
|
|
if not vid_out is None:
|
|
|
|
|
vid_out.release()
|
2020-11-25 02:10:31 -04:00
|
|
|
|
|
|
|
|
# move audio to new video file if appropriate
|
|
|
|
|
if args.png == False and fpsNotAssigned == True:
|
|
|
|
|
outputVideoFileName = '{}_{}X_{}fps.{}'.format(video_path_wo_ext, args.exp, int(np.round(args.fps)), args.ext)
|
2020-12-02 17:51:42 +08:00
|
|
|
transferAudio(video_path_wo_ext + "." + args.ext, outputVideoFileName)
|