2026-03-22 04:31:49 +08:00
|
|
|
"""Image upscaling with Real-ESRGAN fallback to Lanczos."""
|
|
|
|
|
import sys
|
|
|
|
|
import json
|
2026-04-10 13:21:06 +08:00
|
|
|
import os
|
2026-03-22 04:31:49 +08:00
|
|
|
|
|
|
|
|
|
2026-03-23 01:38:19 +08:00
|
|
|
def emit_progress(percent, stage):
|
|
|
|
|
"""Emit structured progress to stderr for bridge.ts to capture."""
|
|
|
|
|
print(json.dumps({"progress": percent, "stage": stage}), file=sys.stderr, flush=True)
|
|
|
|
|
|
|
|
|
|
|
2026-04-10 13:21:06 +08:00
|
|
|
REALESRGAN_MODEL_PATH = os.environ.get(
|
|
|
|
|
"REALESRGAN_MODEL_PATH",
|
|
|
|
|
"/opt/models/realesrgan/RealESRGAN_x4plus.pth",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-03-22 04:31:49 +08:00
|
|
|
def main():
|
|
|
|
|
input_path = sys.argv[1]
|
|
|
|
|
output_path = sys.argv[2]
|
|
|
|
|
settings = json.loads(sys.argv[3]) if len(sys.argv) > 3 else {}
|
|
|
|
|
|
|
|
|
|
scale = settings.get("scale", 2)
|
|
|
|
|
|
|
|
|
|
try:
|
2026-03-23 01:38:19 +08:00
|
|
|
emit_progress(10, "Loading upscale model")
|
2026-03-22 04:31:49 +08:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
img = Image.open(input_path)
|
|
|
|
|
new_size = (img.width * scale, img.height * scale)
|
|
|
|
|
|
|
|
|
|
# Try Real-ESRGAN first
|
|
|
|
|
try:
|
2026-04-10 13:21:06 +08:00
|
|
|
# Redirect stdout to stderr so basicsr/realesrgan init messages
|
|
|
|
|
# cannot contaminate our JSON result on stdout.
|
|
|
|
|
stdout_fd = os.dup(1)
|
|
|
|
|
os.dup2(2, 1)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
|
|
from realesrgan import RealESRGANer
|
|
|
|
|
from gpu import gpu_available
|
|
|
|
|
import numpy as np
|
|
|
|
|
import torch
|
|
|
|
|
finally:
|
|
|
|
|
# Restore stdout after imports
|
|
|
|
|
os.dup2(stdout_fd, 1)
|
|
|
|
|
os.close(stdout_fd)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(REALESRGAN_MODEL_PATH):
|
|
|
|
|
raise FileNotFoundError(f"RealESRGAN model not found: {REALESRGAN_MODEL_PATH}")
|
2026-04-05 19:12:45 +08:00
|
|
|
|
|
|
|
|
use_gpu = gpu_available()
|
|
|
|
|
device = torch.device("cuda" if use_gpu else "cpu")
|
2026-03-22 04:31:49 +08:00
|
|
|
|
2026-04-10 13:21:06 +08:00
|
|
|
# RealESRGAN_x4plus is a 4x model internally
|
2026-03-22 04:31:49 +08:00
|
|
|
model = RRDBNet(
|
|
|
|
|
num_in_ch=3,
|
|
|
|
|
num_out_ch=3,
|
|
|
|
|
num_feat=64,
|
|
|
|
|
num_block=23,
|
|
|
|
|
num_grow_ch=32,
|
2026-04-10 13:21:06 +08:00
|
|
|
scale=4,
|
2026-03-22 04:31:49 +08:00
|
|
|
)
|
|
|
|
|
upsampler = RealESRGANer(
|
2026-04-10 13:21:06 +08:00
|
|
|
scale=4,
|
|
|
|
|
model_path=REALESRGAN_MODEL_PATH,
|
2026-03-22 04:31:49 +08:00
|
|
|
model=model,
|
2026-04-05 19:12:45 +08:00
|
|
|
half=use_gpu,
|
|
|
|
|
device=device,
|
2026-03-22 04:31:49 +08:00
|
|
|
)
|
2026-03-23 01:38:19 +08:00
|
|
|
emit_progress(20, "Model ready")
|
2026-03-22 04:31:49 +08:00
|
|
|
img_array = np.array(img.convert("RGB"))
|
2026-03-23 01:38:19 +08:00
|
|
|
emit_progress(25, "Upscaling image")
|
2026-03-22 04:31:49 +08:00
|
|
|
output, _ = upsampler.enhance(img_array, outscale=scale)
|
2026-03-23 01:38:19 +08:00
|
|
|
emit_progress(90, "Upscaling complete")
|
2026-03-22 04:31:49 +08:00
|
|
|
result = Image.fromarray(output)
|
2026-03-23 01:38:19 +08:00
|
|
|
emit_progress(95, "Saving result")
|
2026-03-22 04:31:49 +08:00
|
|
|
result.save(output_path)
|
|
|
|
|
method = "realesrgan"
|
2026-04-10 13:21:06 +08:00
|
|
|
except (ImportError, FileNotFoundError, RuntimeError, OSError):
|
|
|
|
|
# RealESRGAN unavailable or failed - fall back to Lanczos
|
2026-03-23 01:38:19 +08:00
|
|
|
emit_progress(50, "Upscaling with Lanczos")
|
2026-03-22 04:31:49 +08:00
|
|
|
img_upscaled = img.resize(new_size, Image.LANCZOS)
|
2026-03-23 01:38:19 +08:00
|
|
|
emit_progress(95, "Saving result")
|
2026-03-22 04:31:49 +08:00
|
|
|
img_upscaled.save(output_path)
|
|
|
|
|
method = "lanczos"
|
|
|
|
|
|
|
|
|
|
print(
|
|
|
|
|
json.dumps(
|
|
|
|
|
{
|
|
|
|
|
"success": True,
|
|
|
|
|
"scale": scale,
|
|
|
|
|
"width": new_size[0],
|
|
|
|
|
"height": new_size[1],
|
|
|
|
|
"method": method,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
print(
|
|
|
|
|
json.dumps(
|
|
|
|
|
{
|
|
|
|
|
"success": False,
|
|
|
|
|
"error": "Pillow is not installed. Install with: pip install Pillow",
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(json.dumps({"success": False, "error": str(e)}))
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|