Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import cv2
|
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
import urllib.request
|
| 9 |
-
from basicsr.utils import img2tensor, tensor2img
|
| 10 |
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
| 11 |
from codeformer_arch import CodeFormer
|
| 12 |
|
|
@@ -24,14 +23,14 @@ def setup_environment():
|
|
| 24 |
model_path = "weights/CodeFormer/codeformer.pth"
|
| 25 |
download_file(model_url, model_path)
|
| 26 |
|
| 27 |
-
# Download
|
| 28 |
-
facelib_url = "https://github.com/
|
| 29 |
-
facelib_path = "weights/facelib.pth"
|
| 30 |
download_file(facelib_url, facelib_path)
|
| 31 |
|
| 32 |
# Download Real-ESRGAN model for background upsampling (optional)
|
| 33 |
-
realesrgan_url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.
|
| 34 |
-
realesrgan_path = "weights/RealESRGAN_x4plus.pth"
|
| 35 |
download_file(realesrgan_url, realesrgan_path)
|
| 36 |
|
| 37 |
# Load CodeFormer model
|
|
@@ -43,6 +42,23 @@ def load_codeformer():
|
|
| 43 |
model = model.to('cpu') # Force CPU
|
| 44 |
return model
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Inference function
|
| 47 |
def enhance_image(input_image, fidelity_weight=0.5, background_enhance=True, face_upsample=False):
|
| 48 |
# Convert PIL image to OpenCV format
|
|
@@ -84,7 +100,7 @@ def enhance_image(input_image, fidelity_weight=0.5, background_enhance=True, fac
|
|
| 84 |
from realesrgan import RealESRGANer
|
| 85 |
upsampler = RealESRGANer(
|
| 86 |
scale=4,
|
| 87 |
-
model_path="weights/RealESRGAN_x4plus.pth",
|
| 88 |
device='cpu'
|
| 89 |
)
|
| 90 |
restored_img, _ = upsampler.enhance(restored_img, outscale=4)
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
import urllib.request
|
|
|
|
| 9 |
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
| 10 |
from codeformer_arch import CodeFormer
|
| 11 |
|
|
|
|
| 23 |
model_path = "weights/CodeFormer/codeformer.pth"
|
| 24 |
download_file(model_url, model_path)
|
| 25 |
|
| 26 |
+
# Download facexlib model (for face detection)
|
| 27 |
+
facelib_url = "https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pth"
|
| 28 |
+
facelib_path = "weights/facelib/detection_Resnet50_Final.pth"
|
| 29 |
download_file(facelib_url, facelib_path)
|
| 30 |
|
| 31 |
# Download Real-ESRGAN model for background upsampling (optional)
|
| 32 |
+
realesrgan_url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth"
|
| 33 |
+
realesrgan_path = "weights/realesrgan/RealESRGAN_x4plus.pth"
|
| 34 |
download_file(realesrgan_url, realesrgan_path)
|
| 35 |
|
| 36 |
# Load CodeFormer model
|
|
|
|
| 42 |
model = model.to('cpu') # Force CPU
|
| 43 |
return model
|
| 44 |
|
| 45 |
+
# Image processing utilities (mimicking basicsr.utils)
|
| 46 |
+
def img2tensor(img, bgr2rgb=True, float32=True):
|
| 47 |
+
if bgr2rgb:
|
| 48 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 49 |
+
img = torch.from_numpy(img.transpose(2, 0, 1)).float()
|
| 50 |
+
if float32:
|
| 51 |
+
img = img / 255.0
|
| 52 |
+
return img
|
| 53 |
+
|
| 54 |
+
def tensor2img(tensor, rgb2bgr=True, min_max=(-1, 1)):
|
| 55 |
+
tensor = tensor.squeeze().float().cpu().clamp_(*min_max)
|
| 56 |
+
tensor = (tensor - min_max[0]) / (min_max[1] - min_max[0]) * 255.0
|
| 57 |
+
img = tensor.numpy().transpose(1, 2, 0).astype(np.uint8)
|
| 58 |
+
if rgb2bgr:
|
| 59 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 60 |
+
return img
|
| 61 |
+
|
| 62 |
# Inference function
|
| 63 |
def enhance_image(input_image, fidelity_weight=0.5, background_enhance=True, face_upsample=False):
|
| 64 |
# Convert PIL image to OpenCV format
|
|
|
|
| 100 |
from realesrgan import RealESRGANer
|
| 101 |
upsampler = RealESRGANer(
|
| 102 |
scale=4,
|
| 103 |
+
model_path="weights/realesrgan/RealESRGAN_x4plus.pth",
|
| 104 |
device='cpu'
|
| 105 |
)
|
| 106 |
restored_img, _ = upsampler.enhance(restored_img, outscale=4)
|