|
import base64 |
|
import gzip |
|
import numpy as np |
|
from io import BytesIO |
|
from typing import Dict, List, Any |
|
from PIL import Image |
|
import torch |
|
from transformers import SamModel, SamProcessor |
|
|
|
def pack_bits(boolean_tensor): |
|
|
|
flat = boolean_tensor.flatten() |
|
if flat.size()[0] % 8 != 0: |
|
padding = np.zeros((8 - flat.size % 8,), dtype=bool) |
|
flat = np.concatenate([flat, padding]) |
|
|
|
|
|
packed = np.packbits(flat.reshape((-1, 8))) |
|
packed = packed.tobytes() |
|
return gzip.compress(packed) |
|
|
|
|
|
|
|
class EndpointHandler(): |
|
def __init__(self, path=""): |
|
|
|
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
self.model = SamModel.from_pretrained("facebook/sam-vit-base").to(self.device) |
|
self.processor = SamProcessor.from_pretrained("facebook/sam-vit-base") |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
inputs (:obj: `str` | `PIL.Image` | `np.array`) |
|
kwargs |
|
Return: |
|
A :obj:`list` | `dict`: will be serialized and returned |
|
""" |
|
|
|
inputs = data.pop("inputs", data) |
|
parameters = data.pop("parameters", {"mode": "image"}) |
|
|
|
|
|
image = Image.open(BytesIO(base64.b64decode(inputs['image']))).convert("RGB") |
|
input_points = [inputs['points']] |
|
|
|
model_inputs = self.processor(image, input_points=input_points, return_tensors="pt").to(self.device) |
|
outputs = self.model(**model_inputs) |
|
masks = self.processor.image_processor.post_process_masks( |
|
outputs.pred_masks.cpu(), |
|
model_inputs["original_sizes"].cpu(), |
|
model_inputs["reshaped_input_sizes"].cpu()) |
|
scores = outputs.iou_scores |
|
|
|
packed = [base64.b64encode(pack_bits(masks[0][0][i])).decode() for i in range(masks[0].shape[1])] |
|
shape = list(masks[0].shape)[2:] |
|
|
|
return {"masks": packed, "scores": scores[0][0].tolist(), "shape": shape} |
|
|