Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, cv2, numpy as np, onnxruntime as ort, gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
|
| 4 |
+
# --- MODEL DIRECTORY ---
|
| 5 |
+
# Friendly labels for "noobs" mapped to technical repos
|
| 6 |
+
MODEL_OPTIONS = {
|
| 7 |
+
"π Pro Detail (APISR RRDB)": {
|
| 8 |
+
"repo": "Xenova/2x_APISR_RRDB_GAN_generator-onnx",
|
| 9 |
+
"desc": "Best for AI art and textures. High-quality reconstruction.",
|
| 10 |
+
"scale": 2
|
| 11 |
+
},
|
| 12 |
+
"β‘ Fast & Sharp (APISR GRL)": {
|
| 13 |
+
"repo": "Xenova/4x_APISR_GRL_GAN_generator-onnx",
|
| 14 |
+
"desc": "Best for architecture and sharp lines. Very clean results.",
|
| 15 |
+
"scale": 4
|
| 16 |
+
},
|
| 17 |
+
"πΈ Realistic Photos (Swin2SR)": {
|
| 18 |
+
"repo": "Xenova/swin2SR-realworld-sr-x4-64-bsrgan-psnr",
|
| 19 |
+
"desc": "Best for real-life photography. Keeps skin and nature natural.",
|
| 20 |
+
"scale": 4
|
| 21 |
+
},
|
| 22 |
+
"π§Ή Noise Cleanup (Swin2SR Compressed)": {
|
| 23 |
+
"repo": "Xenova/swin2SR-compressed-sr-x4-48",
|
| 24 |
+
"desc": "Best for blurry, low-quality internet images with 'blocks'.",
|
| 25 |
+
"scale": 4
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Global dictionary to cache loaded models
|
| 30 |
+
loaded_sessions = {}
|
| 31 |
+
|
| 32 |
+
def get_session(model_key):
|
| 33 |
+
if model_key not in loaded_sessions:
|
| 34 |
+
repo = MODEL_OPTIONS[model_key]["repo"]
|
| 35 |
+
path = hf_hub_download(repo_id=repo, filename="onnx/model.onnx")
|
| 36 |
+
opts = ort.SessionOptions()
|
| 37 |
+
opts.intra_op_num_threads = 2
|
| 38 |
+
loaded_sessions[model_key] = ort.InferenceSession(path, opts, providers=['CPUExecutionProvider'])
|
| 39 |
+
return loaded_sessions[model_key]
|
| 40 |
+
|
| 41 |
+
def upscale_image_tiled(frame, model_key, tile_size=128, overlap=16):
|
| 42 |
+
h, w, c = frame.shape
|
| 43 |
+
scale = MODEL_OPTIONS[model_key]["scale"]
|
| 44 |
+
session = get_session(model_key)
|
| 45 |
+
|
| 46 |
+
output_h, output_w = h * scale, w * scale
|
| 47 |
+
upscaled_img = np.zeros((output_h, output_w, c), dtype=np.uint8)
|
| 48 |
+
|
| 49 |
+
stride = tile_size - (overlap * 2)
|
| 50 |
+
|
| 51 |
+
for y in range(0, h, stride):
|
| 52 |
+
for x in range(0, w, stride):
|
| 53 |
+
y1, y2 = max(0, y - overlap), min(h, y + stride + overlap)
|
| 54 |
+
x1, x2 = max(0, x - overlap), min(w, x + stride + overlap)
|
| 55 |
+
|
| 56 |
+
tile = frame[y1:y2, x1:x2]
|
| 57 |
+
|
| 58 |
+
# Pad to multiple of 8 for APISR/Swin2SR compatibility
|
| 59 |
+
th, tw = tile.shape[:2]
|
| 60 |
+
pad_h = (8 - (th % 8)) % 8
|
| 61 |
+
pad_w = (8 - (tw % 8)) % 8
|
| 62 |
+
if pad_h > 0 or pad_w > 0:
|
| 63 |
+
tile = cv2.copyMakeBorder(tile, 0, pad_h, 0, pad_w, cv2.BORDER_REFLECT)
|
| 64 |
+
|
| 65 |
+
# AI Inference
|
| 66 |
+
img_input = cv2.cvtColor(tile, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0
|
| 67 |
+
img_input = np.transpose(img_input, (2, 0, 1))[np.newaxis, :]
|
| 68 |
+
|
| 69 |
+
output = session.run(None, {session.get_inputs()[0].name: img_input})[0]
|
| 70 |
+
|
| 71 |
+
# Post-process tile
|
| 72 |
+
tile_out = np.clip(np.squeeze(output), 0, 1).transpose(1, 2, 0)
|
| 73 |
+
tile_out = cv2.cvtColor((tile_out * 255.0).astype(np.uint8), cv2.COLOR_RGB2BGR)
|
| 74 |
+
|
| 75 |
+
# Remove padding and overlap
|
| 76 |
+
tile_out = tile_out[:(th*scale), :(tw*scale)]
|
| 77 |
+
oy1, ox1 = (y - y1) * scale, (x - x1) * scale
|
| 78 |
+
py1, py2 = y * scale, min(output_h, (y + stride) * scale)
|
| 79 |
+
px1, px2 = x * scale, min(output_w, (x + stride) * scale)
|
| 80 |
+
|
| 81 |
+
upscaled_img[py1:py2, px1:px2] = tile_out[oy1 : oy1 + (py2-py1), ox1 : ox1 + (px2-px1)]
|
| 82 |
+
|
| 83 |
+
return upscaled_img
|
| 84 |
+
|
| 85 |
+
def run_universal(img_data, model_choice, sharpen):
|
| 86 |
+
if img_data is None: return None
|
| 87 |
+
img = img_data["composite"]
|
| 88 |
+
if img.shape[2] == 4: img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
|
| 89 |
+
|
| 90 |
+
res = upscale_image_tiled(img, model_choice)
|
| 91 |
+
|
| 92 |
+
if sharpen > 0:
|
| 93 |
+
blurred = cv2.GaussianBlur(res, (0, 0), 3)
|
| 94 |
+
res = cv2.addWeighted(res, 1 + sharpen, blurred, -sharpen, 0)
|
| 95 |
+
|
| 96 |
+
return res
|
| 97 |
+
|
| 98 |
+
# --- UI DESIGN ---
|
| 99 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 100 |
+
gr.Markdown("# π Universal AI Image Enhancer")
|
| 101 |
+
gr.Markdown("Pick a goal for your image and let the AI handle the rest.")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
with gr.Column():
|
| 105 |
+
image_in = gr.ImageEditor(label="Upload Image", type="numpy")
|
| 106 |
+
|
| 107 |
+
# Friendly Selector
|
| 108 |
+
model_dropdown = gr.Dropdown(
|
| 109 |
+
choices=list(MODEL_OPTIONS.keys()),
|
| 110 |
+
value="π Pro Detail (APISR RRDB)",
|
| 111 |
+
label="What is your goal?"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
sharp_slider = gr.Slider(0, 0.5, value=0.15, label="Sharpness Boost")
|
| 115 |
+
submit_btn = gr.Button("π ENHANCE IMAGE", variant="primary")
|
| 116 |
+
|
| 117 |
+
with gr.Column():
|
| 118 |
+
image_out = gr.Image(label="Upscaled Result")
|
| 119 |
+
|
| 120 |
+
submit_btn.click(run_universal, [image_in, model_dropdown, sharp_slider], image_out)
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
demo.queue().launch()
|