-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadvanced_demo.py
More file actions
349 lines (270 loc) · 11.6 KB
/
advanced_demo.py
File metadata and controls
349 lines (270 loc) · 11.6 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
#!/usr/bin/env python3
"""
Advanced Easy-SLAM Demo
Showcases all advanced features: RGB-D SLAM, semantic mapping, sensor fusion, map merging, and profiling.
"""
import time
import numpy as np
import open3d as o3d
from pathlib import Path
def demo_rgbd_slam():
"""Demo RGB-D SLAM with Open3D."""
print("\n" + "="*50)
print("RGB-D SLAM DEMO")
print("="*50)
try:
from easy_slam.algorithms import RGBDSLAM
# Initialize RGB-D SLAM
slam = RGBDSLAM(voxel_size=0.02)
print("✓ RGB-D SLAM initialized")
# Create mock RGB-D data
rgb = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
depth = np.random.rand(480, 640) * 5.0 # 0-5 meters
# Process frames
for i in range(10):
pose = slam.process(rgb, depth)
print(f" Frame {i+1}: Pose computed")
# Get results
trajectory = slam.get_trajectory()
map_pcd = slam.get_map()
print(f"✓ Processed {len(trajectory)} frames")
print(f"✓ Generated map with {len(map_pcd.points)} points")
return slam
except Exception as e:
print(f"✗ RGB-D SLAM demo failed: {e}")
return None
def demo_semantic_mapping():
"""Demo semantic mapping with YOLOv8."""
print("\n" + "="*50)
print("SEMANTIC MAPPING DEMO")
print("="*50)
try:
from easy_slam.utils.semantic import SemanticMapper
# Initialize semantic mapper
mapper = SemanticMapper()
print("✓ Semantic mapper initialized")
# Create mock RGB image
rgb = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
# Detect objects
detections = mapper.detect(rgb)
print(f"✓ Detected {len(detections)} objects")
for det in detections[:3]: # Show first 3 detections
print(f" - Class {det['class']}: {det['conf']:.2f} confidence")
# Create mock point cloud and attach semantics
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(np.random.randn(100, 3))
# Mock camera intrinsic
intrinsic = o3d.camera.PinholeCameraIntrinsic(640, 480, 525, 525, 320, 240)
# Attach semantic labels
semantic_data = mapper.attach_semantics(pcd, rgb, np.random.rand(480, 640), intrinsic)
print(f"✓ Attached semantics to {len(semantic_data)} points")
return mapper
except Exception as e:
print(f"✗ Semantic mapping demo failed: {e}")
return None
def demo_sensor_fusion():
"""Demo sensor fusion with EKF."""
print("\n" + "="*50)
print("SENSOR FUSION DEMO")
print("="*50)
try:
from easy_slam.utils.sensor_fusion import SensorFusion
# Initialize sensor fusion
fusion = SensorFusion()
print("✓ Sensor fusion initialized")
# Simulate IMU data
for i in range(100):
gyro = np.random.randn(3) * 0.1 # Angular velocity
accel = np.random.randn(3) * 0.5 # Linear acceleration
fusion.add_imu(gyro, accel)
# Simulate visual odometry
for i in range(10):
pos = np.random.randn(3) * 0.1
quat = np.array([1, 0, 0, 0]) + np.random.randn(4) * 0.01
quat = quat / np.linalg.norm(quat) # Normalize
fusion.add_visual(pos, quat)
# Simulate GPS
for i in range(5):
gps_pos = np.random.randn(3) * 10 # GPS position
fusion.add_gps(gps_pos)
# Get fused pose
fused_pose = fusion.get_fused_pose()
velocity = fusion.get_velocity()
orientation = fusion.get_orientation()
print(f"✓ Fused {len(fusion.imu_data)} IMU measurements")
print(f"✓ Fused {len(fusion.visual_data)} visual measurements")
print(f"✓ Fused {len(fusion.gps_data)} GPS measurements")
print(f"✓ Current velocity: {np.linalg.norm(velocity):.3f} m/s")
return fusion
except Exception as e:
print(f"✗ Sensor fusion demo failed: {e}")
return None
def demo_map_merging():
"""Demo map merging and loop closure detection."""
print("\n" + "="*50)
print("MAP MERGING DEMO")
print("="*50)
try:
from easy_slam.utils.map_merging import MapMerger, MultiSessionMapper
# Initialize map merger
merger = MapMerger(voxel_size=0.05)
print("✓ Map merger initialized")
# Create mock point clouds
map1 = o3d.geometry.PointCloud()
map1.points = o3d.utility.Vector3dVector(np.random.randn(200, 3))
map1.paint_uniform_color([1, 0, 0]) # Red
map2 = o3d.geometry.PointCloud()
map2.points = o3d.utility.Vector3dVector(np.random.randn(200, 3) + [5, 0, 0])
map2.paint_uniform_color([0, 1, 0]) # Green
# Merge maps
merged_map, transform = merger.merge_maps(map1, map2)
print(f"✓ Merged maps with {len(merged_map.points)} total points")
print(f"✓ Transformation matrix computed")
# Multi-session mapping
multi_mapper = MultiSessionMapper()
# Add sessions
poses1 = [np.eye(4) for _ in range(10)]
poses2 = [np.eye(4) for _ in range(10)]
multi_mapper.add_session(map1, poses1)
multi_mapper.add_session(map2, poses2)
global_map = multi_mapper.get_global_map()
print(f"✓ Multi-session mapping: {len(multi_mapper.maps)} sessions")
print(f"✓ Global map has {len(global_map.points)} points")
return merger, multi_mapper
except Exception as e:
print(f"✗ Map merging demo failed: {e}")
return None, None
def demo_advanced_profiling():
"""Demo advanced performance profiling."""
print("\n" + "="*50)
print("ADVANCED PROFILING DEMO")
print("="*50)
try:
from easy_slam.utils.advanced_profiling import PerformanceProfiler, ProfilerContext
# Initialize profiler
profiler = PerformanceProfiler()
print("✓ Performance profiler initialized")
# Simulate different module timings
for i in range(50):
# Simulate sensor read
with ProfilerContext(profiler, "sensor_read"):
time.sleep(0.01)
# Simulate SLAM processing
with ProfilerContext(profiler, "slam_processing"):
time.sleep(0.02)
# Simulate visualization
with ProfilerContext(profiler, "visualization"):
time.sleep(0.005)
# Record frame
profiler.record_frame()
# Get statistics
stats = profiler.get_system_stats()
print(f"✓ Processed {stats['frame_count']} frames")
print(f"✓ Average FPS: {stats['avg_fps']:.1f}")
print(f"✓ Average memory: {stats['avg_memory_mb']:.1f} MB")
print(f"✓ Average CPU: {stats['avg_cpu_percent']:.1f}%")
# Get module statistics
for module in ["sensor_read", "slam_processing", "visualization"]:
module_stats = profiler.get_module_stats(module)
if module_stats:
print(f" {module}: {module_stats['mean']*1000:.2f} ms avg")
# Get bottlenecks
bottlenecks = profiler.get_bottlenecks()
if bottlenecks:
print("✓ Performance bottlenecks detected:")
for bottleneck in bottlenecks[:3]:
print(f" - {bottleneck['module']}: {bottleneck['spike_factor']:.1f}x spike")
# Generate report
report = profiler.generate_report("demo_performance_report.txt")
print("✓ Performance report generated")
# Stop profiler
profiler.stop()
return profiler
except Exception as e:
print(f"✗ Advanced profiling demo failed: {e}")
return None
def demo_integrated_features():
"""Demo integrated advanced features."""
print("\n" + "="*50)
print("INTEGRATED ADVANCED FEATURES DEMO")
print("="*50)
try:
from easy_slam import EasySLAM
# Initialize EasySLAM with all advanced features
slam = EasySLAM(
camera=0,
algorithm='rgbd_slam',
visualization=False, # Disable for demo
enable_semantic=True,
enable_sensor_fusion=True,
enable_map_merging=True,
enable_profiling=True,
voxel_size=0.02
)
print("✓ EasySLAM initialized with all advanced features")
# Simulate processing
print(" Simulating SLAM processing...")
time.sleep(2) # Simulate processing time
# Get performance stats
stats = slam.get_performance_stats()
if stats:
print(f" Performance: {stats['fps']:.1f} FPS, {stats['memory_mb']:.1f} MB")
# Get fused pose
fused_pose = slam.get_fused_pose()
if fused_pose is not None:
print(" ✓ Sensor fusion active")
# Get global map
global_map = slam.get_global_map()
if global_map is not None:
print(" ✓ Map merging active")
# Save performance report
slam.save_performance_report("integrated_demo_report.txt")
print(" ✓ Performance report saved")
slam.stop()
print("✓ Integrated demo completed")
return slam
except Exception as e:
print(f"✗ Integrated features demo failed: {e}")
return None
def main():
"""Run all advanced feature demos."""
print("🚀 ADVANCED EASY-SLAM FEATURES DEMO")
print("=" * 60)
print("This demo showcases all advanced features:")
print("• RGB-D SLAM with Open3D")
print("• Semantic mapping with YOLOv8")
print("• Sensor fusion with Extended Kalman Filter")
print("• Map merging and loop closure detection")
print("• Advanced performance profiling")
print("• Integrated features demonstration")
print("=" * 60)
# Run individual demos
results = {}
results['rgbd_slam'] = demo_rgbd_slam()
results['semantic'] = demo_semantic_mapping()
results['sensor_fusion'] = demo_sensor_fusion()
results['map_merging'] = demo_map_merging()
results['profiling'] = demo_advanced_profiling()
results['integrated'] = demo_integrated_features()
# Summary
print("\n" + "="*60)
print("DEMO SUMMARY")
print("="*60)
successful_demos = sum(1 for result in results.values() if result is not None)
total_demos = len(results)
print(f"Successful demos: {successful_demos}/{total_demos}")
for demo_name, result in results.items():
status = "✓ PASSED" if result is not None else "✗ FAILED"
print(f" {demo_name.replace('_', ' ').title()}: {status}")
if successful_demos == total_demos:
print("\n🎉 ALL ADVANCED FEATURES WORKING!")
print("✅ Easy-SLAM is ready for production use with advanced capabilities!")
else:
print(f"\n⚠️ {total_demos - successful_demos} demos failed")
print("Check the error messages above for details.")
print("\n📁 Generated files:")
print(" - demo_performance_report.txt")
print(" - integrated_demo_report.txt")
print(" - performance_report.txt (if integrated demo ran)")
if __name__ == "__main__":
main()