Update README.md
Browse files
README.md
CHANGED
@@ -23,53 +23,43 @@ import torch
|
|
23 |
from PIL import Image
|
24 |
import pandas as pd
|
25 |
|
26 |
-
# กำหนดพาธไปยังโฟลเดอร์ที่เก็บรูปภาพ
|
27 |
folder_path = ""
|
28 |
-
|
29 |
-
# โหลดโมเดลและโปรเซสเซอร์
|
30 |
processor = AutoImageProcessor.from_pretrained("0llheaven/CON-DETR-V5")
|
31 |
model = AutoModelForObjectDetection.from_pretrained("0llheaven/CON-DETR-V5")
|
32 |
|
33 |
-
# สร้าง DataFrame เพื่อเก็บผลลัพธ์
|
34 |
results_list = []
|
35 |
|
36 |
-
# วนลูปผ่านไฟล์ทั้งหมดในโฟลเดอร์
|
37 |
for image_name in os.listdir(folder_path):
|
38 |
-
if image_name.endswith((".jpg", ".png", ".jpeg")):
|
39 |
image_path = os.path.join(folder_path, image_name)
|
40 |
image = Image.open(image_path)
|
41 |
|
42 |
-
#
|
43 |
if image.mode != "RGB":
|
44 |
image = image.convert("RGB")
|
45 |
|
46 |
-
#
|
47 |
inputs = processor(images=image, return_tensors="pt")
|
48 |
outputs = model(**inputs)
|
49 |
|
50 |
-
# กรองการทำนายที่มีความแม่นยำมากกว่า 0.9
|
51 |
target_sizes = torch.tensor([image.size[::-1]])
|
52 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
# ตรวจสอบว่ามีการตรวจจับหรือไม่
|
57 |
detected_any = False
|
58 |
-
|
59 |
for result in results:
|
60 |
scores = result["scores"]
|
61 |
labels = result["labels"]
|
62 |
boxes = result["boxes"]
|
63 |
|
64 |
-
# กรองเฉพาะ 2 กล่องแรกที่มีคะแนนสูงกว่า 0.5
|
65 |
filtered_data = [(score, label, box) for score, label, box in zip(scores, labels, boxes) if score > 0.5][:2] # จำกัดให้เก็บได้ไม่เกิน 2 กล่อง
|
66 |
# for score, label, box in zip(scores, labels, boxes):
|
67 |
-
# if score > 0.5:
|
68 |
-
if len(filtered_data) > 0:
|
69 |
detected_any = True
|
70 |
|
71 |
for score, label, box in filtered_data:
|
72 |
-
# กำหนดชื่อ label ตาม index ของ label ที่ได้
|
73 |
if label.item() == 0:
|
74 |
label_name = "Pneumonia"
|
75 |
elif label.item() == 1:
|
@@ -80,7 +70,7 @@ for image_name in os.listdir(folder_path):
|
|
80 |
label_name = "Pneumonia_virus"
|
81 |
|
82 |
xmin, ymin, xmax, ymax = [round(i, 2) for i in box.tolist()]
|
83 |
-
print(f" - Detected {label_name} with score {round(score.item(), 3)} at {xmin, ymin, xmax, ymax}")
|
84 |
results_list.append({
|
85 |
"image_name": image_name,
|
86 |
"label": label_name,
|
@@ -103,11 +93,7 @@ for image_name in os.listdir(folder_path):
|
|
103 |
"score": 0,
|
104 |
})
|
105 |
|
106 |
-
# แปลง List เป็น DataFrame
|
107 |
results_df = pd.DataFrame(results_list)
|
108 |
-
|
109 |
-
# แสดงผล DataFrame
|
110 |
print("\nFinal results:")
|
111 |
-
|
112 |
results_df.to_csv("testmodel.csv", index=False)
|
113 |
```
|
|
|
23 |
from PIL import Image
|
24 |
import pandas as pd
|
25 |
|
|
|
26 |
folder_path = ""
|
|
|
|
|
27 |
processor = AutoImageProcessor.from_pretrained("0llheaven/CON-DETR-V5")
|
28 |
model = AutoModelForObjectDetection.from_pretrained("0llheaven/CON-DETR-V5")
|
29 |
|
|
|
30 |
results_list = []
|
31 |
|
|
|
32 |
for image_name in os.listdir(folder_path):
|
33 |
+
if image_name.endswith((".jpg", ".png", ".jpeg")):
|
34 |
image_path = os.path.join(folder_path, image_name)
|
35 |
image = Image.open(image_path)
|
36 |
|
37 |
+
# RGB To grayscale
|
38 |
if image.mode != "RGB":
|
39 |
image = image.convert("RGB")
|
40 |
|
41 |
+
# prediction
|
42 |
inputs = processor(images=image, return_tensors="pt")
|
43 |
outputs = model(**inputs)
|
44 |
|
|
|
45 |
target_sizes = torch.tensor([image.size[::-1]])
|
46 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
|
47 |
+
print(f"Processing image: {image_name}")
|
48 |
|
49 |
+
# detect_box
|
|
|
|
|
50 |
detected_any = False
|
|
|
51 |
for result in results:
|
52 |
scores = result["scores"]
|
53 |
labels = result["labels"]
|
54 |
boxes = result["boxes"]
|
55 |
|
|
|
56 |
filtered_data = [(score, label, box) for score, label, box in zip(scores, labels, boxes) if score > 0.5][:2] # จำกัดให้เก็บได้ไม่เกิน 2 กล่อง
|
57 |
# for score, label, box in zip(scores, labels, boxes):
|
58 |
+
# if score > 0.5:
|
59 |
+
if len(filtered_data) > 0:
|
60 |
detected_any = True
|
61 |
|
62 |
for score, label, box in filtered_data:
|
|
|
63 |
if label.item() == 0:
|
64 |
label_name = "Pneumonia"
|
65 |
elif label.item() == 1:
|
|
|
70 |
label_name = "Pneumonia_virus"
|
71 |
|
72 |
xmin, ymin, xmax, ymax = [round(i, 2) for i in box.tolist()]
|
73 |
+
print(f" - Detected {label_name} with score {round(score.item(), 3)} at {xmin, ymin, xmax, ymax}")
|
74 |
results_list.append({
|
75 |
"image_name": image_name,
|
76 |
"label": label_name,
|
|
|
93 |
"score": 0,
|
94 |
})
|
95 |
|
|
|
96 |
results_df = pd.DataFrame(results_list)
|
|
|
|
|
97 |
print("\nFinal results:")
|
|
|
98 |
results_df.to_csv("testmodel.csv", index=False)
|
99 |
```
|