RefVSR / app.py
Testvini's picture
Update app.py
829bbf7 verified
Raw
History Blame Contribute Delete
7.11 kB
import os
import sys
import gradio as gr
from PIL import Image
## environment settup
if not os.path.exists("RefVSR"):
os.system("git clone https://github.com/codeslake/RefVSR.git")
# Mudando para o diretório clonado (seja na primeira vez ou após reinício do container)
os.chdir("RefVSR")
if not os.path.exists("ckpt"):
os.system("./install/install_cudnn113.sh")
os.makedirs("ckpt", exist_ok=True)
os.system("wget https://huggingface.co/spaces/codeslake/RefVSR/resolve/main/SPyNet.pytorch -O ckpt/SPyNet.pytorch")
os.system("wget https://huggingface.co/spaces/codeslake/RefVSR/resolve/main/RefVSR_MFID_8K.pytorch -O ckpt/RefVSR_MFID_8K.pytorch")
os.system("wget https://huggingface.co/spaces/codeslake/RefVSR/resolve/main/RefVSR_small_MFID_8K.pytorch -O ckpt/RefVSR_small_MFID_8K.pytorch")
os.system("wget https://huggingface.co/spaces/codeslake/RefVSR/resolve/main/RefVSR_MFID.pytorch -O ckpt/RefVSR_MFID.pytorch")
os.system("wget https://huggingface.co/spaces/codeslake/RefVSR/resolve/main/RefVSR_small_MFID_8K.pytorch -O ckpt/RefVSR_small_MFID.pytorch")
if os.getcwd() not in sys.path:
sys.path.append(os.getcwd())
## I/O setup (creates folders and places inputs corresponding to the original RefVSR code)
# HD input
HR_LR_path = "test/RealMCVSR/test/HR/UW/0000"
HR_Ref_path = "test/RealMCVSR/test/HR/W/0000"
HR_Ref_path_T = "test/RealMCVSR/test/HR/T/0000"
os.makedirs(HR_LR_path, exist_ok=True)
os.makedirs(HR_Ref_path, exist_ok=True)
os.makedirs(HR_Ref_path_T, exist_ok=True)
if not os.path.exists("HR_LR1.png"):
os.system("wget https://www.dropbox.com/s/x33ka2jlzwsde7r/LR.png -O HR_LR1.png")
os.system("wget https://www.dropbox.com/s/pp903wlz3syf68w/Ref.png -O HR_Ref1.png")
os.system("wget https://www.dropbox.com/s/zl0h83x0le6ejfw/LR.png -O HR_LR2.png")
os.system("wget https://www.dropbox.com/s/9hzupmc3clt0f0e/Ref.png -O HR_Ref2.png")
os.system("wget https://www.dropbox.com/s/2u6lcfdhvcylklg/LR.png -O HR_LR3.png")
os.system("wget https://www.dropbox.com/s/a7bwfy3gl26tvbq/Ref.png -O HR_Ref3.png")
# 4x downsampled input
LR_path = "test/RealMCVSR/test/LRx4/UW/0000"
Ref_path = "test/RealMCVSR/test/LRx4/W/0000"
Ref_path_T = "test/RealMCVSR/test/LRx4/T/0000"
os.makedirs(LR_path, exist_ok=True)
os.makedirs(Ref_path, exist_ok=True)
os.makedirs(Ref_path_T, exist_ok=True)
if not os.path.exists("LR.png"):
os.system("wget https://www.dropbox.com/s/hkvdwm3grshjt0k/LR.png -O LR.png")
os.system("wget https://www.dropbox.com/s/4sv34su3kg1ifkp/Ref.png -O Ref.png")
# output directory
os.makedirs('result', exist_ok=True)
## resize if necessary
def resize(img):
max_side = 480
w = img.size[0]
h = img.size[1]
if max(h, w) > max_side:
scale_ratio = max_side / max(h, w)
wsize = int(w * scale_ratio)
hsize = int(h * scale_ratio)
# Compatibilidade com Pillow 10+
resample_filter = getattr(Image, 'Resampling', Image).LANCZOS
img = img.resize((wsize, hsize), resample_filter)
w = img.size[0]
h = img.size[1]
img = img.crop((0, 0, w - w % 8, h - h % 8))
return img
#################### 8K ##################
## inference
def inference_8K(LR, Ref):
LR = resize(LR)
Ref = resize(Ref)
LR.save(os.path.join(LR_path, '0000.png'))
Ref.save(os.path.join(Ref_path, '0000.png'))
Ref.save(os.path.join(Ref_path_T, '0000.png'))
LR.save(os.path.join(HR_LR_path, '0000.png'))
Ref.save(os.path.join(HR_Ref_path, '0000.png'))
Ref.save(os.path.join(HR_Ref_path_T, '0000.png'))
os.system("python -B run.py \
--mode RefVSR_MFID_8K \
--config config_RefVSR_MFID_8K \
--data RealMCVSR \
--ckpt_abs_name ckpt/RefVSR_MFID_8K.pytorch \
--data_offset ./test \
--output_offset ./result \
--qualitative_only \
--cpu \
--is_gradio")
return "result/0000.png"
title_8K = "RefVSR (8K Model)"
description_8K = "Demo application for Reference-based Video Super-Resolution (RefVSR). Upload a low-resolution frame and a reference frame to 'LR' and 'Ref' input windows, respectively. The demo runs on CPUs and takes about 30s."
article_8K = "<p style='text-align: center'><b>To check the full capability of the module, we recommend to clone Github repository and run RefVSR models on videos using GPUs.</b></p><p style='text-align: center'>This demo runs on CPUs and only supports RefVSR for a single LR and Ref frames due to computational complexity.</p><p style='text-align: center'><a href='https://junyonglee.me/projects/RefVSR' target='_blank'>Project</a> | <a href='https://arxiv.org/abs/2203.14537' target='_blank'>arXiv</a> | <a href='https://github.com/codeslake/RefVSR' target='_blank'>Github</a></p>"
examples_8K = [['HR_LR1.png', 'HR_Ref1.png'], ['HR_LR2.png', 'HR_Ref2.png'], ['HR_LR3.png', 'HR_Ref3.png']]
# Interface 1
demo_8k = gr.Interface(
fn=inference_8K,
inputs=[gr.Image(type="pil", label="LR Input"), gr.Image(type="pil", label="Ref Input")],
outputs=gr.Image(type="filepath", label="Output"),
title=title_8K,
description=description_8K,
article=article_8K,
examples=examples_8K
)
#################### low res ##################
## inference
def inference(LR, Ref):
LR = resize(LR)
Ref = resize(Ref)
LR.save(os.path.join(LR_path, '0000.png'))
Ref.save(os.path.join(Ref_path, '0000.png'))
Ref.save(os.path.join(Ref_path_T, '0000.png'))
LR.save(os.path.join(HR_LR_path, '0000.png'))
Ref.save(os.path.join(HR_Ref_path, '0000.png'))
Ref.save(os.path.join(HR_Ref_path_T, '0000.png'))
os.system("python -B run.py \
--mode RefVSR_MFID \
--config config_RefVSR_MFID \
--data RealMCVSR \
--ckpt_abs_name ckpt/RefVSR_MFID.pytorch \
--data_offset ./test \
--output_offset ./result \
--qualitative_only \
--cpu \
--is_gradio")
return "result/0000.png"
title_low = "Demo for RefVSR (CVPR 2022) - Low Res"
description_low = "The demo applies 4xVSR on a video frame. It runs on CPUs and takes about 150s. It is recommended for the reference frame to have a 2x larger zoom factor than that of the low-resolution frame."
article_low = article_8K
## Resize sample for Low Res
if os.path.exists('LR.png') and os.path.exists('Ref.png'):
resize(Image.open('LR.png')).save('LR.png')
resize(Image.open('Ref.png')).save('Ref.png')
examples_low = [['LR.png','Ref.png']]
# Interface 2
demo_low = gr.Interface(
fn=inference,
inputs=[gr.Image(type="pil", label="LR Input"), gr.Image(type="pil", label="Ref Input")],
outputs=gr.Image(type="filepath", label="Output"),
title=title_low,
description=description_low,
article=article_low,
examples=examples_low
)
## Combina as duas interfaces em abas e inicia
app = gr.TabbedInterface([demo_8k, demo_low], ["8K Model", "Low Res Model"])
app.launch()