Downloads · 30 days
3
0% of all-time downloads
0llheaven/detr-finetuned-V2
detr-finetuned-V2 is a object detection model from 0llheaven. Use it when you need objects located in an image. It is set up for transformers.
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
Downloads · 30 days
3
0% of all-time downloads
All-time downloads
738
Public
Parameters
41.6M
167 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors167 MB · 100%
From the Hugging Face model README
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
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)
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)