Beexly commited on
Commit
049680f
·
verified ·
1 Parent(s): 060cd39

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +25 -4
  2. requirements.txt +1 -0
app.py CHANGED
@@ -191,11 +191,32 @@ def _upscale_duration(image, scale):
191
  def upscale_image(image, scale):
192
  if image is None:
193
  raise gr.Error("Upload an image first.")
194
- from RealESRGAN import RealESRGAN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
- model = RealESRGAN(torch.device("cuda"), scale=int(scale))
197
- model.load_weights(f"weights/RealESRGAN_x{int(scale)}.pth", download=True)
198
- return model.predict(image.convert("RGB"))
 
199
 
200
 
201
  # ---------------------------------------------------------------------------
 
191
  def upscale_image(image, scale):
192
  if image is None:
193
  raise gr.Error("Upload an image first.")
194
+ # The PyPI package `realesrgan` (installed here) is xinntao's original
195
+ # BasicSR-based release: RealESRGANer(scale, model_path, model=<arch>),
196
+ # not the simpler community RealESRGAN(device).load_weights()/.predict()
197
+ # wrapper this code was first written against — that class doesn't exist
198
+ # in the installed package (real ModuleNotFoundError from a live call).
199
+ import cv2
200
+ from basicsr.archs.rrdbnet_arch import RRDBNet
201
+ from realesrgan import RealESRGANer
202
+ from PIL import Image
203
+
204
+ scale = int(scale)
205
+ arch = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=scale)
206
+ model_url = (
207
+ "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth"
208
+ if scale == 2
209
+ else "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth"
210
+ )
211
+ upsampler = RealESRGANer(
212
+ scale=scale, model_path=model_url, model=arch,
213
+ tile=0, tile_pad=10, pre_pad=0, half=True, device="cuda",
214
+ )
215
 
216
+ img_bgr = cv2.cvtColor(np.array(image.convert("RGB")), cv2.COLOR_RGB2BGR)
217
+ output_bgr, _ = upsampler.enhance(img_bgr, outscale=scale)
218
+ output_rgb = cv2.cvtColor(output_bgr, cv2.COLOR_BGR2RGB)
219
+ return Image.fromarray(output_rgb)
220
 
221
 
222
  # ---------------------------------------------------------------------------
requirements.txt CHANGED
@@ -13,3 +13,4 @@ misaki[en]
13
  f5-tts
14
  gradio_client
15
  realesrgan
 
 
13
  f5-tts
14
  gradio_client
15
  realesrgan
16
+ opencv-python-headless