Add GFPGAN + RealESRGAN hybrid handler
Browse files- handler.py +119 -115
- requirements.txt +1 -0
handler.py
CHANGED
|
@@ -1,134 +1,138 @@
|
|
| 1 |
-
import io
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
import torch
|
| 5 |
import base64
|
| 6 |
-
import logging
|
| 7 |
import requests
|
| 8 |
-
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
from gfpgan import GFPGANer
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# ======================================================
|
| 13 |
-
# LOGGING CONFIGURATION
|
| 14 |
-
# ======================================================
|
| 15 |
-
logging.basicConfig(level=logging.DEBUG)
|
| 16 |
-
logger = logging.getLogger(__name__)
|
| 17 |
-
logger.setLevel(logging.DEBUG)
|
| 18 |
-
logger.debug("π¦ [INIT] Importing GFPGAN handler module...")
|
| 19 |
-
|
| 20 |
-
# ======================================================
|
| 21 |
-
# GFPGAN MODEL URL
|
| 22 |
-
# ======================================================
|
| 23 |
-
MODEL_URL = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
| 24 |
-
MODEL_NAME = "GFPGANv1.4.pth"
|
| 25 |
|
| 26 |
-
|
| 27 |
-
# ======================================================
|
| 28 |
-
# ENDPOINT HANDLER
|
| 29 |
-
# ======================================================
|
| 30 |
class EndpointHandler:
|
| 31 |
def __init__(self, path="."):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
r.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
#
|
| 75 |
-
#
|
| 76 |
def __call__(self, data):
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
# ------------------------------------------------------
|
| 81 |
-
# Handle both JSON base64 and raw bytes
|
| 82 |
-
# ------------------------------------------------------
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
image_bytes = data
|
| 90 |
-
else:
|
| 91 |
-
raise ValueError("Unsupported input format β expected bytes or base64 data")
|
| 92 |
-
|
| 93 |
-
logger.debug(f"π§Ύ [BYTES] Received {len(image_bytes)} bytes")
|
| 94 |
except Exception as e:
|
| 95 |
-
|
| 96 |
-
return {"error":
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
return {"error": f"Image loading failed: {e}"}
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
)
|
| 118 |
-
logger.debug("β
[RESTORE] Face restoration completed successfully.")
|
| 119 |
-
except Exception as e:
|
| 120 |
-
logger.error(f"π₯ [ERROR] GFPGAN enhancement failed: {e}")
|
| 121 |
-
return {"error": f"Enhancement failed: {e}"}
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import io
|
| 3 |
import torch
|
| 4 |
import base64
|
|
|
|
| 5 |
import requests
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from gfpgan import GFPGANer
|
| 8 |
+
from realesrgan import RealESRGANer
|
| 9 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class EndpointHandler:
|
| 13 |
def __init__(self, path="."):
|
| 14 |
+
print("π [INIT] Starting GFPGAN + RealESRGAN hybrid handler initialization...")
|
| 15 |
+
print(f"π Working directory: {os.getcwd()}")
|
| 16 |
+
print(f"π Handler path argument: {path}")
|
| 17 |
+
|
| 18 |
+
# ------------------------------
|
| 19 |
+
# Download GFPGAN v1.4 weights
|
| 20 |
+
# ------------------------------
|
| 21 |
+
self.model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.4.0/GFPGANv1.4.pth"
|
| 22 |
+
self.model_path = os.path.join(path, "GFPGANv1.4.pth")
|
| 23 |
+
if not os.path.exists(self.model_path):
|
| 24 |
+
print(f"π₯ [DOWNLOAD] Fetching GFPGAN v1.4 weights...")
|
| 25 |
+
r = requests.get(self.model_url)
|
| 26 |
+
r.raise_for_status()
|
| 27 |
+
with open(self.model_path, "wb") as f:
|
| 28 |
+
f.write(r.content)
|
| 29 |
+
print(f"β
[MODEL] Downloaded GFPGAN model to {self.model_path}")
|
| 30 |
+
else:
|
| 31 |
+
print("β
[MODEL] GFPGAN weights already exist locally.")
|
| 32 |
+
|
| 33 |
+
# ------------------------------
|
| 34 |
+
# Setup background upsampler (Real-ESRGAN)
|
| 35 |
+
# ------------------------------
|
| 36 |
+
print("π§ [INIT] Setting up Real-ESRGAN background upsampler...")
|
| 37 |
+
rrdbnet = RRDBNet(
|
| 38 |
+
num_in_ch=3, num_out_ch=3,
|
| 39 |
+
num_feat=64, num_block=23,
|
| 40 |
+
num_grow_ch=32, scale=4
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
self.bg_upsampler = RealESRGANer(
|
| 44 |
+
scale=2,
|
| 45 |
+
model_path=None, # auto-download model weights
|
| 46 |
+
model=rrdbnet,
|
| 47 |
+
tile=400,
|
| 48 |
+
tile_pad=10,
|
| 49 |
+
pre_pad=0,
|
| 50 |
+
half=False,
|
| 51 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 52 |
+
)
|
| 53 |
+
print("β
[INIT] Real-ESRGAN background upsampler ready.")
|
| 54 |
+
|
| 55 |
+
# ------------------------------
|
| 56 |
+
# Setup GFPGANer
|
| 57 |
+
# ------------------------------
|
| 58 |
+
print("π§ [INIT] Setting up GFPGANer (v1.4)...")
|
| 59 |
+
self.restorer = GFPGANer(
|
| 60 |
+
model_path=self.model_path,
|
| 61 |
+
upscale=2,
|
| 62 |
+
arch="clean",
|
| 63 |
+
channel_multiplier=2,
|
| 64 |
+
bg_upsampler=self.bg_upsampler,
|
| 65 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 66 |
+
)
|
| 67 |
+
print("β
[INIT DONE] GFPGAN + RealESRGAN hybrid handler ready.")
|
| 68 |
|
| 69 |
+
# ----------------------------------------------------------
|
| 70 |
+
# Main inference entry point
|
| 71 |
+
# ----------------------------------------------------------
|
| 72 |
def __call__(self, data):
|
| 73 |
+
print("π°οΈ [CALL] Endpoint invoked!")
|
| 74 |
+
print(f"π¦ [CALL] Raw input type: {type(data)}")
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
try:
|
| 77 |
+
image = self.preprocess(data)
|
| 78 |
+
print("π§© [STEP] Image preprocessed successfully.")
|
| 79 |
+
restored = self.inference(image)
|
| 80 |
+
print("π¨ [STEP] Inference completed successfully.")
|
| 81 |
+
return self.postprocess(restored)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
+
print(f"π₯ [ERROR] Exception during call: {str(e)}")
|
| 84 |
+
return {"error": str(e)}
|
| 85 |
|
| 86 |
+
# ----------------------------------------------------------
|
| 87 |
+
# Preprocessing
|
| 88 |
+
# ----------------------------------------------------------
|
| 89 |
+
def preprocess(self, data):
|
| 90 |
+
print("π§ [PREPROCESS] Starting...")
|
| 91 |
+
if isinstance(data, (bytes, bytearray)):
|
| 92 |
+
print("πΌοΈ [PREPROCESS] Raw bytes detected.")
|
| 93 |
+
return Image.open(io.BytesIO(data)).convert("RGB")
|
|
|
|
| 94 |
|
| 95 |
+
if isinstance(data, dict):
|
| 96 |
+
img_field = data.get("inputs") or data.get("image")
|
| 97 |
+
if isinstance(img_field, str):
|
| 98 |
+
print("𧬠[PREPROCESS] Base64 string detected.")
|
| 99 |
+
decoded = base64.b64decode(img_field)
|
| 100 |
+
return Image.open(io.BytesIO(decoded)).convert("RGB")
|
| 101 |
+
if isinstance(img_field, (bytes, bytearray)):
|
| 102 |
+
print("π§© [PREPROCESS] Byte array detected.")
|
| 103 |
+
return Image.open(io.BytesIO(img_field)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
raise ValueError("Unsupported input format β expected bytes or base64 data.")
|
| 106 |
+
|
| 107 |
+
# ----------------------------------------------------------
|
| 108 |
+
# Inference
|
| 109 |
+
# ----------------------------------------------------------
|
| 110 |
+
def inference(self, image):
|
| 111 |
+
print("βοΈ [INFERENCE] Running GFPGAN + RealESRGAN enhancement...")
|
| 112 |
+
cropped_faces, restored_faces, restored_img = self.restorer.enhance(
|
| 113 |
+
image,
|
| 114 |
+
has_aligned=False,
|
| 115 |
+
only_center_face=False,
|
| 116 |
+
paste_back=True,
|
| 117 |
+
)
|
| 118 |
+
print(f"β
[INFERENCE] Restored image size: {restored_img.shape}")
|
| 119 |
+
return restored_img
|
| 120 |
+
|
| 121 |
+
# ----------------------------------------------------------
|
| 122 |
+
# Postprocess
|
| 123 |
+
# ----------------------------------------------------------
|
| 124 |
+
def postprocess(self, restored_img):
|
| 125 |
+
print("π€ [POSTPROCESS] Encoding restored image...")
|
| 126 |
+
if isinstance(restored_img, torch.Tensor):
|
| 127 |
+
restored_img = restored_img.detach().cpu().numpy()
|
| 128 |
+
|
| 129 |
+
# Convert numpy to PIL if needed
|
| 130 |
+
if not isinstance(restored_img, Image.Image):
|
| 131 |
+
restored_img = Image.fromarray(restored_img[..., ::-1]) # BGR -> RGB
|
| 132 |
+
|
| 133 |
+
buf = io.BytesIO()
|
| 134 |
+
restored_img.save(buf, format="PNG")
|
| 135 |
+
encoded = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 136 |
+
print("β
[POSTPROCESS] Image encoding complete.")
|
| 137 |
+
return {"image": encoded}
|
| 138 |
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
torch==2.1.0
|
| 2 |
torchvision==0.16.0
|
| 3 |
gfpgan==1.3.8
|
|
|
|
| 4 |
basicsr==1.4.2
|
| 5 |
facexlib==0.3.0
|
| 6 |
opencv-python
|
|
|
|
| 1 |
torch==2.1.0
|
| 2 |
torchvision==0.16.0
|
| 3 |
gfpgan==1.3.8
|
| 4 |
+
realesrgan==0.3.0
|
| 5 |
basicsr==1.4.2
|
| 6 |
facexlib==0.3.0
|
| 7 |
opencv-python
|