mirror of
https://github.com/n00mkrad/flowframes.git
synced 2025-12-24 04:09:29 +01:00
Cleanup in inference script, other minor changes
This commit is contained in:
@@ -228,8 +228,11 @@ namespace Flowframes
|
||||
{
|
||||
UpdateOutputFPS();
|
||||
int guiInterpFactor = interpFactorCombox.GetInt();
|
||||
if (!Program.busy && guiInterpFactor > 2 && !GetAi().supportsAnyExp && Config.GetInt("autoEncMode") > 0)
|
||||
Logger.Log($"Warning: {GetAi().aiName.Replace("_", "-")} doesn't natively support 4x/8x and will run multiple times for {guiInterpFactor}x. Auto-Encode will only work on the last run.");
|
||||
if (!initialized)
|
||||
return;
|
||||
string aiName = GetAi().aiName.Replace("_", "-");
|
||||
if (!Program.busy && guiInterpFactor > 2 && !GetAi().supportsAnyExp && Config.GetInt("autoEncMode") > 0 && !Logger.GetLastLine().Contains(aiName))
|
||||
Logger.Log($"Warning: {aiName} doesn't natively support 4x/8x and will run multiple times for {guiInterpFactor}x. Auto-Encode will only work on the last run.");
|
||||
}
|
||||
|
||||
public void SetWorking(bool state, bool allowCancel = true)
|
||||
|
||||
@@ -95,6 +95,8 @@ namespace Flowframes
|
||||
public static string GetLastLine ()
|
||||
{
|
||||
string[] lines = textbox.Text.SplitIntoLines();
|
||||
if (lines.Length < 1)
|
||||
return "";
|
||||
return lines.Last();
|
||||
}
|
||||
|
||||
|
||||
@@ -169,7 +169,7 @@ namespace Flowframes
|
||||
string rifeDir = Path.Combine(Paths.GetPkgPath(), Path.GetFileNameWithoutExtension(Packages.rifeCuda.fileName));
|
||||
string script = "inference_video.py";
|
||||
string uhdStr = InterpolateUtils.UseUHD() ? "--UHD" : "";
|
||||
string args = $" --img {framesPath.Wrap()} --exp {(int)Math.Log(interpFactor, 2)} {uhdStr} --imgformat {InterpolateUtils.GetOutExt()} --output {Paths.interpDir}";
|
||||
string args = $" --input {framesPath.Wrap()} --exp {(int)Math.Log(interpFactor, 2)} {uhdStr} --imgformat {InterpolateUtils.GetOutExt()} --output {Paths.interpDir}";
|
||||
|
||||
if (!File.Exists(Path.Combine(rifeDir, script)))
|
||||
{
|
||||
|
||||
@@ -4,13 +4,11 @@ import cv2
|
||||
import torch
|
||||
import argparse
|
||||
import numpy as np
|
||||
#from tqdm import tqdm
|
||||
from torch.nn import functional as F
|
||||
import warnings
|
||||
import _thread
|
||||
import skvideo.io
|
||||
from queue import Queue, Empty
|
||||
#import moviepy.editor
|
||||
import shutil
|
||||
warnings.filterwarnings("ignore")
|
||||
|
||||
@@ -22,8 +20,8 @@ print("Added {0} to temporary PATH".format(dname))
|
||||
sys.path.append(dname)
|
||||
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
torch.set_grad_enabled(False)
|
||||
if torch.cuda.is_available():
|
||||
torch.set_grad_enabled(False)
|
||||
torch.backends.cudnn.enabled = True
|
||||
torch.backends.cudnn.benchmark = True
|
||||
else:
|
||||
@@ -37,21 +35,13 @@ except:
|
||||
print("Failed to get hardware info!")
|
||||
|
||||
parser = argparse.ArgumentParser(description='Interpolation for a pair of images')
|
||||
parser.add_argument('--video', dest='video', type=str, default=None)
|
||||
parser.add_argument('--img', dest='img', type=str, default=None)
|
||||
parser.add_argument('--input', dest='input', type=str, default=None)
|
||||
parser.add_argument('--output', required=False, default='frames-interpolated')
|
||||
parser.add_argument('--imgformat', default="png")
|
||||
parser.add_argument('--montage', default=False, dest='montage', action='store_true', help='montage origin video')
|
||||
parser.add_argument('--UHD', dest='UHD', action='store_true', help='support 4k video')
|
||||
parser.add_argument('--skip', dest='skip', default=False, action='store_true', help='whether to remove static frames before processing')
|
||||
parser.add_argument('--fps', dest='fps', type=int, default=None)
|
||||
parser.add_argument('--png', dest='png', default=True, 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')
|
||||
parser.add_argument('--exp', dest='exp', type=int, default=1)
|
||||
args = parser.parse_args()
|
||||
assert (not args.video is None or not args.img is None)
|
||||
if not args.img is None:
|
||||
args.png = True
|
||||
assert (not args.input is None)
|
||||
|
||||
from model.RIFE_HD import Model
|
||||
model = Model()
|
||||
@@ -59,42 +49,23 @@ model.load_model(os.path.join(dname, "models"), -1)
|
||||
model.eval()
|
||||
model.device()
|
||||
|
||||
path = args.img
|
||||
path = args.input
|
||||
name = os.path.basename(path)
|
||||
interp_output_path = (args.output).join(path.rsplit(name, 1))
|
||||
print("\ninterp_output_path: " + interp_output_path)
|
||||
|
||||
if not args.video is None:
|
||||
videoCapture = cv2.VideoCapture(args.video)
|
||||
fps = videoCapture.get(cv2.CAP_PROP_FPS)
|
||||
tot_frame = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
|
||||
videoCapture.release()
|
||||
if args.fps is None:
|
||||
fpsNotAssigned = True
|
||||
args.fps = fps * (2 ** args.exp)
|
||||
else:
|
||||
fpsNotAssigned = False
|
||||
videogen = skvideo.io.vreader(args.video)
|
||||
lastframe = next(videogen)
|
||||
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
|
||||
video_path_wo_ext, ext = os.path.splitext(args.video)
|
||||
print('{} frames in total'.format(tot_frame))
|
||||
else:
|
||||
videogen = []
|
||||
for f in os.listdir(args.img):
|
||||
if 'png' in f:
|
||||
videogen.append(f)
|
||||
tot_frame = len(videogen)
|
||||
videogen.sort(key= lambda x:int(x[:-4]))
|
||||
lastframe = cv2.imread(os.path.join(args.img, videogen[0]))[:, :, ::-1].copy()
|
||||
videogen = videogen[1:]
|
||||
videogen = []
|
||||
for f in os.listdir(args.input):
|
||||
if 'png' in f:
|
||||
videogen.append(f)
|
||||
tot_frame = len(videogen)
|
||||
videogen.sort(key= lambda x:int(x[:-4]))
|
||||
lastframe = cv2.imread(os.path.join(args.input, videogen[0]))[:, :, ::-1].copy()
|
||||
videogen = videogen[1:]
|
||||
h, w, _ = lastframe.shape
|
||||
vid_out = None
|
||||
if args.png:
|
||||
if not os.path.exists(interp_output_path):
|
||||
os.mkdir(interp_output_path)
|
||||
else:
|
||||
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))
|
||||
if not os.path.exists(interp_output_path):
|
||||
os.mkdir(interp_output_path)
|
||||
|
||||
def clear_write_buffer(user_args, write_buffer):
|
||||
cnt = 1
|
||||
@@ -102,20 +73,14 @@ def clear_write_buffer(user_args, write_buffer):
|
||||
item = write_buffer.get()
|
||||
if item is None:
|
||||
break
|
||||
if user_args.png:
|
||||
print('=> {:0>8d}.{}'.format(cnt, args.imgformat))
|
||||
cv2.imwrite('{}/{:0>8d}.{}'.format(interp_output_path, cnt, args.imgformat), item[:, :, ::-1])
|
||||
#cv2.imwrite('vid_out/{:0>7d}.png'.format(cnt), item[:, :, ::-1])
|
||||
cnt += 1
|
||||
else:
|
||||
vid_out.write(item[:, :, ::-1])
|
||||
print('=> {:0>8d}.{}'.format(cnt, args.imgformat))
|
||||
cv2.imwrite('{}/{:0>8d}.{}'.format(interp_output_path, cnt, args.imgformat), item[:, :, ::-1])
|
||||
cnt += 1
|
||||
|
||||
def build_read_buffer(user_args, read_buffer, videogen):
|
||||
for frame in videogen:
|
||||
if not user_args.img is None:
|
||||
frame = cv2.imread(os.path.join(user_args.img, frame))[:, :, ::-1].copy()
|
||||
if user_args.montage:
|
||||
frame = frame[:, left: left + w]
|
||||
if not user_args.input is None:
|
||||
frame = cv2.imread(os.path.join(user_args.input, frame))[:, :, ::-1].copy()
|
||||
read_buffer.put(frame)
|
||||
read_buffer.put(None)
|
||||
|
||||
@@ -128,9 +93,6 @@ def make_inference(I0, I1, exp):
|
||||
second_half = make_inference(middle, I1, exp=exp - 1)
|
||||
return [*first_half, middle, *second_half]
|
||||
|
||||
if args.montage:
|
||||
left = w // 4
|
||||
w = w // 2
|
||||
if args.UHD:
|
||||
print("UHD mode enabled.")
|
||||
ph = ((h - 1) // 64 + 1) * 64
|
||||
@@ -139,10 +101,7 @@ else:
|
||||
ph = ((h - 1) // 32 + 1) * 32
|
||||
pw = ((w - 1) // 32 + 1) * 32
|
||||
padding = (0, pw - w, 0, ph - h)
|
||||
#pbar = tqdm(total=tot_frame)
|
||||
skip_frame = 1
|
||||
if args.montage:
|
||||
lastframe = lastframe[:, left: left + w]
|
||||
|
||||
write_buffer = Queue(maxsize=200)
|
||||
read_buffer = Queue(maxsize=200)
|
||||
@@ -159,44 +118,20 @@ while True:
|
||||
I0 = I1
|
||||
I1 = torch.from_numpy(np.transpose(frame, (2,0,1))).to(device, non_blocking=True).unsqueeze(0).float() / 255.
|
||||
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
|
||||
# #pbar.update(1)
|
||||
# continue
|
||||
#if p > 0.2:
|
||||
# mid1 = lastframe
|
||||
# mid0 = lastframe
|
||||
# mid2 = lastframe
|
||||
#else:
|
||||
|
||||
output = make_inference(I0, I1, args.exp)
|
||||
if args.montage:
|
||||
write_buffer.put(np.concatenate((lastframe, lastframe), 1))
|
||||
for mid in output:
|
||||
mid = (((mid[0] * 255.).byte().cpu().numpy().transpose(1, 2, 0)))
|
||||
write_buffer.put(np.concatenate((lastframe, mid[:h, :w]), 1))
|
||||
else:
|
||||
write_buffer.put(lastframe)
|
||||
for mid in output:
|
||||
mid = (((mid[0] * 255.).byte().cpu().numpy().transpose(1, 2, 0)))
|
||||
write_buffer.put(mid[:h, :w])
|
||||
#pbar.update(1)
|
||||
lastframe = frame
|
||||
if args.montage:
|
||||
write_buffer.put(np.concatenate((lastframe, lastframe), 1))
|
||||
else:
|
||||
write_buffer.put(lastframe)
|
||||
for mid in output:
|
||||
mid = (((mid[0] * 255.).byte().cpu().numpy().transpose(1, 2, 0)))
|
||||
write_buffer.put(mid[:h, :w])
|
||||
|
||||
lastframe = frame
|
||||
write_buffer.put(lastframe)
|
||||
import time
|
||||
while(not write_buffer.empty()):
|
||||
time.sleep(0.1)
|
||||
#pbar.close()
|
||||
if not vid_out is None:
|
||||
vid_out.release()
|
||||
|
||||
# move audio to new video file if appropriate
|
||||
if args.png == False and fpsNotAssigned == True and not args.skip:
|
||||
outputVideoFileName = '{}_{}X_{}fps.{}'.format(video_path_wo_ext, args.exp, int(np.round(args.fps)), args.ext)
|
||||
transferAudio(video_path_wo_ext + "." + args.ext, outputVideoFileName)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user