b2bomber commited on
Commit
f114dc6
Β·
verified Β·
1 Parent(s): 90e138f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -4,6 +4,7 @@ import numpy as np
4
  import os
5
  import tempfile
6
  import subprocess
 
7
  from basicsr.archs.rrdbnet_arch import RRDBNet
8
  from realesrgan import RealESRGANer
9
  from realesrgan.archs.srvgg_arch import SRVGGNetCompact
@@ -14,7 +15,7 @@ from basicsr.utils.download_util import load_file_from_url
14
  # We create a dictionary to cache models so they are only loaded once.
15
  model_cache = {}
16
 
17
- def get_upsampler(model_name='realesr-general-x4v3', denoise_strength=1):
18
  """Loads and returns the specified RealESRGAN model."""
19
  if model_name in model_cache:
20
  return model_cache[model_name]
@@ -32,14 +33,14 @@ def get_upsampler(model_name='realesr-general-x4v3', denoise_strength=1):
32
 
33
  upsampler = RealESRGANer(
34
  scale=netscale, model_path=model_path, model=model,
35
- tile=64, tile_pad=10, pre_pad=10, half=False, gpu_id=None
36
  )
37
  model_cache[model_name] = upsampler
38
  return upsampler
39
 
40
  def get_face_enhancer(upsampler, outscale):
41
  """Loads and returns the GFPGAN face enhancer."""
42
- key = 'face_enhancer'
43
  if key in model_cache:
44
  return model_cache[key]
45
 
@@ -51,23 +52,23 @@ def get_face_enhancer(upsampler, outscale):
51
  return face_enhancer
52
 
53
  # --- Core Video Processing Function ---
54
- def enhance_video(video_path, model_name, denoise_strength, outscale, face_enhance, progress=gr.Progress(track_tqdm=True)):
55
  """Enhances a video frame by frame and provides progress updates."""
56
  if not video_path:
57
  raise gr.Error("Please upload a video to enhance.")
58
 
59
  try:
60
- upsampler = get_upsampler(model_name, denoise_strength)
61
 
62
  face_enhancer = None
63
  if face_enhance:
64
  face_enhancer = get_face_enhancer(upsampler, outscale)
65
 
66
  cap = cv2.VideoCapture(video_path)
67
- fps = cap.get(cv2.CAP_PROP_FPS)
68
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
69
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
70
- total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
71
 
72
  # Prepare output video writer
73
  temp_dir = tempfile.mkdtemp()
