Repository navigation
Expand file tree
/
Copy pathTesting_with_classification.py
More file actions
193 lines (157 loc) · 6.71 KB
/
Copy pathTesting_with_classification.py
File metadata and controls
193 lines (157 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import cv2
import os
import csv
from ultralytics import YOLO
def predict_on_single_image(
model_path,
input_image_path,
output_image_path=None,
confidence_threshold=0.25,
):
"""
Performs YOLOv8 object detection on a single input image.
Args:
model_path (str): Path to the YOLOv8 model weights.
input_image_path (str): Path to the input image.
output_image_path (str): Path to save the annotated image (optional).
confidence_threshold (float): Confidence threshold for object detection (default: 0.25).
"""
# Load the YOLOv8 model
model = YOLO(model_path)
# Load the input image
image = cv2.imread(input_image_path)
# Run YOLOv8 inference on the image
results = model(image)
xyxys = []
confidences = []
num_boxes_detected = 0
# Extract the bounding boxes and confidence scores
for result in results:
boxes = result.boxes
confidences = boxes.conf.tolist()
xyxys = boxes.xyxy.tolist()
# Draw bounding boxes with thicker lines
for xyxy, confidence in zip(xyxys, confidences):
xyxy = [int(coord) for coord in xyxy]
if confidence >= confidence_threshold:
cv2.rectangle(
image, (xyxy[0], xyxy[1]), (xyxy[2], xyxy[3]), (255, 255, 255), 2
) # Green color, thicker lines
# Put confidence score in the top-right corner with a smaller font
conf_text = f"{confidence:.2f}"
cv2.putText(
image,
conf_text,
(
xyxy[2] - 30,
xyxy[1] + 12,
), # Adjust the coordinates for positioning
cv2.FONT_HERSHEY_SIMPLEX,
0.4, # Smaller font size
(255, 255, 255), # Green color
1, # Thickness
)
num_boxes_detected += 1
print("Number of boxes detected: ", num_boxes_detected)
if num_boxes_detected >= 1:
detected_class = "Bleeding"
else:
detected_class = "Non-Bleeding"
print("Detected class:", detected_class)
# Save the annotated image
cv2.imwrite(output_image_path, image)
return detected_class
def predict_on_images_in_directory(
model_path, input_dir, output_dir="output_images", confidence_threshold=0.25
):
"""
Performs YOLOv8 object detection on all images in a directory and saves the results in a CSV file.
Args:
model_path (str): Path to the YOLOv8 model weights.
input_dir (str): Directory containing input images.
output_dir (str): Directory to save annotated images (default: "output_images").
confidence_threshold (float): Confidence threshold for object detection (default: 0.25).
"""
# Load the YOLOv8 model
model = YOLO(model_path)
# Create the output directory if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# List all image files in the input directory
image_files = [
f for f in os.listdir(input_dir) if f.endswith(".png") or f.endswith(".jpg")
]
# Create a CSV file to store the results
csv_file_path = os.path.join(output_dir, "classification.csv")
with open(csv_file_path, mode="w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(["Image Name", "Predicted Class Label"])
for image_file in image_files:
# Path to the input image
input_image_path = os.path.join(input_dir, image_file)
# Load the input image
image = cv2.imread(input_image_path)
# Run YOLOv8 inference on the image
results = model(image)
xyxys = []
confidences = []
num_boxes_detected = 0
# Extract the bounding boxes and confidence scores
for result in results:
boxes = result.boxes
confidences = boxes.conf.tolist()
xyxys = boxes.xyxy.tolist()
# Draw bounding boxes with thicker lines
for xyxy, confidence in zip(xyxys, confidences):
xyxy = [int(coord) for coord in xyxy]
if confidence >= confidence_threshold:
cv2.rectangle(
image,
(xyxy[0], xyxy[1]),
(xyxy[2], xyxy[3]),
(255, 255, 255),
2,
)
# Put confidence score in the top-right corner with a smaller font
conf_text = f"{confidence:.2f}"
cv2.putText(
image,
conf_text,
(
xyxy[2] - 30,
xyxy[1] + 12,
), # Adjust the coordinates for positioning
cv2.FONT_HERSHEY_SIMPLEX,
0.4, # Smaller font size
(255, 255, 255), # Green color
1, # Thickness
)
num_boxes_detected += 1
print("Number of boxes detected in", image_file, ":", num_boxes_detected)
# Determine the class based on the number of boxes detected
if num_boxes_detected >= 1:
detected_class = "Bleeding"
else:
detected_class = "Non-Bleeding"
print("Detected class for", image_file, ":", detected_class)
# Remove the ".png" extension from the image name
image_name = os.path.splitext(image_file)[0]
# Write the results to the CSV file
csv_writer.writerow([image_name, detected_class])
# Save the annotated image to the output directory with the same file name
output_image_path = os.path.join(output_dir, image_file)
cv2.imwrite(output_image_path, image)
# Example usage for processing a single image:
model_path = "path/to/your/model_weights.pt"
input_image_path = "path/to/your/input_image.jpg"
output_image_path = "path/to/your/output_image.jpg"
confidence_threshold = 0.25
predict_on_single_image(
model_path, input_image_path, output_image_path, confidence_threshold
)
# Example usage for processing all images in a directory:
model_path = "path/to/your/model_weights.pt"
input_dir = "path/to/your/input_directory"
output_dir = "path/to/your/output_directory"
confidence_threshold = 0.25
predict_on_images_in_directory(model_path, input_dir, output_dir, confidence_threshold)