-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
207 lines (167 loc) · 7.25 KB
/
main.py
File metadata and controls
207 lines (167 loc) · 7.25 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import cv2
import time
import traceback
from Crash import CrashDetector
from config import CAMERA_ID, WINDOW_NAME, LOG_DIR, SCREENSHOT_DIR, YOLO_INPUT_SIZE
from outputting import DetectionLogger
def initialize_camera(camera_id):
"""Initialize camera with error handling."""
print(f"Initializing camera {camera_id}...")
cap = cv2.VideoCapture(camera_id)
if not cap.isOpened():
print(f"✗ Failed to open camera {camera_id}")
return None
# Get default properties
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)
print(f"✓ Camera initialized: {width}x{height} @ {fps:.1f} FPS")
# Try to set better properties (optional)
try:
cap.set(cv2.CAP_PROP_FPS, 30)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 2) # Reduce buffer for lower latency
except:
pass # Some cameras don't support these settings
return cap
def main():
print("=" * 50)
print("CRASH DETECTION SYSTEM")
print("=" * 50)
# Initialize logger
logger = DetectionLogger()
# Initialize camera
cap = initialize_camera(CAMERA_ID)
if cap is None:
print("Trying camera 0...")
cap = initialize_camera(0)
if cap is None:
print("No camera found. Exiting.")
return
# Get camera properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Initialize detector
try:
detector = CrashDetector()
except Exception as e:
print(f"✗ Failed to initialize detector: {e}")
print("Exiting...")
cap.release()
return
# Start session
session_id = logger.log_start_session()
print(f"Session ID: {session_id}")
# Performance tracking
detect_on = True
fps_timer = time.time()
frame_count = 0
fps_value = 0
frame_number = 0
print("\nControls:")
print(" 'q' - Quit")
print(" 'd' - Toggle detection")
print(" 's' - Save screenshot")
print(" 'p' - Pause/Resume")
print("=" * 50)
print("Starting detection...\n")
paused = False
try:
while True:
if not paused:
# Capture frame
ret, frame = cap.read()
if not ret:
print("✗ Failed to capture frame")
break
frame_count += 1
frame_number += 1
display_frame = frame.copy()
if detect_on:
try:
# Resize for YOLO inference
small_frame = cv2.resize(frame, YOLO_INPUT_SIZE)
# Perform detection
annotated_small = detector.detect(small_frame)
# Scale back for display
display_frame = cv2.resize(annotated_small, (width, height))
# Log detection if available
if detector.last_detection_info:
logger.log_detection(detector.last_detection_info, frame_number)
# Display crash warning if detected
if detector.last_detection_info.get('potential_crash', False):
cv2.putText(display_frame, "⚠️ POTENTIAL CRASH!",
(width//2 - 150, 50), cv2.FONT_HERSHEY_SIMPLEX,
1.5, (0, 0, 255), 3)
except Exception as e:
print(f"⚠️ Detection error: {e}")
# Fallback to raw frame
cv2.putText(display_frame, "DETECTION ERROR",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
else:
cv2.putText(display_frame, "RAW CAMERA",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# Update FPS every second
current_time = time.time()
if current_time - fps_timer >= 1.0:
fps_value = frame_count / (current_time - fps_timer)
frame_count = 0
fps_timer = current_time
# Display info overlay
cv2.putText(display_frame, f"FPS: {fps_value:.1f}",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
mode_text = "DETECT" if detect_on else "RAW"
mode_color = (0, 255, 0) if detect_on else (255, 255, 0)
cv2.putText(display_frame, f"Mode: {mode_text}",
(10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, mode_color, 2)
# Display detection count
cv2.putText(display_frame, f"Detections: {detector.detection_count}",
(10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
if paused:
cv2.putText(display_frame, "PAUSED",
(width//2 - 50, height//2), cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 0, 255), 3)
# Display the frame
cv2.imshow(WINDOW_NAME, display_frame)
# Handle keyboard input
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
print("\nQuitting...")
break
elif key == ord('d'):
detect_on = not detect_on
mode = "Detection: ON" if detect_on else "Detection: OFF"
print(mode)
logger.log_event(mode)
elif key == ord('s'):
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"{SCREENSHOT_DIR}/screenshot_{timestamp}.jpg"
cv2.imwrite(filename, display_frame)
print(f"Screenshot saved: {filename}")
logger.log_event(f"Screenshot saved: {filename}")
elif key == ord('p'):
paused = not paused
state = "PAUSED" if paused else "RESUMED"
print(f"Video {state}")
logger.log_event(f"Video {state}")
except KeyboardInterrupt:
print("\nInterrupted by user")
except Exception as e:
print(f"\n✗ Unexpected error: {e}")
traceback.print_exc()
finally:
# Cleanup
print("\n" + "=" * 50)
print("Cleaning up...")
cap.release()
cv2.destroyAllWindows()
# Log session end
logger.log_end_session()
# Show session summary
stats = logger.get_detection_stats()
print(f"\nSession Summary:")
print(f" Total Detections: {stats['total_detections']}")
print(f" Potential Crashes: {stats['potential_crashes']}")
print(f" Log Directory: {LOG_DIR}")
print("\n✓ Application closed")
if __name__ == "__main__":
main()