Fabrice-TIERCELIN commited on
Commit
dcbe3fa
·
verified ·
1 Parent(s): b7c7d92

mcp_server=True

Browse files
Files changed (1) hide show
  1. app.py +90 -5
app.py CHANGED
@@ -1,6 +1,91 @@
1
  import gradio as gr
2
 
3
- from infer import infer_image, infer_video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  input_image = gr.Image(type='pil', label='Input Image')
6
  input_model_image = gr.Radio([('x2', 2), ('x4', 4), ('x8', 8)], type="value", value=4, label="Model Upscale/Enhance Type")
@@ -11,20 +96,20 @@ tab_img = gr.Interface(
11
  fn=infer_image,
12
  inputs=[input_image, input_model_image],
13
  outputs=output_image,
14
- title="Real-ESRGAN Pytorch",
15
  description="Gradio UI for Real-ESRGAN Pytorch version. To use it, simply upload your image and choose the model. Read more at the links below. Please click submit only once <br>Credits: [Nick088](https://linktr.ee/Nick088), Xinntao, Tencent, Geeve George, ai-forever, daroche <br><p style='text-align: center'><a href='https://github.com/Nick088/Real-ESRGAN_Pytorch'>Github Repo</a></p>"
16
  )
17
 
18
  input_video = gr.Video(label='Input Video')
19
  input_model_video = gr.Radio([('x2', 2), ('x4', 4), ('x8', 8)], type="value", value=2, label="Model Upscale/Enhance Type")
20
  submit_video_button = gr.Button('Submit')
21
- output_video = gr.Video(label='Output Video')
22
 