@@ -76,7 +77,7 @@ def enhance_video(video_path, model_name, denoise_strength, outscale, face_enhan
76
  writer = cv2.VideoWriter(enhanced_video_path, fourcc, fps, (width * outscale, height * outscale))
77
 
78
  # Process each frame
79
- for i in progress.tqdm(range(total_frames), desc="Enhancing Frames..."):
80
  ret, frame = cap.read()
81
  if not ret:
82
  break
@@ -93,7 +94,7 @@ def enhance_video(video_path, model_name, denoise_strength, outscale, face_enhan
93
 
94
  # Merge audio back into the enhanced video
95
  final_output_path = os.path.join(temp_dir, "final_output_with_audio.mp4")
96
- audio_merge_cmd = f"ffmpeg -y -i {enhanced_video_path} -i {video_path} -c:v libx264 -crf 23 -preset fast -c:a aac -b:a 128k -map 0:v:0 -map 1:a:0 -shortest {final_output_path}"
97
  subprocess.call(audio_merge_cmd, shell=True, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
98
 
99
  return final_output_path
@@ -106,46 +107,54 @@ def enhance_video(video_path, model_name, denoise_strength, outscale, face_enhan
106
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet"), title="πŸŽ₯ AI Video Enhancer") as demo:
107
  gr.Markdown(
108
  """
 
109
  Improve video quality, upscale resolution, and restore faces with cutting-edge AI.
110
  **Note:** Processing can be slow, especially for longer videos.
111
  """
112
  )
113
 
114
- with gr.Row(variant="panel"):
115
- # --- Input Column ---
116
- with gr.Column(scale=1):
117
- video_input = gr.Video(label="🎬 Upload Your Video")
118
-
119
- # Settings in a clean Accordion
120
- with gr.Accordion("βš™οΈ Enhancement Options", open=True):
 
 
 
121
  model_name = gr.Dropdown(
122
  choices=["realesr-general-x4v3", "RealESRGAN_x4plus_anime_6B"],
123
  value="realesr-general-x4v3",
124
- label="Model Type"
125
  )
126
  outscale = gr.Slider(1, 4, value=2, step=1, label="Upscale Factor")
127
- face_enhance = gr.Checkbox(label="Restore Faces (GFPGAN)")
128
- denoise_strength = gr.Slider(0, 1, value=0.5, step=0.1, label="Denoise Strength (for general model only)")
129
 
130
- enhance_btn = gr.Button("✨ Enhance Video", variant="primary")
131
-
132
- # --- Output Column ---
133
- with gr.Column(scale=1):
134
- video_output = gr.Video(label="🌟 Enhanced Result")
135
- download_file = gr.File(label="⬇️ Download Enhanced Video", visible=False)
 
 
 
 
 
 
136
 
137
  # --- Event Logic ---
138
- def on_submit(video, model, denoise, scale, face):
139
  # When the button is clicked, start the enhancement and return the path to the output video.
140
  # Also, make the download button visible.
141
- output_path = enhance_video(video, model, denoise, scale, face)
142
  return output_path, gr.update(value=output_path, visible=True)
143
 
144
  enhance_btn.click(
145
  fn=on_submit,
146
- inputs=[video_input, model_name, denoise_strength, outscale, face_enhance],
147
  outputs=[video_output, download_file]
148
  )
149
 
150
  if __name__ == '__main__':
151
- demo.launch()
 
4
  import os
5
  import tempfile
6
  import subprocess
7
+ import traceback
8
  from basicsr.archs.rrdbnet_arch import RRDBNet
9
  from realesrgan import RealESRGANer
10
  from realesrgan.archs.srvgg_arch import SRVGGNetCompact
 
15
  # We create a dictionary to cache models so they are only loaded once.
16
  model_cache = {}
17
 
18
+ def get_upsampler(model_name='realesr-general-x4v3'):
19
  """Loads and returns the specified RealESRGAN model."""
20
  if model_name in model_cache:
21
  return model_cache[model_name]
 
33
 
34
  upsampler = RealESRGANer(
35
  scale=netscale, model_path=model_path, model=model,
36
+ tile=64, tile_pad=10, pre_pad=10, half=True, gpu_id=None # Use half precision for speed
37
  )
38
  model_cache[model_name] = upsampler
39
  return upsampler
40
 
41
  def get_face_enhancer(upsampler, outscale):
42
  """Loads and returns the GFPGAN face enhancer."""
43
+ key = f'face_enhancer_{outscale}'
44
  if key in model_cache:
45
  return model_cache[key]
46
 
 
52
  return face_enhancer
53
 
54
  # --- Core Video Processing Function ---
55
+ def enhance_video(video_path, model_name, outscale, face_enhance, progress=gr.Progress(track_tqdm=True)):
56
  """Enhances a video frame by frame and provides progress updates."""
57
  if not video_path:
58
  raise gr.Error("Please upload a video to enhance.")
59
 
60
  try:
61
+ upsampler = get_upsampler(model_name)
62
 
63
  face_enhancer = None
64
  if face_enhance:
65
  face_enhancer = get_face_enhancer(upsampler, outscale)
66
 
67
  cap = cv2.VideoCapture(video_path)
68
+ fps = cap.get(cv.CAP_PROP_FPS)
69
+ width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
70
+ height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
71
+ total_frames = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
72
 
73
  # Prepare output video writer
74
  temp_dir = tempfile.mkdtemp()
 
77
  writer = cv2.VideoWriter(enhanced_video_path, fourcc, fps, (width * outscale, height * outscale))
78
 
79
  # Process each frame
80
+ for _ in progress.tqdm(range(total_frames), desc="Enhancing Frames..."):
81
  ret, frame = cap.read()
82
  if not ret:
83
  break
 
94
 
95
  # Merge audio back into the enhanced video
96
  final_output_path = os.path.join(temp_dir, "final_output_with_audio.mp4")
97
+ audio_merge_cmd = f'ffmpeg -y -i "{enhanced_video_path}" -i "{video_path}" -c:v libx264 -crf 23 -preset fast -c:a aac -b:a 128k -map 0:v:0 -map 1:a:0 -shortest "{final_output_path}"'
98
  subprocess.call(audio_merge_cmd, shell=True, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
99
 
100
  return final_output_path
 
107
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet"), title="πŸŽ₯ AI Video Enhancer") as demo:
108
  gr.Markdown(
109
  """
110
+ # πŸŽ₯ AI Video Enhancer & Upscaler
111
  Improve video quality, upscale resolution, and restore faces with cutting-edge AI.
112
  **Note:** Processing can be slow, especially for longer videos.
113
  """
114
  )
115
 
116
+ # Top row for video previews
117
+ with gr.Row():
118
+ video_input = gr.Video(label="🎬 Original Video")
119
+ video_output = gr.Video(label="🌟 Enhanced Result")
120
+
121
+ # Panel for all settings and actions
122
+ with gr.Box():
123
+ with gr.Row():
124
+ # Left side for main settings
125
+ with gr.Column(scale=3):
126
  model_name = gr.Dropdown(
127
  choices=["realesr-general-x4v3", "RealESRGAN_x4plus_anime_6B"],
128
  value="realesr-general-x4v3",
129
+ label="Model Type (General or Anime)"
130
  )
131
  outscale = gr.Slider(1, 4, value=2, step=1, label="Upscale Factor")
 
 
132
 
133
+ # Right side for the most important actions
134
+ with gr.Column(scale=1, min_width=200):
135
+ face_enhance = gr.Checkbox(label="✨ Restore Faces (GFPGAN)", value=False)
136
+ enhance_btn = gr.Button("πŸš€ Enhance Video", variant="primary")
137
+
138
+ # Examples and Download components
139
+ gr.Examples(
140
+ examples=["sample_video.mp4"], # Add path to your example video
141
+ inputs=[video_input],
142
+ label="Click an example to start"
143
+ )
144
+ download_file = gr.File(label="⬇️ Download Enhanced Video", visible=False)
145
 
146
  # --- Event Logic ---
147
+ def on_submit(video, model, scale, face):
148
  # When the button is clicked, start the enhancement and return the path to the output video.
149
  # Also, make the download button visible.
150
+ output_path = enhance_video(video, model, scale, face)
151
  return output_path, gr.update(value=output_path, visible=True)
152
 
153
  enhance_btn.click(
154
  fn=on_submit,
155
+ inputs=[video_input, model_name, outscale, face_enhance],
156
  outputs=[video_output, download_file]
157
  )
158
 
159
  if __name__ == '__main__':
160
+ demo.launch()