tooth-sam / app.py
mujtaba1212's picture
Use Gradio API endpoints
b1bce98 verified
Raw History Blame Contribute Delete
2.34 kB
# Tooth outline AI for PixelCeph Workspace.
# The app sends the photo once (set_image), then each click (segment) gets a tooth mask back.
# Photos are kept in memory only (last 30), never saved to disk.
import spaces
import io, json, base64, uuid
from collections import OrderedDict
import numpy as np, torch, gradio as gr
from PIL import Image
from transformers import SamModel, SamProcessor
torch.set_num_threads(2)
MODEL = "Zigeng/SlimSAM-uniform-77"
model = SamModel.from_pretrained(MODEL).eval()
processor = SamProcessor.from_pretrained(MODEL)
CACHE = OrderedDict() # id -> (embeddings, original size, resized size)
@spaces.GPU
def _gpu_check(): # free Spaces run on ZeroGPU, which needs one GPU function; the outline work runs on the CPU
return True
def set_image(jpeg_b64: str) -> str:
img = Image.open(io.BytesIO(base64.b64decode(jpeg_b64))).convert("RGB")
inputs = processor(img, return_tensors="pt")
with torch.no_grad():
emb = model.get_image_embeddings(inputs["pixel_values"])
key = uuid.uuid4().hex
CACHE[key] = (emb, inputs["original_sizes"], inputs["reshaped_input_sizes"])
while len(CACHE) > 30:
CACHE.popitem(last=False)
return key
def segment(payload: str) -> str:
body = json.loads(payload)
if body.get("id") not in CACHE:
return json.dumps({"missing": True}) # the Space restarted: the app sends the photo again
emb, orig, resized = CACHE[body["id"]]
(h, w), (rh, rw) = orig[0].tolist(), resized[0].tolist()
pts = torch.tensor([[[[p["fx"] * rw, p["fy"] * rh] for p in body["points"]]]], dtype=torch.float)
labels = torch.tensor([[[int(p["label"]) for p in body["points"]]]])
with torch.no_grad():
out = model(image_embeddings=emb, input_points=pts, input_labels=labels, multimask_output=True)
masks = processor.post_process_masks(out.pred_masks, orig, resized)[0][0] # 3 candidate masks
best = int(out.iou_scores[0, 0].argmax())
png = io.BytesIO()
Image.fromarray(masks[best].numpy().astype(np.uint8) * 255).save(png, "PNG")
return json.dumps({"w": w, "h": h, "png": base64.b64encode(png.getvalue()).decode()})
with gr.Blocks() as demo:
gr.Markdown("Tooth outline AI for PixelCeph Workspace. Used by the app, nothing to do here.")
gr.api(set_image)
gr.api(segment)
demo.launch()