File size: 2,357 Bytes
af55182 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
from transformers import AutoImageProcessor, AutoModelForImageSegmentation
from PIL import Image
import torch
import numpy as np
import gradio as gr
import matplotlib.pyplot as plt
# --- ตั้งชื่อโมเดล (ใช้ของ Hugging Face) ---
model_name = "Arya6967/SAMMed-2d" # ตัวอย่าง
processor = AutoImageProcessor.from_pretrained(model_name)
model = AutoModelForImageSegmentation.from_pretrained(model_name)
# --- โหลดภาพ ---
img_path = "your_image.png" # เปลี่ยนเป็นภาพร่างกาย/diagram ของคุณ
img = Image.open(img_path).convert("RGB")
# --- ทำ segmentation ---
inputs = processor(img, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
# --- แปลง logits -> mask ---
mask = outputs.logits.argmax(dim=1)[0].cpu().numpy() # mask ของแต่ละ pixel
# สมมติ organ_id เริ่มจาก 0
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):
# click_data: (x, y) pixel
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 "ไม่ทราบอวัยวะ"
# --- Gradio UI ---
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() |