Update README.md
Browse files
README.md
CHANGED
@@ -79,4 +79,87 @@ plt.axis('off')
|
|
79 |
plt.show()
|
80 |
|
81 |
print(labels)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
```
|
|
|
79 |
plt.show()
|
80 |
|
81 |
print(labels)
|
82 |
+
```
|
83 |
+
## Predic and save to csv
|
84 |
+
```python
|
85 |
+
import os
|
86 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
87 |
+
import torch
|
88 |
+
from PIL import Image
|
89 |
+
import pandas as pd
|
90 |
+
|
91 |
+
# กำหนดพาธไปยังโฟลเดอร์ที่เก็บรูปภาพ
|
92 |
+
folder_path = "../data/data_em_100img/"
|
93 |
+
|
94 |
+
# โหลดโมเดลและโปรเซสเซอร์
|
95 |
+
processor = AutoImageProcessor.from_pretrained("0llheaven/detr-finetuned-V2")
|
96 |
+
model = AutoModelForObjectDetection.from_pretrained("0llheaven/detr-finetuned-V2")
|
97 |
+
|
98 |
+
# สร้าง DataFrame เพื่อเก็บผลลัพธ์
|
99 |
+
results_list = []
|
100 |
+
|
101 |
+
# วนลูปผ่านไฟล์ทั้งหมดในโฟลเดอร์
|
102 |
+
for image_name in os.listdir(folder_path):
|
103 |
+
if image_name.endswith((".jpg", ".png", ".jpeg")): # กรองเฉพาะไฟล์รูปภาพ
|
104 |
+
image_path = os.path.join(folder_path, image_name)
|
105 |
+
image = Image.open(image_path)
|
106 |
+
|
107 |
+
# แปลงรูปภาพเป็น RGB หากเป็น grayscale
|
108 |
+
if image.mode != "RGB":
|
109 |
+
image = image.convert("RGB")
|
110 |
+
|
111 |
+
# ทำนายผล
|
112 |
+
inputs = processor(images=image, return_tensors="pt")
|
113 |
+
outputs = model(**inputs)
|
114 |
+
|
115 |
+
# กรองการทำนายที่มีความแม่นยำมากกว่า 0.9
|
116 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
117 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
|
118 |
+
|
119 |
+
print(f"Processing image: {image_name}") # แสดงชื่อรูปภาพที่กำลังประมวลผล
|
120 |
+
|
121 |
+
# ตรวจสอบว่ามีการตรวจจับหรือไม่
|
122 |
+
detected_any = False
|
123 |
+
|
124 |
+
for result in results:
|
125 |
+
scores = result["scores"]
|
126 |
+
labels = result["labels"]
|
127 |
+
boxes = result["boxes"]
|
128 |
+
|
129 |
+
for score, label, box in zip(scores, labels, boxes):
|
130 |
+
if score > 0.9: # กรองเฉพาะผลลัพธ์ที่มีความแม่นยำมากกว่า 0.9
|
131 |
+
detected_any = True
|
132 |
+
xmin, ymin, xmax, ymax = [round(i, 2) for i in box.tolist()]
|
133 |
+
print(f" - Detected {label} with score {round(score.item(), 3)} at {xmin, ymin, xmax, ymax}") # แสดงข้อมูลที่ทำนายได้
|
134 |
+
results_list.append({
|
135 |
+
"image_name": image_name,
|
136 |
+
"score": round(score.item(), 3),
|
137 |
+
"label": label,
|
138 |
+
"xmin": xmin,
|
139 |
+
"ymin": ymin,
|
140 |
+
"xmax": xmax,
|
141 |
+
"ymax": ymax
|
142 |
+
})
|
143 |
+
|
144 |
+
if not detected_any:
|
145 |
+
print(" - No Detect")
|
146 |
+
results_list.append({
|
147 |
+
"image_name": image_name,
|
148 |
+
"score": "-",
|
149 |
+
"label": "No Detect",
|
150 |
+
"xmin": "-",
|
151 |
+
"ymin": "-",
|
152 |
+
"xmax": "-",
|
153 |
+
"ymax": "-"
|
154 |
+
})
|
155 |
+
|
156 |
+
# แปลง List เป็น DataFrame
|
157 |
+
results_df = pd.DataFrame(results_list)
|
158 |
+
|
159 |
+
# แสดงผล DataFrame
|
160 |
+
print("\nFinal results:")
|
161 |
+
# print(results_df)
|
162 |
+
|
163 |
+
# บันทึก DataFrame เป็นไฟล์ CSV
|
164 |
+
results_df.to_csv("../results_em.csv", index=False)
|
165 |
```
|