Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoImageProcessor, AutoModelForImageSegmentation
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
# --- ตั้งชื่อโมเดล (ใช้ของ Hugging Face) ---
|
| 9 |
+
model_name = "Arya6967/SAMMed-2d" # ตัวอย่าง
|
| 10 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 11 |
+
model = AutoModelForImageSegmentation.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
# --- โหลดภาพ ---
|
| 14 |
+
img_path = "your_image.png" # เปลี่ยนเป็นภาพร่างกาย/diagram ของคุณ
|
| 15 |
+
img = Image.open(img_path).convert("RGB")
|
| 16 |
+
|
| 17 |
+
# --- ทำ segmentation ---
|
| 18 |
+
inputs = processor(img, return_tensors="pt")
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model(**inputs)
|
| 21 |
+
|
| 22 |
+
# --- แปลง logits -> mask ---
|
| 23 |
+
mask = outputs.logits.argmax(dim=1)[0].cpu().numpy() # mask ของแต่ละ pixel
|
| 24 |
+
# สมมติ organ_id เริ่มจาก 0
|
| 25 |
+
organ_names = [
|
| 26 |
+
"Heart", "Liver", "Lung L", "Lung R", "Stomach", "Kidney L", "Kidney R",
|
| 27 |
+
"Brain", "Pancreas", "Spleen", "Bladder", "Intestine", "Esophagus", "Gallbladder",
|
| 28 |
+
"Appendix", "Thyroid", "Trachea", "Aorta", "Vena Cava", "Pelvis",
|
| 29 |
+
"Femur L", "Femur R", "Humerus L", "Humerus R", "Radius L", "Radius R",
|
| 30 |
+
"Ulna L", "Ulna R", "Clavicle L", "Clavicle R", "Scapula L", "Scapula R",
|
| 31 |
+
"Skull", "Spine", "Ribcage", "Sternum", "Patella L", "Patella R",
|
| 32 |
+
"Tibia L", "Tibia R", "Fibula L", "Fibula R", "Shoulder Joint L", "Shoulder Joint R",
|
| 33 |
+
"Elbow Joint L", "Elbow Joint R", "Knee Joint L", "Knee Joint R", "Ankle Joint L", "Ankle Joint R"
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
# --- ฟังก์ชันคลิก ---
|
| 37 |
+
def show_organ(click_data):
|
| 38 |
+
# click_data: (x, y) pixel
|
| 39 |
+
x = int(click_data[0])
|
| 40 |
+
y = int(click_data[1])
|
| 41 |
+
organ_id = mask[y, x]
|
| 42 |
+
if organ_id < len(organ_names):
|
| 43 |
+
return f"อวัยวะ: {organ_names[organ_id]}"
|
| 44 |
+
else:
|
| 45 |
+
return "ไม่ทราบอวัยวะ"
|
| 46 |
+
|
| 47 |
+
# --- Gradio UI ---
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("## คลิกบนภาพเพื่อดูอวัยวะ")
|
| 50 |
+
img_component = gr.Image(value=np.array(img), interactive=True, tool="select")
|
| 51 |
+
output_text = gr.Textbox(label="ผลลัพธ์")
|
| 52 |
+
img_component.select(show_organ, inputs=img_component, outputs=output_text)
|
| 53 |
+
|
| 54 |
+
demo.launch()
|