|
import torch |
|
from PIL import Image |
|
from io import BytesIO |
|
from realesrgan import RealESRGANer |
|
from typing import Dict, List, Any |
|
import os |
|
from pathlib import Path |
|
from basicsr.archs.rrdbnet_arch import RRDBNet |
|
import numpy as np |
|
import cv2 |
|
|
|
|
|
import torch |
|
import base64 |
|
|
|
|
|
|
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
self.model = RealESRGANer( |
|
scale=4, |
|
model_path=f"/repository/weights/Real-ESRGAN-x4plus.pth", |
|
|
|
model= RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4), |
|
tile=1000, |
|
tile_pad=10, |
|
|
|
half=True, |
|
|
|
) |
|
|
|
def __call__(self, data: Any) -> Dict[str, List[float]]: |
|
inputs = data.pop("inputs", data) |
|
outscale = 3 |
|
|
|
|
|
image = Image.open(BytesIO(base64.b64decode(inputs['image']))) |
|
|
|
opencv_image = np.array(image) |
|
|
|
opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_RGB2BGR) |
|
output, _ = self.model.enhance(opencv_image, outscale=outscale) |
|
|
|
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB) |
|
|
|
img_byte_arr = BytesIO() |
|
output = Image.fromarray(output) |
|
output.save(img_byte_arr, format='PNG') |
|
img_str = base64.b64encode(img_byte_arr.getvalue()) |
|
return {"out_image": img_str.decode()} |