mrfakename
commited on
Commit
•
708a0d1
1
Parent(s):
2b3d1e4
Upload 3 files
Browse files- app.py +117 -0
- no_vid.mp4 +0 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sldl.video import VideoSR
|
2 |
+
from sldl.image import ImageSR
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import tempfile
|
6 |
+
import shutil
|
7 |
+
import torch
|
8 |
+
import ffmpeg
|
9 |
+
import time
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
cc = 2
|
13 |
+
if torch.backends.mps.is_available():
|
14 |
+
device = 'mps'
|
15 |
+
cc = 5
|
16 |
+
elif torch.cuda.is_available():
|
17 |
+
device = 'cuda'
|
18 |
+
cc = 10
|
19 |
+
else:
|
20 |
+
device = 'auto'
|
21 |
+
|
22 |
+
vbsrgan = VideoSR('BSRGAN').to(device)
|
23 |
+
vresrgan = VideoSR('RealESRGAN').to(device)
|
24 |
+
ibsrgan = ImageSR('BSRGAN').to(device)
|
25 |
+
iresrgan = ImageSR('RealESRGAN').to(device)
|
26 |
+
|
27 |
+
def upscale_video(input_video, output_video, progress, mname):
|
28 |
+
modelname = mname.lower()
|
29 |
+
model = vbsrgan
|
30 |
+
if modelname == 'bsrgan (default)':
|
31 |
+
# do nothing
|
32 |
+
pass
|
33 |
+
elif modelname == 'real esrgan':
|
34 |
+
model = vresrgan
|
35 |
+
model(input_video, output_video, progress.tqdm)
|
36 |
+
|
37 |
+
def upscale_image(input_image, output_image, mname):
|
38 |
+
modelname = mname.lower()
|
39 |
+
model = ibsrgan
|
40 |
+
if modelname == 'bsrgan (default)':
|
41 |
+
# do nothing
|
42 |
+
pass
|
43 |
+
elif modelname == 'real esrgan':
|
44 |
+
model = iresrgan
|
45 |
+
shutil.copy(input_image, output_image)
|
46 |
+
model(output_image)
|
47 |
+
|
48 |
+
# Gradio interface
|
49 |
+
def video_upscaling_interface(input_text, model_name, progress=gr.Progress()):
|
50 |
+
if input_text:
|
51 |
+
temp_dir = tempfile.mkdtemp()
|
52 |
+
input_video_path = f"{temp_dir}/input_video"
|
53 |
+
output_video_path = f"{temp_dir}/output_video.mp4"
|
54 |
+
ffmpeg.input(input_text).output(input_video_path + '.mp4').run()
|
55 |
+
|
56 |
+
# Upscale the video
|
57 |
+
upscale_video(input_video_path + '.mp4', output_video_path, progress, model_name)
|
58 |
+
|
59 |
+
return [output_video_path, output_video_path]
|
60 |
+
else:
|
61 |
+
return ["no_vid.mp4", "no_vid.mp4"]
|
62 |
+
|
63 |
+
|
64 |
+
def image_upscaling_interface(input_text, model_name):
|
65 |
+
if input_text:
|
66 |
+
temp_dir = tempfile.mkdtemp()
|
67 |
+
input_image_path = f"{temp_dir}/input_image.jpg"
|
68 |
+
output_image_path = f"{temp_dir}/output_image.jpg"
|
69 |
+
input_text.save(input_image_path)
|
70 |
+
upscale_image(input_image_path, output_image_path, model_name)
|
71 |
+
return [output_image_path, output_image_path]
|
72 |
+
else:
|
73 |
+
return ["no_image.jpg", "no_image.jpg"]
|
74 |
+
|
75 |
+
|
76 |
+
css = "footer {display: none !important;} .gradio-container {min-height: 0px !important;}"
|
77 |
+
|
78 |
+
|
79 |
+
with gr.Blocks(css=css) as demo:
|
80 |
+
gr.Markdown('''
|
81 |
+
# Upscale
|
82 |
+
## A CVSYS Project
|
83 |
+
|
84 |
+
Please note that after you upload an image, it may take several minutes before the progress bar appears. This is because we first convert your video to ensure the correct format.
|
85 |
+
''')
|
86 |
+
# with gr.Tab("Image"):
|
87 |
+
# with gr.Row():
|
88 |
+
# with gr.Column():
|
89 |
+
# iinp = gr.Image(label="Upload Image", interactive=True, type="pil")
|
90 |
+
# imod = gr.Dropdown(
|
91 |
+
# ["BSRGAN (Default)", "Real ESRGAN"],
|
92 |
+
# value="BSRGAN (Default)",
|
93 |
+
# interactive=True,
|
94 |
+
# label="Model"
|
95 |
+
# )
|
96 |
+
# with gr.Column():
|
97 |
+
# iout = gr.Image(label="View Image", interactive=False, type="filepath")
|
98 |
+
# ifile = gr.File(label="Download Image", interactive=False)
|
99 |
+
# ibtn = gr.Button(value="Upscale Image")
|
100 |
+
with gr.Tab("Video"):
|
101 |
+
with gr.Row():
|
102 |
+
with gr.Column():
|
103 |
+
vinp = gr.Video(label="Upload Video", interactive=True)
|
104 |
+
vmod = gr.Dropdown(
|
105 |
+
["BSRGAN (Default)", "Real ESRGAN"],
|
106 |
+
value="BSRGAN (Default)",
|
107 |
+
interactive=True,
|
108 |
+
label="Model"
|
109 |
+
)
|
110 |
+
with gr.Column():
|
111 |
+
vout = gr.Video(label="Watch Video", interactive=False)
|
112 |
+
vfile = gr.File(label="Download Video", interactive=False)
|
113 |
+
vbtn = gr.Button(value="Upscale Video")
|
114 |
+
ibtn.click(image_upscaling_interface, [iinp, imod], outputs=[iout, ifile])
|
115 |
+
vbtn.click(video_upscaling_interface, [vinp, vmod], outputs=[vout, vfile])
|
116 |
+
demo.queue(concurrency_count=cc)
|
117 |
+
demo.launch()
|
no_vid.mp4
ADDED
Binary file (5.33 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
git+https://github.com/fakerybakery/sldl-gr
|
3 |
+
ffmpeg-python
|
4 |
+
pillow
|