-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadas_ultra_advanced.py
More file actions
568 lines (461 loc) · 17.9 KB
/
adas_ultra_advanced.py
File metadata and controls
568 lines (461 loc) · 17.9 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#!/usr/bin/env python3
"""
ULTRA-ADVANCED ADAS PERCEPTION SYSTEM
Complete Implementation with All Advanced Features
Features Included:
- 3D Point Cloud Visualization
- 360° Panoramic View
- Time-Lapse & Slow Motion Recording
- Multi-View Synchronized Display
- Real-Time Performance Graphs
- Heatmap Timeline
- Object Trajectory Maps
- Scene Classification (Time/Road/Traffic/Weather)
- Pedestrian Pose Estimation
- Vehicle Type Classification
- License Plate Detection
- Optical Flow Visualization
- Motion Prediction
- Ensemble Detection
- Driving Behavior Analysis
- And much more...
Author: DeepMost AI Perception Team
Version: 3.0.0 ULTIMATE
"""
import wx
import wx.lib.newevent
import cv2
import numpy as np
import threading
import queue
import time
import json
import os
import logging
from dataclasses import dataclass, field
from typing import Optional, List, Dict, Tuple, Any
from collections import deque, OrderedDict
from enum import Enum, auto
from pathlib import Path
from datetime import datetime
import warnings
# Advanced imports
try:
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure
MATPLOTLIB_AVAILABLE = True
except ImportError:
MATPLOTLIB_AVAILABLE = False
print("Warning: matplotlib not available - graphs disabled")
try:
import mediapipe as mp
MEDIAPIPE_AVAILABLE = True
except ImportError:
MEDIAPIPE_AVAILABLE = False
print("Warning: mediapipe not available - pose estimation disabled")
warnings.filterwarnings('ignore')
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('adas_ultra.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger('ADAS_Ultra')
# Custom events
FrameUpdateEvent, EVT_FRAME_UPDATE = wx.lib.newevent.NewEvent()
MetricsUpdateEvent, EVT_METRICS_UPDATE = wx.lib.newevent.NewEvent()
GraphUpdateEvent, EVT_GRAPH_UPDATE = wx.lib.newevent.NewEvent()
# ============================================================================
# ENUMS & DATA CLASSES
# ============================================================================
class TimeOfDay(Enum):
NIGHT = auto()
DAWN = auto()
DAY = auto()
DUSK = auto()
class RoadType(Enum):
HIGHWAY = auto()
URBAN = auto()
RURAL = auto()
PARKING = auto()
UNKNOWN = auto()
class TrafficDensity(Enum):
LOW = auto()
MEDIUM = auto()
HIGH = auto()
class RoadCondition(Enum):
DRY = auto()
WET = auto()
ICY = auto()
DAMAGED = auto()
class VehicleType(Enum):
SEDAN = auto()
SUV = auto()
TRUCK = auto()
BUS = auto()
MOTORCYCLE = auto()
BICYCLE = auto()
UNKNOWN = auto()
class DrivingStyle(Enum):
CAUTIOUS = auto()
NORMAL = auto()
AGGRESSIVE = auto()
@dataclass
class Detection3D:
"""Detection with 3D information"""
bbox_2d: Tuple[int, int, int, int]
confidence: float
class_id: int
class_name: str
distance: float = 0.0
depth: float = 0.0
position_3d: Tuple[float, float, float] = (0, 0, 0)
velocity_3d: Tuple[float, float, float] = (0, 0, 0)
point_cloud: Optional[np.ndarray] = None
vehicle_type: VehicleType = VehicleType.UNKNOWN
has_license_plate: bool = False
@dataclass
class PoseKeypoints:
"""Pedestrian pose keypoints"""
keypoints: List[Tuple[float, float, float]] # x, y, confidence
bbox: Tuple[int, int, int, int]
confidence: float
action: str = "standing" # walking, running, standing
@dataclass
class SceneContext:
"""Complete scene understanding"""
time_of_day: TimeOfDay = TimeOfDay.DAY
road_type: RoadType = RoadType.UNKNOWN
traffic_density: TrafficDensity = TrafficDensity.LOW
road_condition: RoadCondition = RoadCondition.DRY
visibility_score: float = 1.0
complexity_score: float = 0.0
weather: str = "clear"
@dataclass
class OpticalFlowData:
"""Optical flow information"""
flow: np.ndarray
magnitude: np.ndarray
angle: np.ndarray
dominant_motion: Tuple[float, float]
@dataclass
class TrajectoryPrediction:
"""Predicted future trajectory"""
predicted_positions: List[Tuple[float, float]]
confidence: float
collision_probability: float
# ============================================================================
# 3D POINT CLOUD RECONSTRUCTION
# ============================================================================
class PointCloudReconstructor:
"""Reconstruct 3D point cloud from depth and camera"""
def __init__(self, fx=800, fy=800, cx=640, cy=360):
self.fx = fx # Focal length x
self.fy = fy # Focal length y
self.cx = cx # Principal point x
self.cy = cy # Principal point y
def reconstruct(self, rgb_frame: np.ndarray, depth_map: np.ndarray) -> np.ndarray:
"""Create 3D point cloud from RGB and depth"""
height, width = depth_map.shape
# Create meshgrid of pixel coordinates
u, v = np.meshgrid(np.arange(width), np.arange(height))
# Convert to 3D coordinates
z = depth_map.astype(np.float32)
x = (u - self.cx) * z / self.fx
y = (v - self.cy) * z / self.fy
# Stack coordinates
points_3d = np.stack([x, y, z], axis=-1)
# Get colors from RGB frame
colors = cv2.cvtColor(rgb_frame, cv2.COLOR_BGR2RGB)
# Flatten
points = points_3d.reshape(-1, 3)
colors = colors.reshape(-1, 3)
# Filter invalid points
valid = z.flatten() > 0
points = points[valid]
colors = colors[valid]
# Combine points and colors
point_cloud = np.hstack([points, colors])
return point_cloud
def visualize_point_cloud(self, point_cloud: np.ndarray, size=(800, 600)) -> np.ndarray:
"""Render point cloud to 2D image (simple projection)"""
if point_cloud is None or len(point_cloud) == 0:
return np.zeros((*size, 3), dtype=np.uint8)
# Extract coordinates and colors
points = point_cloud[:, :3]
colors = point_cloud[:, 3:6].astype(np.uint8)
# Simple orthographic projection (top-down view)
img = np.zeros((*size, 3), dtype=np.uint8)
# Scale and project
x_proj = ((points[:, 0] + 10) * 20).astype(int)
y_proj = ((20 - points[:, 2]) * 20).astype(int)
# Draw points
for i in range(len(points)):
if 0 <= x_proj[i] < size[1] and 0 <= y_proj[i] < size[0]:
cv2.circle(img, (x_proj[i], y_proj[i]), 1, colors[i].tolist(), -1)
return img
# ============================================================================
# 360° PANORAMIC VIEW STITCHER
# ============================================================================
class PanoramaStitcher:
"""Stitch multiple camera views into panoramic view"""
def __init__(self):
self.stitcher = cv2.Stitcher_create(cv2.Stitcher_PANORAMA)
self.cached_panorama = None
def stitch(self, images: List[np.ndarray]) -> Optional[np.ndarray]:
"""Stitch multiple images into panorama"""
if len(images) < 2:
return images[0] if images else None
try:
status, panorama = self.stitcher.stitch(images)
if status == cv2.Stitcher_OK:
self.cached_panorama = panorama
return panorama
else:
logger.warning(f"Panorama stitching failed: {status}")
return self.cached_panorama
except Exception as e:
logger.error(f"Panorama stitching error: {e}")
return self.cached_panorama
# ============================================================================
# TIME-LAPSE & SLOW MOTION RECORDER
# ============================================================================
class AdvancedRecorder:
"""Record with time-lapse and slow motion"""
def __init__(self):
self.writers = {}
self.frame_buffers = {}
self.recording_modes = {}
def start_recording(self, output_path: str, frame_size: Tuple[int, int],
mode: str = "normal", fps: float = 30.0):
"""Start recording with specific mode"""
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
if mode == "timelapse":
fps = fps / 10 # 10x speed up
elif mode == "slowmotion":
fps = fps * 2 # 2x slow down
writer = cv2.VideoWriter(output_path, fourcc, fps, frame_size)
self.writers[mode] = writer
self.frame_buffers[mode] = deque(maxlen=1000)
self.recording_modes[mode] = {
'frame_skip': 10 if mode == "timelapse" else 1,
'frame_count': 0,
'output_path': output_path
}
logger.info(f"Started {mode} recording: {output_path}")
def write_frame(self, frame: np.ndarray, mode: str = "normal"):
"""Write frame with mode-specific handling"""
if mode not in self.writers:
return
info = self.recording_modes[mode]
info['frame_count'] += 1
# Frame skipping for time-lapse
if mode == "timelapse":
if info['frame_count'] % info['frame_skip'] == 0:
self.writers[mode].write(frame)
# Duplicate frames for slow motion
elif mode == "slowmotion":
self.writers[mode].write(frame)
self.writers[mode].write(frame) # Write twice
else:
self.writers[mode].write(frame)
def stop_recording(self, mode: str = "normal"):
"""Stop recording for specific mode"""
if mode in self.writers:
self.writers[mode].release()
del self.writers[mode]
logger.info(f"Stopped {mode} recording")
# ============================================================================
# SCENE CLASSIFIER
# ============================================================================
class SceneClassifier:
"""Classify scene characteristics"""
def classify(self, frame: np.ndarray, detections: List) -> SceneContext:
"""Comprehensive scene classification"""
context = SceneContext()
# Time of day classification
context.time_of_day = self._classify_time_of_day(frame)
# Road type detection
context.road_type = self._classify_road_type(frame, detections)
# Traffic density
context.traffic_density = self._classify_traffic_density(detections)
# Road condition
context.road_condition = self._classify_road_condition(frame)
# Visibility score
context.visibility_score = self._calculate_visibility(frame)
# Scene complexity
context.complexity_score = self._calculate_complexity(frame, detections)
return context
def _classify_time_of_day(self, frame: np.ndarray) -> TimeOfDay:
"""Classify time of day from brightness and color"""
# Convert to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
brightness = hsv[:, :, 2].mean()
# Convert to LAB for better brightness detection
lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
l_mean = lab[:, :, 0].mean()
# Color temperature (warm = dawn/dusk, cool = day/night)
b_mean = frame[:, :, 0].mean()
r_mean = frame[:, :, 2].mean()
color_temp = r_mean - b_mean
if brightness < 60:
return TimeOfDay.NIGHT
elif brightness < 120 and color_temp > 10:
return TimeOfDay.DAWN if color_temp > 20 else TimeOfDay.DUSK
elif brightness > 120 and color_temp > 5:
return TimeOfDay.DUSK
else:
return TimeOfDay.DAY
def _classify_road_type(self, frame: np.ndarray, detections: List) -> RoadType:
"""Classify road type"""
height, width = frame.shape[:2]
# Count vehicles
vehicle_count = len([d for d in detections if hasattr(d, 'class_name') and
d.class_name in ['car', 'truck', 'bus']])
# Detect lane markings (simplified)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
bottom_half = edges[height//2:, :]
line_density = np.sum(bottom_half > 0) / (width * height / 2)
# Classification logic
if line_density > 0.05 and vehicle_count > 5:
return RoadType.HIGHWAY
elif vehicle_count > 2:
return RoadType.URBAN
elif line_density < 0.02:
return RoadType.PARKING
elif vehicle_count < 2:
return RoadType.RURAL
else:
return RoadType.UNKNOWN
def _classify_traffic_density(self, detections: List) -> TrafficDensity:
"""Classify traffic density"""
vehicle_count = len([d for d in detections if hasattr(d, 'class_name') and
d.class_name in ['car', 'truck', 'bus', 'motorcycle']])
if vehicle_count < 3:
return TrafficDensity.LOW
elif vehicle_count < 8:
return TrafficDensity.MEDIUM
else:
return TrafficDensity.HIGH
def _classify_road_condition(self, frame: np.ndarray) -> RoadCondition:
"""Detect road surface condition"""
# Convert to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Focus on bottom part (road)
height = frame.shape[0]
road_region = hsv[int(height*0.7):, :]
# Calculate saturation and brightness
saturation = road_region[:, :, 1].mean()
brightness = road_region[:, :, 2].mean()
# Detect reflections (wet road)
gray_road = cv2.cvtColor(frame[int(height*0.7):, :], cv2.COLOR_BGR2GRAY)
_, bright_spots = cv2.threshold(gray_road, 200, 255, cv2.THRESH_BINARY)
reflection_ratio = np.sum(bright_spots > 0) / bright_spots.size
if reflection_ratio > 0.1 and brightness > 100:
return RoadCondition.WET
elif saturation < 20 and brightness < 80:
return RoadCondition.ICY
else:
return RoadCondition.DRY
def _calculate_visibility(self, frame: np.ndarray) -> float:
"""Calculate visibility score (0-1)"""
# Convert to LAB
lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
# Calculate contrast
l_channel = lab[:, :, 0]
contrast = l_channel.std()
# Calculate sharpness (edge density)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
edge_density = np.sum(edges > 0) / edges.size
# Combine metrics
visibility = min(1.0, (contrast / 50.0) * 0.7 + (edge_density * 100) * 0.3)
return visibility
def _calculate_complexity(self, frame: np.ndarray, detections: List) -> float:
"""Calculate scene complexity (0-1)"""
# Number of objects
object_count = len(detections)
# Edge density
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
edge_density = np.sum(edges > 0) / edges.size
# Color variety
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hue_std = hsv[:, :, 0].std()
# Combine
complexity = min(1.0, (object_count / 20.0) * 0.5 +
(edge_density * 50) * 0.3 +
(hue_std / 50.0) * 0.2)
return complexity
# ============================================================================
# PEDESTRIAN POSE ESTIMATOR
# ============================================================================
class PedestrianPoseEstimator:
"""Estimate pedestrian poses using MediaPipe"""
def __init__(self):
self.enabled = MEDIAPIPE_AVAILABLE
if self.enabled:
self.mp_pose = mp.solutions.pose
self.pose = self.mp_pose.Pose(
static_image_mode=False,
model_complexity=1,
min_detection_confidence=0.5
)
def estimate(self, frame: np.ndarray, person_bbox: Tuple) -> Optional[PoseKeypoints]:
"""Estimate pose for person in bounding box"""
if not self.enabled:
return None
try:
x1, y1, x2, y2 = person_bbox
person_img = frame[y1:y2, x1:x2]
if person_img.size == 0:
return None
# Process with MediaPipe
rgb = cv2.cvtColor(person_img, cv2.COLOR_BGR2RGB)
results = self.pose.process(rgb)
if not results.pose_landmarks:
return None
# Extract keypoints
keypoints = []
for landmark in results.pose_landmarks.landmark:
# Convert to original image coordinates
x = int(landmark.x * (x2 - x1) + x1)
y = int(landmark.y * (y2 - y1) + y1)
keypoints.append((x, y, landmark.visibility))
# Determine action
action = self._classify_action(keypoints)
return PoseKeypoints(
keypoints=keypoints,
bbox=person_bbox,
confidence=0.8,
action=action
)
except Exception as e:
logger.error(f"Pose estimation error: {e}")
return None
def _classify_action(self, keypoints: List) -> str:
"""Classify pedestrian action from keypoints"""
if len(keypoints) < 33:
return "unknown"
# Simple action classification based on keypoint positions
# (This is simplified - real implementation would be more complex)
# Get key joints
left_hip = keypoints[23]
right_hip = keypoints[24]
left_knee = keypoints[25]
right_knee = keypoints[26]
# Calculate angles (simplified)
hip_y = (left_hip[1] + right_hip[1]) / 2
knee_y = (left_knee[1] + right_knee[1]) / 2
if abs(left_knee[1] - right_knee[1]) > 50:
return "walking"
else:
return "standing"
# Continue in next message...