Add GFPGAN + RealESRGAN hybrid handler
Browse filesFiles changed (2)
- handler.py+119-115
- requirements.txt+1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134import ioimport osimport cv2import torchimport base64import loggingimport requestsimport numpy as npfrom PIL import Imagefrom gfpgan import GFPGANer# ======================================================# LOGGING CONFIGURATION# ======================================================logging.basicConfig(level=logging.DEBUG)logger = logging.getLogger(__name__)logger.setLevel(logging.DEBUG)logger.debug("π¦ [INIT] Importing GFPGAN handler module...")# ======================================================# GFPGAN MODEL URL# ======================================================MODEL_URL = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"MODEL_NAME = "GFPGANv1.4.pth"# ======================================================# ENDPOINT HANDLER# ======================================================class EndpointHandler:def __init__(self, path="."):logger.debug("π [INIT] Starting GFPGAN EndpointHandler initialization...")logger.debug(f"π Working directory: {os.getcwd()}")logger.debug(f"π Handler path argument: {path}")model_path = os.path.join(path, MODEL_NAME)logger.debug(f"π [MODEL] Expected model path: {model_path}")# ------------------------------------------------------# Download model if missing# ------------------------------------------------------if not os.path.exists(model_path):try:logger.debug(f"π₯ [DOWNLOAD] Model not found locally β fetching from {MODEL_URL}")r = requests.get(MODEL_URL, stream=True)r.raise_for_status()with open(model_path, "wb") as f:for chunk in r.iter_content(chunk_size=8192):if chunk:f.write(chunk)logger.debug("β [MODEL] Downloaded GFPGAN weights successfully.")except Exception as e:logger.error(f"π₯ [ERROR] Failed to download GFPGAN weights: {e}")raise# ------------------------------------------------------# Initialize GFPGANer (same as official Gradio demo)# ------------------------------------------------------try:logger.debug("π§ [MODEL] Initializing GFPGANer (upscale=2, arch='clean')...")self.restorer = GFPGANer(model_path=model_path,upscale=2, # Rescaling factor = 2arch="clean",channel_multiplier=2,bg_upsampler=None)logger.debug("β [MODEL] GFPGAN model initialized successfully.")except Exception as e:logger.error(f"π₯ [ERROR] Model initialization failed: {e}")raise# ======================================================# INFERENCE CALL# ======================================================def __call__(self, data):logger.debug("βοΈ [INFER] Starting inference...")logger.debug(f"π₯ Incoming data type: {type(data)}")# ------------------------------------------------------# Handle both JSON base64 and raw bytes# ------------------------------------------------------try:if isinstance(data, dict) and "inputs" in data:logger.debug("π¦ Detected JSON base64 input")image_bytes = base64.b64decode(data["inputs"])elif isinstance(data, (bytes, bytearray)):logger.debug("π¦ Detected raw bytes input")image_bytes = dataelse:raise ValueError("Unsupported input format β expected bytes or base64 data")logger.debug(f"π§Ύ [BYTES] Received {len(image_bytes)} bytes")except Exception as e:logger.error(f"π₯ [ERROR] Input parsing failed: {e}")return {"error": f"Invalid input: {e}"}# ------------------------------------------------------# Decode image# ------------------------------------------------------try:img_np = np.array(Image.open(io.BytesIO(image_bytes)).convert("RGB"))logger.debug(f"πΌοΈ [IMAGE] Loaded image of shape: {img_np.shape}")except Exception as e:logger.error(f"π₯ [ERROR] Failed to load image: {e}")return {"error": f"Image loading failed: {e}"}# ------------------------------------------------------# Run GFPGAN restoration# ------------------------------------------------------try:cropped_faces, restored_faces, restored_img = self.restorer.enhance(img_np,has_aligned=False,only_center_face=False,paste_back=True # Matches GFPGAN web demo)logger.debug("β [RESTORE] Face restoration completed successfully.")except Exception as e:logger.error(f"π₯ [ERROR] GFPGAN enhancement failed: {e}")return {"error": f"Enhancement failed: {e}"}# ------------------------------------------------------# Encode result as base64 PNG# ------------------------------------------------------try:_, buffer = cv2.imencode(".png", restored_img[:, :, ::-1]) # BGRβRGBimg_base64 = base64.b64encode(buffer).decode("utf-8")logger.debug("π€ [ENCODE] Encoded restored image successfully.")return {"image": img_base64}except Exception as e:logger.error(f"π₯ [ERROR] Failed to encode image: {e}")return {"error": f"Encoding failed: {e}"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import osimport ioimport torchimport base64import requestsfrom PIL import Imagefrom gfpgan import GFPGANerfrom realesrgan import RealESRGANerfrom basicsr.archs.rrdbnet_arch import RRDBNetclass EndpointHandler:def __init__(self, path="."):print("π [INIT] Starting GFPGAN + RealESRGAN hybrid handler initialization...")print(f"π Working directory: {os.getcwd()}")print(f"π Handler path argument: {path}")# ------------------------------# Download GFPGAN v1.4 weights# ------------------------------self.model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.4.0/GFPGANv1.4.pth"self.model_path = os.path.join(path, "GFPGANv1.4.pth")if not os.path.exists(self.model_path):print(f"π₯ [DOWNLOAD] Fetching GFPGAN v1.4 weights...")r = requests.get(self.model_url)r.raise_for_status()with open(self.model_path, "wb") as f:f.write(r.content)print(f"β [MODEL] Downloaded GFPGAN model to {self.model_path}")else:print("β [MODEL] GFPGAN weights already exist locally.")# ------------------------------# Setup background upsampler (Real-ESRGAN)# ------------------------------print("π§ [INIT] Setting up Real-ESRGAN background upsampler...")rrdbnet = RRDBNet(num_in_ch=3, num_out_ch=3,num_feat=64, num_block=23,num_grow_ch=32, scale=4)self.bg_upsampler = RealESRGANer(scale=2,model_path=None, # auto-download model weightsmodel=rrdbnet,tile=400,tile_pad=10,pre_pad=0,half=False,device="cuda" if torch.cuda.is_available() else "cpu",)print("β [INIT] Real-ESRGAN background upsampler ready.")# ------------------------------# Setup GFPGANer# ------------------------------print("π§ [INIT] Setting up GFPGANer (v1.4)...")self.restorer = GFPGANer(model_path=self.model_path,upscale=2,arch="clean",channel_multiplier=2,bg_upsampler=self.bg_upsampler,device="cuda" if torch.cuda.is_available() else "cpu",)print("β [INIT DONE] GFPGAN + RealESRGAN hybrid handler ready.")# ----------------------------------------------------------# Main inference entry point# ----------------------------------------------------------def __call__(self, data):print("π°οΈ [CALL] Endpoint invoked!")print(f"π¦ [CALL] Raw input type: {type(data)}")try:image = self.preprocess(data)print("π§© [STEP] Image preprocessed successfully.")restored = self.inference(image)print("π¨ [STEP] Inference completed successfully.")return self.postprocess(restored)except Exception as e:print(f"π₯ [ERROR] Exception during call: {str(e)}")return {"error": str(e)}# ----------------------------------------------------------# Preprocessing# ----------------------------------------------------------def preprocess(self, data):print("π§ [PREPROCESS] Starting...")if isinstance(data, (bytes, bytearray)):print("πΌοΈ [PREPROCESS] Raw bytes detected.")return Image.open(io.BytesIO(data)).convert("RGB")if isinstance(data, dict):img_field = data.get("inputs") or data.get("image")if isinstance(img_field, str):print("𧬠[PREPROCESS] Base64 string detected.")decoded = base64.b64decode(img_field)return Image.open(io.BytesIO(decoded)).convert("RGB")if isinstance(img_field, (bytes, bytearray)):print("π§© [PREPROCESS] Byte array detected.")return Image.open(io.BytesIO(img_field)).convert("RGB")raise ValueError("Unsupported input format β expected bytes or base64 data.")# ----------------------------------------------------------# Inference# ----------------------------------------------------------def inference(self, image):print("βοΈ [INFERENCE] Running GFPGAN + RealESRGAN enhancement...")cropped_faces, restored_faces, restored_img = self.restorer.enhance(image,has_aligned=False,only_center_face=False,paste_back=True,)print(f"β [INFERENCE] Restored image size: {restored_img.shape}")return restored_img# ----------------------------------------------------------# Postprocess# ----------------------------------------------------------def postprocess(self, restored_img):print("π€ [POSTPROCESS] Encoding restored image...")if isinstance(restored_img, torch.Tensor):restored_img = restored_img.detach().cpu().numpy()# Convert numpy to PIL if neededif not isinstance(restored_img, Image.Image):restored_img = Image.fromarray(restored_img[..., ::-1]) # BGR -> RGBbuf = io.BytesIO()restored_img.save(buf, format="PNG")encoded = base64.b64encode(buf.getvalue()).decode("utf-8")print("β [POSTPROCESS] Image encoding complete.")return {"image": encoded}
123456torch==2.1.0torchvision==0.16.0gfpgan==1.3.8basicsr==1.4.2facexlib==0.3.0opencv-python1234567torch==2.1.0torchvision==0.16.0gfpgan==1.3.8realesrgan==0.3.0basicsr==1.4.2facexlib==0.3.0opencv-python
handler.py+119-115
requirements.txt+1