23
  tab_vid = gr.Interface(
24
  fn=infer_video,
25
  inputs=[input_video, input_model_video],
26
  outputs=output_video,
27
- title="Real-ESRGAN Pytorch",
28
  description="Gradio UI for Real-ESRGAN Pytorch version. To use it, simply upload your video and choose the model. Read more at the links below. Please click submit only once <br>Credits: [Nick088](https://linktr.ee/Nick088), Xinntao, Tencent, Geeve George, ai-forever, daroche <br><p style='text-align: center'><a href='https://arxiv.org/abs/2107.10833'>Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data</a> | <a href='https://github.com/ai-forever/Real-ESRGAN'>Github Repo</a></p>",
29
  examples=[
30
  [
@@ -38,4 +123,4 @@ tab_vid = gr.Interface(
38
 
39
  demo = gr.TabbedInterface([tab_img, tab_vid], ["Image", "Video"])
40
 
41
- demo.launch(debug=True, show_error=True, share=True)
 
1
  import gradio as gr
2
 
3
+ from PIL import Image
4
+ import cv2 as cv
5
+ import torch
6
+ from RealESRGAN import RealESRGAN
7
+ import tempfile
8
+ import numpy as np
9
+ import tqdm
10
+ import ffmpeg
11
+ import spaces
12
+
13
+
14
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
15
+
16
+ @spaces.GPU(duration=60)
17
+ def infer_image(img: Image.Image, size_modifier: int ) -> Image.Image:
18
+ if img is None:
19
+ raise Exception("Image not uploaded")
20
+
21
+ width, height = img.size
22
+
23
+ if width >= 5000 or height >= 5000:
24
+ raise Exception("The image is too large.")
25
+
26
+ model = RealESRGAN(device, scale=size_modifier)
27
+ model.load_weights(f'weights/RealESRGAN_x{size_modifier}.pth', download=False)
28
+
29
+ result = model.predict(img.convert('RGB'))
30
+ print(f"Image size ({device}): {size_modifier} ... OK")
31
+ return result
32
+
33
+ @spaces.GPU(duration=120)
34
+ def infer_video(video_filepath: str, size_modifier: int) -> str:
35
+ model = RealESRGAN(device, scale=size_modifier)
36
+ model.load_weights(f'weights/RealESRGAN_x{size_modifier}.pth', download=False)
37
+
38
+ cap = cv.VideoCapture(video_filepath)
39
+
40
+ tmpfile = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
41
+ vid_output = tmpfile.name
42
+ tmpfile.close()
43
+
44
+ # Check if the input video has an audio stream
45
+ probe = ffmpeg.probe(video_filepath)
46
+ has_audio = any(stream['codec_type'] == 'audio' for stream in probe['streams'])
47
+
48
+ if has_audio:
49
+ # Extract audio from the input video
50
+ audio_file = video_filepath.replace(".mp4", ".wav")
51
+ ffmpeg.input(video_filepath).output(audio_file, format='wav', ac=1).run(overwrite_output=True)
52
+
53
+ vid_writer = cv.VideoWriter(
54
+ vid_output,
55
+ fourcc=cv.VideoWriter.fourcc(*'mp4v'),
56
+ fps=cap.get(cv.CAP_PROP_FPS),
57
+ frameSize=(int(cap.get(cv.CAP_PROP_FRAME_WIDTH)) * size_modifier, int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) * size_modifier)
58
+ )
59
+
60
+ n_frames = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
61
+
62
+ for _ in tqdm.tqdm(range(n_frames)):
63
+ ret, frame = cap.read()
64
+ if not ret:
65
+ break
66
+
67
+ frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
68
+ frame = Image.fromarray(frame)
69
+
70
+ upscaled_frame = model.predict(frame.convert('RGB'))
71
+
72
+ upscaled_frame = np.array(upscaled_frame)
73
+ upscaled_frame = cv.cvtColor(upscaled_frame, cv.COLOR_RGB2BGR)
74
+
75
+ vid_writer.write(upscaled_frame)
76
+
77
+ vid_writer.release()
78
+
79
+ if has_audio:
80
+ # Re-encode the video with the modified audio
81
+ ffmpeg.input(vid_output).output(video_filepath.replace(".mp4", "_upscaled.mp4"), vcodec='libx264', acodec='aac', audio_bitrate='320k').run(overwrite_output=True)
82
+
83
+ # Replace the original audio with the upscaled audio
84
+ ffmpeg.input(audio_file).output(video_filepath.replace(".mp4", "_upscaled.mp4"), acodec='aac', audio_bitrate='320k').run(overwrite_output=True)
85
+
86
+ print(f"Video file : {video_filepath}")
87
+
88
+ return vid_output.replace(".mp4", "_upscaled.mp4") if has_audio else vid_output
89
 
90
  input_image = gr.Image(type='pil', label='Input Image')
91
  input_model_image = gr.Radio([('x2', 2), ('x4', 4), ('x8', 8)], type="value", value=4, label="Model Upscale/Enhance Type")
 
96
  fn=infer_image,
97
  inputs=[input_image, input_model_image],
98
  outputs=output_image,
99
+ title="Real-ESRGAN",
100
  description="Gradio UI for Real-ESRGAN Pytorch version. To use it, simply upload your image and choose the model. Read more at the links below. Please click submit only once <br>Credits: [Nick088](https://linktr.ee/Nick088), Xinntao, Tencent, Geeve George, ai-forever, daroche <br><p style='text-align: center'><a href='https://github.com/Nick088/Real-ESRGAN_Pytorch'>Github Repo</a></p>"
101
  )
102
 
103
  input_video = gr.Video(label='Input Video')
104
  input_model_video = gr.Radio([('x2', 2), ('x4', 4), ('x8', 8)], type="value", value=2, label="Model Upscale/Enhance Type")
105
  submit_video_button = gr.Button('Submit')
106
+ output_video = gr.Video(label='Output Video', autoplay = True)
107
 
108
  tab_vid = gr.Interface(
109
  fn=infer_video,
110
  inputs=[input_video, input_model_video],
111
  outputs=output_video,
112
+ title="Real-ESRGAN",
113
  description="Gradio UI for Real-ESRGAN Pytorch version. To use it, simply upload your video and choose the model. Read more at the links below. Please click submit only once <br>Credits: [Nick088](https://linktr.ee/Nick088), Xinntao, Tencent, Geeve George, ai-forever, daroche <br><p style='text-align: center'><a href='https://arxiv.org/abs/2107.10833'>Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data</a> | <a href='https://github.com/ai-forever/Real-ESRGAN'>Github Repo</a></p>",
114
  examples=[
115
  [
 
123
 
124
  demo = gr.TabbedInterface([tab_img, tab_vid], ["Image", "Video"])
125
 
126
+ demo.launch(mcp_server=True, debug=True, show_error=True, share=True)