-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_tester.py
216 lines (175 loc) · 7.68 KB
/
cli_tester.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import cv2
import numpy as np
from ultralytics import YOLO
import logging
class ObjectDetectionTester:
def __init__(self, model_path):
"""
Initialize the object detection tester with a trained YOLO model
Args:
model_path (str): Path to the trained YOLO model
"""
self.model = YOLO(model_path)
self.colors = self._generate_colors() # Generate unique colors for each vehicle class
def _generate_colors(self):
"""
Generate unique colors for different object classes
Returns:
dict: A dictionary mapping class indices to unique BGR colors
"""
np.random.seed(42)
return {i: tuple(np.random.randint(0, 255, 3).tolist())
for i in range(len(self.model.names))}
def _create_log_file(self, input_path, detections):
"""
Create a log file with detection details
Args:
input_path (str): Path to input image/video
detections (list): List of detected objects
"""
base_name = os.path.splitext(os.path.basename(input_path))[0]
is_video = input_path.lower().endswith(('.mp4', '.avi', '.mov', '.mkv'))
log_name = f"{base_name}{'_Video' if is_video else '_Gorsel'}.log"
with open(log_name, 'w') as log_file:
for det in detections:
bbox = det['bbox']
conf = det['confidence']
log_entry = f"{det['class']} {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]} {conf:.2f}"
if is_video:
log_entry += f" {det['frame']}"
log_file.write(log_entry + "\n")
print(f"Log file saved to {log_name}")
def detect_and_visualize(self, input_path):
"""
Detect objects in an image or video and visualize results
Args:
input_path (str): Path to input image or video file
"""
if input_path.lower().endswith(('.png', '.jpg', '.jpeg')):
self._process_image(input_path)
elif input_path.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
self._process_video(input_path)
else:
raise ValueError("Unsupported file type")
def _process_image(self, image_path):
"""Process a single image for object detection"""
results = self.model(image_path, conf=0.4, iou=0.5, verbose=False)
# Get inference time from results
inference_time = results[0].speed['inference'] # Inference time in ms
print(f"\nImage processed in {inference_time:.1f}ms")
image = cv2.imread(image_path)
log_detections = []
for result in results:
boxes = result.boxes
for box in boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
cls = int(box.cls[0])
conf = float(box.conf[0])
center_x = (x1 + x2) / 2 / image.shape[1]
center_y = (y1 + y2) / 2 / image.shape[0]
width = (x2 - x1) / image.shape[1]
height = (y2 - y1) / image.shape[0]
color = self.colors[cls]
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
label = f"{self.model.names[cls]} {conf:.2f}"
cv2.putText(image, label, (x1, y1-10),
cv2.FONT_HERSHEY_DUPLEX, 0.9, color, 2)
log_detections.append({
'class': cls,
'bbox': [center_x, center_y, width, height],
'confidence': conf
})
output_path = f"{os.path.splitext(image_path)[0]}_detected.png"
cv2.imwrite(output_path, image)
self._create_log_file(image_path, log_detections)
print(f"Results saved to {output_path}")
def _process_video(self, video_path):
"""Process a video for object detection"""
cap = cv2.VideoCapture(video_path)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
output_path = f"{os.path.splitext(video_path)[0]}_detected.mp4"
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
all_log_detections = []
total_inference = 0.0
frame_number = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
results = self.model(frame, conf=0.30, iou=0.5, verbose=False)
# Accumulate inference time
frame_inference = results[0].speed['inference']
total_inference += frame_inference
frame_number += 1
frame_detections = []
for result in results:
boxes = result.boxes
for box in boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
cls = int(box.cls[0])
conf = float(box.conf[0])
center_x = (x1 + x2) / 2 / frame.shape[1]
center_y = (y1 + y2) / 2 / frame.shape[0]
width_dim = (x2 - x1) / frame.shape[1]
height_dim = (y2 - y1) / frame.shape[0]
color = self.colors[cls]
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
label = f"{self.model.names[cls]} {conf:.2f}"
cv2.putText(frame, label, (x1, y1-10),
cv2.FONT_HERSHEY_DUPLEX, 0.9, color, 2)
frame_detections.append({
'class': cls,
'bbox': [center_x, center_y, width_dim, height_dim],
'confidence': conf
})
out.write(frame)
all_log_detections.extend([{**det, 'frame': frame_number}
for det in frame_detections])
# Calculate and display average inference time
if frame_number > 0:
avg_inference = total_inference / frame_number
print(f"\nVideo processing complete")
print(f"Frames processed: {frame_number}")
print(f"Average inference time per frame: {avg_inference:.1f}ms")
cap.release()
out.release()
self._create_log_file(video_path, all_log_detections)
print(f"Results saved to {output_path}")
def main():
model_path = "YOLOv8_Model.pt"
detector = ObjectDetectionTester(model_path)
while True:
print("\nObject Detection CLI")
print("1. Process Image")
print("2. Process Video")
print("3. Exit")
choice = input("Enter your choice (1-3): ").strip()
if choice == '3':
print("Exiting program...")
break
if choice not in ('1', '2'):
print("Invalid choice. Please try again.")
continue
file_path = input("Enter file path: ").strip()
if not os.path.exists(file_path):
print("Error: File not found")
continue
try:
if choice == '1':
if not file_path.lower().endswith(('.png', '.jpg', '.jpeg')):
print("Error: Invalid image format")
continue
else:
if not file_path.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
print("Error: Invalid video format")
continue
print("Processing file...")
detector.detect_and_visualize(file_path)
except Exception as e:
print(f"Error processing file: {str(e)}")
if __name__ == "__main__":
main()