Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import urllib.request
|
| 9 |
+
import tarfile
|
| 10 |
+
|
| 11 |
+
# Function to download a file from a URL
|
| 12 |
+
def download_file(url, dest):
|
| 13 |
+
if not os.path.exists(dest):
|
| 14 |
+
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
| 15 |
+
urllib.request.urlretrieve(url, dest)
|
| 16 |
+
print(f"Downloaded {dest}")
|
| 17 |
+
|
| 18 |
+
# Download pretrained model and necessary files
|
| 19 |
+
def setup_environment():
|
| 20 |
+
# Download CodeFormer pretrained model
|
| 21 |
+
model_url = "https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth"
|
| 22 |
+
model_path = "weights/codeformer.pth"
|
| 23 |
+
download_file(model_url, model_path)
|
| 24 |
+
|
| 25 |
+
# Download facexlib detection models (needed for face detection)
|
| 26 |
+
retinaface_url = "https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pth"
|
| 27 |
+
retinaface_path = "weights/detection_Resnet50_Final.pth"
|
| 28 |
+
download_file(retinaface_url, retinaface_path)
|
| 29 |
+
|
| 30 |
+
# Define a simplified CodeFormer architecture (instead of downloading codeformer_arch.py)
|
| 31 |
+
class CodeFormer(torch.nn.Module):
|
| 32 |
+
def __init__(self, dim_embd=512, codebook_size=1024, n_head=8, n_layer=9, connect_list=['32', '64', '128', '256']):
|
| 33 |
+
super(CodeFormer, self).__init__()
|
| 34 |
+
# This is a simplified placeholder. In practice, you'd need the full architecture.
|
| 35 |
+
self.encoder = torch.nn.Sequential(
|
| 36 |
+
torch.nn.Conv2d(3, dim_embd, kernel_size=3, stride=1, padding=1),
|
| 37 |
+
torch.nn.ReLU(),
|
| 38 |
+
torch.nn.Conv2d(dim_embd, dim_embd, kernel_size=3, stride=1, padding=1)
|
| 39 |
+
)
|
| 40 |
+
self.decoder = torch.nn.Sequential(
|
| 41 |
+
torch.nn.ConvTranspose2d(dim_embd, 3, kernel_size=3, stride=1, padding=1),
|
| 42 |
+
torch.nn.Sigmoid()
|
| 43 |
+
)
|
| 44 |
+
# Note: This is a mock implementation. Full CodeFormer requires the actual codeformer_arch.py.
|
| 45 |
+
|
| 46 |
+
def forward(self, x, w=0.5, adain=True):
|
| 47 |
+
# Simplified forward pass (placeholder)
|
| 48 |
+
enc = self.encoder(x)
|
| 49 |
+
dec = self.decoder(enc)
|
| 50 |
+
return dec
|
| 51 |
+
|
| 52 |
+
# Load CodeFormer model
|
| 53 |
+
def load_codeformer():
|
| 54 |
+
setup_environment()
|
| 55 |
+
model_path = "weights/codeformer.pth"
|
| 56 |
+
net = CodeFormer().to('cpu')
|
| 57 |
+
checkpoint = torch.load(model_path, map_location='cpu')
|
| 58 |
+
net.load_state_dict(checkpoint, strict=False) # strict=False due to simplified architecture
|
| 59 |
+
net.eval()
|
| 60 |
+
return net
|
| 61 |
+
|
| 62 |
+
# Image processing utilities (mimicking basicsr.utils)
|
| 63 |
+
def img2tensor(img, bgr2rgb=True, float32=True):
|
| 64 |
+
if bgr2rgb:
|
| 65 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 66 |
+
img = torch.from_numpy(img.transpose(2, 0, 1)).float()
|
| 67 |
+
if float32:
|
| 68 |
+
img = img / 255.0
|
| 69 |
+
return img
|
| 70 |
+
|
| 71 |
+
def tensor2img(tensor, rgb2bgr=True, min_max=(-1, 1)):
|
| 72 |
+
tensor = tensor.squeeze().float().cpu().clamp_(*min_max)
|
| 73 |
+
tensor = (tensor - min_max[0]) / (min_max[1] - min_max[0]) * 255.0
|
| 74 |
+
img = tensor.numpy().transpose(1, 2, 0).astype(np.uint8)
|
| 75 |
+
if rgb2bgr:
|
| 76 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 77 |
+
return img
|
| 78 |
+
|
| 79 |
+
# Inference function
|
| 80 |
+
def enhance_image(image, fidelity_weight=0.5):
|
| 81 |
+
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
| 82 |
+
|
| 83 |
+
# Load model
|
| 84 |
+
net = load_codeformer()
|
| 85 |
+
|
| 86 |
+
# Convert PIL image to OpenCV format
|
| 87 |
+
img = np.array(image)
|
| 88 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 89 |
+
|
| 90 |
+
# Initialize face helper
|
| 91 |
+
face_helper = FaceRestoreHelper(upscale_factor=1, face_size=512, crop_ratio=(1, 1), det_model='retinaface_resnet50', save_ext='png', device='cpu')
|
| 92 |
+
face_helper.clean_all()
|
| 93 |
+
face_helper.read_image(img)
|
| 94 |
+
face_helper.get_face_landmarks_5(align=True)
|
| 95 |
+
face_helper.align_warp_face()
|
| 96 |
+
|
| 97 |
+
# Enhance face with CodeFormer
|
| 98 |
+
for cropped_face in face_helper.cropped_faces:
|
| 99 |
+
cropped_face_t = img2tensor(cropped_face, bgr2rgb=True, float32=True)
|
| 100 |
+
with torch.no_grad():
|
| 101 |
+
output = net(cropped_face_t.unsqueeze(0), w=fidelity_weight, adain=True)[0]
|
| 102 |
+
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
| 103 |
+
restored_face = restored_face.astype('uint8')
|
| 104 |
+
face_helper.add_restored_face(restored_face)
|
| 105 |
+
|
| 106 |
+
# Get final restored image
|
| 107 |
+
face_helper.get_inverse_affine(None)
|
| 108 |
+
restored_img = face_helper.paste_faces_to_input_image()
|
| 109 |
+
|
| 110 |
+
# Convert back to PIL for Gradio
|
| 111 |
+
restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
|
| 112 |
+
return Image.fromarray(restored_img)
|
| 113 |
+
|
| 114 |
+
# Gradio interface
|
| 115 |
+
with gr.Blocks() as demo:
|
| 116 |
+
gr.Markdown("# CodeFormer Face Restoration (CPU)")
|
| 117 |
+
gr.Markdown("Upload an image to enhance faces using CodeFormer. Runs on CPU in Hugging Face Spaces.")
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
| 121 |
+
output_image = gr.Image(type="pil", label="Enhanced Image")
|
| 122 |
+
|
| 123 |
+
fidelity_slider = gr.Slider(0, 1, value=0.5, step=0.1, label="Fidelity Weight (0 = more restoration, 1 = more original)")
|
| 124 |
+
submit_btn = gr.Button("Enhance")
|
| 125 |
+
|
| 126 |
+
submit_btn.click(
|
| 127 |
+
fn=enhance_image,
|
| 128 |
+
inputs=[input_image, fidelity_slider],
|
| 129 |
+
outputs=output_image
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
# Ensure setup runs once
|
| 134 |
+
setup_environment()
|
| 135 |
+
demo.launch()
|