Create handler.py
Browse files- handler.py +35 -0
handler.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import base64
|
3 |
+
import numpy as np
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
class EndpointHandler:
|
7 |
+
def __init__(self, path=""):
|
8 |
+
print("Handler initialized")
|
9 |
+
# Здесь можно загрузить модель и веса
|
10 |
+
|
11 |
+
def __call__(self, data):
|
12 |
+
image_b64 = data.get("image")
|
13 |
+
if image_b64 is None:
|
14 |
+
return {"error": "No image provided"}
|
15 |
+
|
16 |
+
# Декодируем изображение
|
17 |
+
image = Image.open(BytesIO(base64.b64decode(image_b64)))
|
18 |
+
width, height = image.size
|
19 |
+
|
20 |
+
# Генерируем фейковую маску
|
21 |
+
mask = np.zeros((height, width), dtype=np.uint8)
|
22 |
+
mask[height//4: 3*height//4, width//4: 3*width//4] = 255
|
23 |
+
|
24 |
+
# Преобразуем маску в base64 PNG
|
25 |
+
mask_image = Image.fromarray(mask)
|
26 |
+
buffered = BytesIO()
|
27 |
+
mask_image.save(buffered, format="PNG")
|
28 |
+
mask_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
29 |
+
|
30 |
+
# Возвращаем результат
|
31 |
+
return {
|
32 |
+
"label": "mock-segmentation",
|
33 |
+
"mask": mask_base64, # ⚠️ mask — строка, закодированная в base64
|
34 |
+
"score": 0.99
|
35 |
+
}
|