|
|
|
|
|
""" |
|
|
แอปจำแนกอวัยวะ / ส่วนของผิวหนัง |
|
|
โดยใช้โมเดลที่เทรนมาแล้วจาก Hugging Face |
|
|
""" |
|
|
|
|
|
from transformers import AutoImageProcessor, AutoModelForImageClassification |
|
|
import torch |
|
|
from PIL import Image |
|
|
import requests |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_name = "histai/SPIDER-skin-model" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("กำลังโหลดโมเดลและตัวประมวลผล...") |
|
|
processor = AutoImageProcessor.from_pretrained(model_name) |
|
|
model = AutoModelForImageClassification.from_pretrained(model_name) |
|
|
print("โหลดโมเดลเรียบร้อย!") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
organ_descriptions = { |
|
|
0: "ผิวหนังปกติ", |
|
|
1: "แผลผิวหนัง", |
|
|
2: "ตุ่มน้ำ / ฟองอากาศ", |
|
|
3: "จุดเลือดออกใต้ผิวหนัง", |
|
|
4: "รอยไหม้", |
|
|
5: "การอักเสบ", |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers_logo.png" |
|
|
image = Image.open(requests.get(url, stream=True).raw) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with torch.no_grad(): |
|
|
logits = model(**inputs).logits |
|
|
predicted_class_idx = logits.argmax(-1).item() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
description = organ_descriptions.get(predicted_class_idx, "ไม่ทราบอวัยวะ / สภาพผิว") |
|
|
print("------ ผลลัพธ์ ------") |
|
|
print(f"รหัสคลาสที่ทำนาย: {predicted_class_idx}") |
|
|
print(f"คำอธิบาย: {description}") |