Update handler.py
Browse files- handler.py +21 -21
handler.py
CHANGED
@@ -6,29 +6,29 @@ import numpy as np
|
|
6 |
# Здесь предполагается, что у вас есть функция segment(image: PIL.Image) -> np.ndarray (маска)
|
7 |
from medsam2_model import MedSAM2
|
8 |
|
9 |
-
# Загрузка модели
|
10 |
-
model = MedSAM2("MedSAM2_pretrain_10ep_b1_AMD-SD_sam2_hiera_t.pth")
|
11 |
-
|
12 |
def image_to_base64(image: Image.Image) -> str:
|
13 |
buffered = io.BytesIO()
|
14 |
image.save(buffered, format="PNG")
|
15 |
return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode()
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
6 |
# Здесь предполагается, что у вас есть функция segment(image: PIL.Image) -> np.ndarray (маска)
|
7 |
from medsam2_model import MedSAM2
|
8 |
|
|
|
|
|
|
|
9 |
def image_to_base64(image: Image.Image) -> str:
|
10 |
buffered = io.BytesIO()
|
11 |
image.save(buffered, format="PNG")
|
12 |
return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode()
|
13 |
|
14 |
+
class EndpointHandler():
|
15 |
+
def __init__(self, path=""):
|
16 |
+
model = MedSAM2("MedSAM2_pretrain_10ep_b1_AMD-SD_sam2_hiera_t.pth")
|
17 |
+
|
18 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
19 |
+
if isinstance(data, dict) and "image" in data:
|
20 |
+
image_data = data["image"]
|
21 |
+
if image_data.startswith("data:image"):
|
22 |
+
header, base64_data = image_data.split(",", 1)
|
23 |
+
image = Image.open(io.BytesIO(base64.b64decode(base64_data)))
|
24 |
+
|
25 |
+
# Получаем маску
|
26 |
+
mask_array = model.predict(image_np, box) # Предполагается бинарная маска (0 и 1)
|
27 |
+
mask_pil = Image.fromarray((mask_array * 255).astype(np.uint8))
|
28 |
+
|
29 |
+
return [{
|
30 |
+
"label": "mock-segmentation",
|
31 |
+
"mask": image_to_base64(mask_pil),
|
32 |
+
"score": 0.99
|
33 |
+
}]
|
34 |
+
return [{"label": "mock-segmentation", "mask": None, "score": 0.0}]
|