|
|
from transformers import AutoImageProcessor, AutoModelForImageSegmentation |
|
|
from PIL import Image |
|
|
import torch |
|
|
import numpy as np |
|
|
import gradio as gr |
|
|
import matplotlib.pyplot as plt |
|
|
|
|
|
|
|
|
model_name = "Arya6967/SAMMed-2d" |
|
|
processor = AutoImageProcessor.from_pretrained(model_name) |
|
|
model = AutoModelForImageSegmentation.from_pretrained(model_name) |
|
|
|
|
|
|
|
|
img_path = "your_image.png" |
|
|
img = Image.open(img_path).convert("RGB") |
|
|
|
|
|
|
|
|
inputs = processor(img, return_tensors="pt") |
|
|
with torch.no_grad(): |
|
|
outputs = model(**inputs) |
|
|
|
|
|
|
|
|
mask = outputs.logits.argmax(dim=1)[0].cpu().numpy() |
|
|
|
|
|
organ_names = [ |
|
|
"Heart", "Liver", "Lung L", "Lung R", "Stomach", "Kidney L", "Kidney R", |
|
|
"Brain", "Pancreas", "Spleen", "Bladder", "Intestine", "Esophagus", "Gallbladder", |
|
|
"Appendix", "Thyroid", "Trachea", "Aorta", "Vena Cava", "Pelvis", |
|
|
"Femur L", "Femur R", "Humerus L", "Humerus R", "Radius L", "Radius R", |
|
|
"Ulna L", "Ulna R", "Clavicle L", "Clavicle R", "Scapula L", "Scapula R", |
|
|
"Skull", "Spine", "Ribcage", "Sternum", "Patella L", "Patella R", |
|
|
"Tibia L", "Tibia R", "Fibula L", "Fibula R", "Shoulder Joint L", "Shoulder Joint R", |
|
|
"Elbow Joint L", "Elbow Joint R", "Knee Joint L", "Knee Joint R", "Ankle Joint L", "Ankle Joint R" |
|
|
] |
|
|
|
|
|
|
|
|
def show_organ(click_data): |
|
|
|
|
|
x = int(click_data[0]) |
|
|
y = int(click_data[1]) |
|
|
organ_id = mask[y, x] |
|
|
if organ_id < len(organ_names): |
|
|
return f"อวัยวะ: {organ_names[organ_id]}" |
|
|
else: |
|
|
return "ไม่ทราบอวัยวะ" |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("## คลิกบนภาพเพื่อดูอวัยวะ") |
|
|
img_component = gr.Image(value=np.array(img), interactive=True, tool="select") |
|
|
output_text = gr.Textbox(label="ผลลัพธ์") |
|
|
img_component.select(show_organ, inputs=img_component, outputs=output_text) |
|
|
|
|
|
demo.launch() |