Edit model card

Model Card for Model ID

Model Details

Model Description

This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.

  • Developed by: [More Information Needed]
  • Funded by [optional]: [More Information Needed]
  • Shared by [optional]: [More Information Needed]
  • Model type: [More Information Needed]
  • Language(s) (NLP): [More Information Needed]
  • License: [More Information Needed]
  • Finetuned from model [optional]: [More Information Needed]

Model Sources [optional]

  • Repository: [More Information Needed]
  • Paper [optional]: [More Information Needed]
  • Demo [optional]: [More Information Needed]

Uses

from transformers import AutoImageProcessor, AutoModelForObjectDetection
import torch
from PIL import Image, ImageDraw
import matplotlib.pyplot as pltz

# เปิดรูปภาพจากพาธในเครื่อง
url = "../../Pnephase2_Project/data/chest_xray/OTHER_ALL/00000020_001.png"
image = Image.open(url)

# แปลงรูปภาพเป็น RGB หากเป็น grayscale
if image.mode != "RGB":
    image = image.convert("RGB")

processor = AutoImageProcessor.from_pretrained("0llheaven/detr-finetuned-V2")
model = AutoModelForObjectDetection.from_pretrained("0llheaven/detr-finetuned-V2")

inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)

# กรองการทำนายที่มีความแม่นยำมากกว่า 0.9
target_sizes = torch.tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)

# print(results)
# วาดกรอบรอบวัตถุที่ตรวจพบในภาพ
draw = ImageDraw.Draw(image)
for result in results:
    scores = result["scores"]
    labels = result["labels"]
    boxes = result["boxes"]

    for score, label, box in zip(scores, labels, boxes):
        if score > 0.9:  # กรองเฉพาะผลลัพธ์ที่มีความแม่นยำมากกว่า 0.9
            box = [round(i, 2) for i in box.tolist()]
            draw.rectangle(box, outline="red", width=3)
            draw.text((box[0], box[1]), f"Pnuemonia: {round(score.item(), 3)}", fill="red")  # เปลี่ยน label เป็น "Pnue"

# แสดงผลภาพ
plt.figure(figsize=(10, 10))
plt.imshow(image)
plt.axis('off')
plt.show()

print(labels)

Predic and save to csv

import os
from transformers import AutoImageProcessor, AutoModelForObjectDetection
import torch
from PIL import Image
import pandas as pd

# กำหนดพาธไปยังโฟลเดอร์ที่เก็บรูปภาพ
folder_path = "../data/data_em_100img/"

# โหลดโมเดลและโปรเซสเซอร์
processor = AutoImageProcessor.from_pretrained("0llheaven/detr-finetuned-V2")
model = AutoModelForObjectDetection.from_pretrained("0llheaven/detr-finetuned-V2")

# สร้าง DataFrame เพื่อเก็บผลลัพธ์
results_list = []

# วนลูปผ่านไฟล์ทั้งหมดในโฟลเดอร์
for image_name in os.listdir(folder_path):
    if image_name.endswith((".jpg", ".png", ".jpeg")):  # กรองเฉพาะไฟล์รูปภาพ
        image_path = os.path.join(folder_path, image_name)
        image = Image.open(image_path)

        # แปลงรูปภาพเป็น RGB หากเป็น grayscale
        if image.mode != "RGB":
            image = image.convert("RGB")

        # ทำนายผล
        inputs = processor(images=image, return_tensors="pt")
        outputs = model(**inputs)

        # กรองการทำนายที่มีความแม่นยำมากกว่า 0.9
        target_sizes = torch.tensor([image.size[::-1]])
        results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)

        print(f"Processing image: {image_name}")  # แสดงชื่อรูปภาพที่กำลังประมวลผล

        # ตรวจสอบว่ามีการตรวจจับหรือไม่
        detected_any = False

        for result in results:
            scores = result["scores"]
            labels = result["labels"]
            boxes = result["boxes"]

            for score, label, box in zip(scores, labels, boxes):
                if score > 0.9:  # กรองเฉพาะผลลัพธ์ที่มีความแม่นยำมากกว่า 0.9
                    detected_any = True
                    xmin, ymin, xmax, ymax = [round(i, 2) for i in box.tolist()]
                    print(f" - Detected {label} with score {round(score.item(), 3)} at {xmin, ymin, xmax, ymax}")  # แสดงข้อมูลที่ทำนายได้
                    results_list.append({
                        "image_name": image_name,
                        "score": round(score.item(), 3),
                        "label": label,
                        "xmin": xmin,
                        "ymin": ymin,
                        "xmax": xmax,
                        "ymax": ymax
                    })

        if not detected_any:
            print(" - No Detect")
            results_list.append({
                "image_name": image_name,
                "score": "-",
                "label": "No Detect",
                "xmin": "-",
                "ymin": "-",
                "xmax": "-",
                "ymax": "-"
            })

# แปลง List เป็น DataFrame
results_df = pd.DataFrame(results_list)

# แสดงผล DataFrame
print("\nFinal results:")
# print(results_df)

# บันทึก DataFrame เป็นไฟล์ CSV
results_df.to_csv("../results_em.csv", index=False)
Downloads last month
35
Safetensors
Model size
41.6M params
Tensor type
F32
·
Inference Examples
Inference API (serverless) is not available, repository is disabled.