Upload handler.py
Browse files- handler.py +42 -0
handler.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
from gfpgan import GFPGANer
|
| 3 |
+
import cv2
|
| 4 |
+
from imageio import imread
|
| 5 |
+
from basicsr.utils import imwrite
|
| 6 |
+
import io
|
| 7 |
+
import os
|
| 8 |
+
import numpy as np
|
| 9 |
+
import base64
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class EndpointHandler():
|
| 13 |
+
def __init__(self, path=""):
|
| 14 |
+
self.restorer = GFPGANer(
|
| 15 |
+
model_path="./GFPGANv1.4.pth",
|
| 16 |
+
upscale=2,
|
| 17 |
+
arch="clean",
|
| 18 |
+
channel_multiplier=2,
|
| 19 |
+
bg_upsampler=None)
|
| 20 |
+
|
| 21 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 22 |
+
"""
|
| 23 |
+
data args:
|
| 24 |
+
inputs (:obj: `str`)
|
| 25 |
+
date (:obj: `str`)
|
| 26 |
+
Return:
|
| 27 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
| 28 |
+
"""
|
| 29 |
+
# get inputs
|
| 30 |
+
inputs = data.pop("inputs",data)
|
| 31 |
+
img = imread(io.BytesIO(base64.b64decode(inputs)))
|
| 32 |
+
cropped_faces, restored_faces, restored_img = self.restorer.enhance(
|
| 33 |
+
img,
|
| 34 |
+
has_aligned=False,
|
| 35 |
+
only_center_face=False,
|
| 36 |
+
paste_back=True,
|
| 37 |
+
weight=0.5)
|
| 38 |
+
|
| 39 |
+
for idx, (cropped_face, restored_face) in enumerate(zip(cropped_faces, restored_faces)):
|
| 40 |
+
retval, buffer = cv2.imencode('.png', restored_face)
|
| 41 |
+
jpg_as_text = base64.b64encode(buffer)
|
| 42 |
+
return jpg_as_text
|