File size: 30,525 Bytes
cb233ac
 
 
 
 
 
 
 
 
 
75e8992
cb233ac
 
75e8992
cb233ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75e8992
 
 
 
 
 
cb233ac
 
75e8992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb233ac
 
75e8992
 
cb233ac
 
 
75e8992
 
 
 
cb233ac
 
 
 
 
 
 
 
 
 
58351ba
 
cb233ac
58351ba
cb233ac
 
 
 
 
 
 
 
 
 
58351ba
 
cb233ac
58351ba
 
 
cb233ac
 
 
 
58351ba
 
 
cb233ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58351ba
cb233ac
 
 
 
58351ba
 
 
 
 
 
 
 
 
 
75e8992
 
 
 
 
 
 
 
 
 
 
 
58351ba
cb233ac
58351ba
cb233ac
58351ba
cb233ac
 
58351ba
cb233ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58351ba
 
cb233ac
 
 
 
 
 
 
58351ba
cb233ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d8d6b
cb233ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d8d6b
cb233ac
 
 
 
 
 
 
 
 
 
 
87d8d6b
cb233ac
 
 
 
 
 
 
 
 
87d8d6b
cb233ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d8d6b
cb233ac
 
 
 
 
 
 
 
58351ba
 
 
cb233ac
 
58351ba
 
cb233ac
 
87d8d6b
cb233ac
 
58351ba
 
cb233ac
58351ba
 
cb233ac
 
58351ba
cb233ac
58351ba
cb233ac
58351ba
cb233ac
 
 
58351ba
 
 
 
 
cb233ac
58351ba
 
 
 
cb233ac
 
 
 
 
 
 
 
 
 
 
58351ba
cb233ac
75e8992
 
cb233ac
 
 
 
 
 
 
 
58351ba
 
cb233ac
 
 
 
87d8d6b
cb233ac
75e8992
 
cb233ac
 
 
 
 
 
 
 
 
 
 
 
 
 
75e8992
cb233ac
 
75e8992
 
 
 
 
cb233ac
75e8992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb233ac
 
75e8992
 
 
 
 
 
 
 
 
 
cb233ac
75e8992
 
 
cb233ac
 
 
 
75e8992
 
cb233ac
 
87d8d6b
cb233ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d8d6b
cb233ac
 
 
 
 
 
 
87d8d6b
cb233ac
 
 
 
 
 
 
87d8d6b
cb233ac
 
 
 
87d8d6b
75e8992
 
 
 
 
 
 
 
 
 
 
 
87d8d6b
23e0176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d8d6b
cb233ac
75e8992
cb233ac
 
75e8992
 
 
 
cb233ac
 
 
75e8992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb233ac
75e8992
cb233ac
 
 
75e8992
cb233ac
 
 
5ba3ea2
d3af8e5
 
 
 
 
 
 
 
cb233ac
 
 
 
 
 
 
 
 
d3af8e5
5ba3ea2
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
"""
FastAPI Server for Lucid 2.0 Surveillance System
Provides real-time camera streaming and AI integration
"""

import cv2
import numpy as np
import asyncio
import threading
import time
import os
from typing import Generator, List, Tuple, Dict, Optional
from fastapi import FastAPI, HTTPException, Response, WebSocket, WebSocketDisconnect, UploadFile, File
from fastapi.responses import StreamingResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import json
from datetime import datetime

# Import existing components
from advanced_camera_manager import AdvancedCameraManager
from enhanced_tracking_system import EnhancedTrackingSystem
from realtime_embedding_matcher import RealtimeEmbeddingMatcher

app = FastAPI(title="Lucid 2.0 API", version="1.0.0")

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Global instances
camera_manager = None
tracking_system = None
matcher = None

# WebSocket connections
active_connections = []

# Person profiles storage
person_profiles = {}  # {global_id: {name, photo, metadata}}

# Frame cache for streaming
frame_cache = {}
frame_cache_lock = threading.Lock()

# Alert rules storage
alert_rules = []  # List of alert rule configurations

def initialize_system():
    """Initialize the surveillance system components"""
    global camera_manager, tracking_system, matcher
    
    print("Initializing Lucid 2.0 system...")
    
    # Initialize camera manager
    camera_manager = AdvancedCameraManager()
    
    # Initialize tracking system
    tracking_system = EnhancedTrackingSystem()
    if not tracking_system.initialize_system():
        raise Exception("Failed to initialize tracking system")
    
    # Initialize matcher
    matcher = RealtimeEmbeddingMatcher()
    matcher.start_matching()
    
    print("✓ Lucid 2.0 system initialized")

async def broadcast_detection(data):
    """Broadcast detection data to all active WebSocket connections"""
    disconnected = []
    for connection in active_connections:
        try:
            await connection.send_json(data)
        except WebSocketDisconnect:
            disconnected.append(connection)
        except Exception as e:
            print(f"Error sending to WebSocket: {e}")
            disconnected.append(connection)
    
    # Remove disconnected clients
    for connection in disconnected:
        if connection in active_connections:
            active_connections.remove(connection)

async def broadcast_alert(data):
    """Broadcast alert data to all active WebSocket connections"""
    disconnected = []
    for connection in active_connections:
        try:
            await connection.send_json(data)
        except WebSocketDisconnect:
            disconnected.append(connection)
        except Exception as e:
            print(f"Error sending alert to WebSocket: {e}")
            disconnected.append(connection)
    
    # Remove disconnected clients
    for connection in disconnected:
        if connection in active_connections:
            active_connections.remove(connection)

def check_alert_rules(identity_data):
    """Check if any alert rules are triggered"""
    global alert_rules, active_connections
    
    if not alert_rules:
        return
    
    print(f"[Alert Rules] Checking {len(alert_rules)} rules for identity {identity_data.get('global_id', 'unknown')}")
    
    for rule in alert_rules:
        rule_type = rule.get("type")
        triggered = False
        alert_message = None
        
        # 1. Person Reappearance
        if rule_type == "reappearance":
            if rule.get("person_id") == identity_data.get("global_id"):
                triggered = True
                alert_message = f"Person {rule.get('person_name', identity_data.get('global_id')[:12])} has reappeared"
        
        # 2. Unknown Person After Hours
        elif rule_type == "unknown_after_hours":
            current_hour = datetime.now().hour
            if current_hour >= 21 or current_hour < 6:  # After 9 PM or before 6 AM
                if identity_data.get("confidence", 0) < 0.8:  # Unknown/low confidence
                    triggered = True
                    alert_message = f"Unknown person detected after hours (confidence: {identity_data.get('confidence', 0):.2f})"
        
        # 3. High Confidence Detection
        elif rule_type == "high_confidence":
            threshold = rule.get("threshold", 0.8)
            if identity_data.get("confidence", 0) >= threshold:
                triggered = True
                alert_message = f"High confidence detection: {identity_data.get('confidence', 0):.2f}"
        
        # 4. Loitering Detection (person seen for extended time)
        elif rule_type == "loitering":
            # This would need dwell time calculation - placeholder for now
            cameras_seen = identity_data.get("cameras_seen", [])
            if len(cameras_seen) >= 1:  # Detected in at least one camera
                triggered = True
                alert_message = f"Person detected in {len(cameras_seen)} camera(s)"
        
        # Send alert if triggered
        if triggered and alert_message:
            alert_data = {
                "type": "alert",
                "alert_type": rule_type,
                "message": alert_message,
                "timestamp": time.time(),
                "data": identity_data
            }
            
            print(f"[Alert] Rule triggered: {rule_type} - {alert_message}")
            
            # Broadcast to all connected clients
            asyncio.run_coroutine_threadsafe(
                broadcast_alert(alert_data), 
                asyncio.get_event_loop()
            )

def capture_frames_continuously(camera_id: str):
    """Continuously capture frames and store in cache for streaming"""
    global camera_manager, frame_cache, tracking_system, matcher
    
    print(f"[Streaming] Started frame capture for {camera_id}")
    print(f"[DEBUG] tracking_system initialized: {tracking_system is not None}")
    print(f"[DEBUG] camera_manager initialized: {camera_manager is not None}")
    
    frame_count = 0
    while True:
        try:
            # Check if components are initialized
            if camera_manager is None or tracking_system is None:
                time.sleep(1)
                continue
                
            # Get frame from camera manager
            ret, frame = camera_manager.get_frame(camera_id)
            
            print(f"[DEBUG] {camera_id}: Got frame - ret={ret}, frame shape={frame.shape if frame is not None else 'None'}")
            
            if ret and frame is not None:
                frame_count += 1
                if frame_count % 30 == 1:  # Log every 30 frames
                    print(f"[DEBUG] {camera_id}: Processing frame {frame_count}")
                # Process frame through tracking system
                frame_number = int(time.time() * 1000) % 1000000
                detections = tracking_system.process_frame(frame, camera_id, frame_number)
                
                if frame_count % 30 == 1:
                    print(f"[DEBUG] {camera_id}: Got {len(detections)} detections")
                
                # Draw detections on frame
                display_frame = frame.copy()
                
                # Draw detections
                for det in detections:
                    x1, y1, x2, y2 = det.bbox
                    # Draw bounding box
                    cv2.rectangle(display_frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
                    # Draw label
                    label = f"Det: {det.id[:8]}"
                    if det.confidence:
                        label += f" ({det.confidence:.2f})"
                    cv2.putText(display_frame, label, (x1, y1 - 10),
                               cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
                
                # Draw tracks and create global identities
                active_tracks = tracking_system.get_active_tracks(camera_id)
                for track in active_tracks:
                    if track.detections:
                        latest_detection = track.detections[-1]
                        
                        # FIXED: Ensure global ID is created for new tracks
                        if not track.global_id and track.representative_embedding is not None:
                            # Try cross-camera re-identification first
                            matched_global_id = tracking_system.cross_camera_reidentification(track)
                            if not matched_global_id:
                                # Create new global identity
                                tracking_system._create_global_identity(track)
                                if frame_count % 30 == 1:
                                    print(f"[DEBUG] Created new global ID: {track.global_id} for track {track.id}")
                                
                                # Check alert rules for new identity
                                if track.global_id:
                                    identity = tracking_system.global_identities.get(track.global_id)
                                    if identity:
                                        identity_data = {
                                            "global_id": track.global_id,
                                            "confidence": identity.confidence,
                                            "cameras_seen": identity.cameras_seen,
                                            "person_name": track.global_id[:12]
                                        }
                                        check_alert_rules(identity_data)
                        
                        x1, y1, x2, y2 = latest_detection.bbox
                        # Draw track box (green for tracked)
                        cv2.rectangle(display_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
                        # Draw track label with global ID
                        track_label = f"Track: {track.id}"
                        if track.global_id:
                            track_label += f" ({track.global_id})"
                        cv2.putText(display_frame, track_label, (x1, y2 + 20),
                                   cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
                
                # Store frame in cache
                with frame_cache_lock:
                    frame_cache[camera_id] = display_frame
                
                # Send real-time alerts for new detections
                for det in detections:
                    if det.confidence > 0.7:  # Only send high-confidence detections
                        detection_data = {
                            "type": "new_detection",
                            "camera_id": camera_id,
                            "detection_id": det.id,
                            "bbox": det.bbox,
                            "confidence": det.confidence,
                            "timestamp": time.time()
                        }
                        # Run in asyncio thread
                        asyncio.run_coroutine_threadsafe(
                            broadcast_detection(detection_data), 
                            asyncio.get_event_loop()
                        )
                
                # Control frame rate
                time.sleep(0.033)  # ~30 FPS
            else:
                time.sleep(0.1)
                
        except Exception as e:
            print(f"[Streaming] Error capturing from {camera_id}: {e}")
            time.sleep(1)

def generate_frames(camera_id: str) -> Generator[bytes, None, None]:
    """Generate MJPEG frames for streaming"""
    global frame_cache
    
    print(f"[STREAM] Client connected to /video_feed/{camera_id}")
    
    # Start capture thread if not already running
    capture_thread = threading.Thread(
        target=capture_frames_continuously,
        args=(camera_id,),
        daemon=True
    )
    capture_thread.start()
    print(f"[STREAM] Started capture thread for {camera_id}")
    
    while True:
        try:
            # Get frame from cache
            with frame_cache_lock:
                frame = frame_cache.get(camera_id)
            
            if frame is not None:
                # Encode frame as JPEG
                ret, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 80])
                if ret:
                    # Yield frame in MJPEG format
                    yield (b'--frame\r\n'
                           b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')
            
            # Control stream rate
            time.sleep(0.033)  # ~30 FPS
            
        except Exception as e:
            print(f"[Streaming] Error generating frames for {camera_id}: {e}")
            time.sleep(1)

@app.get("/")
async def root():
    return {"message": "Lucid 2.0 Surveillance System API", "status": "online"}

@app.get("/api/cameras")
async def get_cameras():
    """Get list of all cameras"""
    if camera_manager is None:
        raise HTTPException(status_code=503, detail="Camera manager not initialized")
    
    cameras = camera_manager.get_all_cameras()
    return {
        "cameras": [
            {
                "id": cam_id,
                "source": cam_info.source,
                "type": cam_info.type,
                "status": cam_info.status,
                "resolution": cam_info.resolution,
                "fps": cam_info.fps
            }
            for cam_id, cam_info in cameras.items()
        ]
    }

@app.post("/api/cameras")
async def add_camera(camera_id: str, source: str):
    """Add a new camera"""
    if camera_manager is None:
        raise HTTPException(status_code=503, detail="Camera manager not initialized")
    
    success = camera_manager.add_camera(camera_id, source)
    if success:
        return {"message": f"Camera {camera_id} added successfully", "status": "success"}
    else:
        raise HTTPException(status_code=400, detail=f"Failed to add camera {camera_id}")

@app.delete("/api/cameras/{camera_id}")
async def remove_camera(camera_id: str):
    """Remove a camera"""
    if camera_manager is None:
        raise HTTPException(status_code=503, detail="Camera manager not initialized")
    
    # Stop camera capture
    camera_manager.remove_camera(camera_id)
    return {"message": f"Camera {camera_id} removed", "status": "success"}

@app.get("/api/video_feed/{camera_id}")
async def video_feed(camera_id: str):
    """Stream video feed from a camera as MJPEG"""
    if camera_manager is None:
        raise HTTPException(status_code=503, detail="Camera manager not initialized")
    
    # Check if camera exists
    cameras = camera_manager.get_all_cameras()
    if camera_id not in cameras:
        raise HTTPException(status_code=404, detail=f"Camera {camera_id} not found")
    
    # Return streaming response
    return StreamingResponse(
        generate_frames(camera_id),
        media_type="multipart/x-mixed-replace; boundary=frame"
    )

@app.get("/api/stats")
async def get_system_stats():
    """Get system statistics"""
    if tracking_system is None or matcher is None:
        raise HTTPException(status_code=503, detail="System not fully initialized")
    
    tracking_stats = tracking_system.get_system_stats()
    matcher_stats = matcher.get_system_statistics()
    
    # Add global identity count
    global_identity_count = len(tracking_system.global_identities)
    
    return {
        "tracking": tracking_stats,
        "matcher": matcher_stats,
        "global_identities": global_identity_count
    }

@app.get("/api/identities")
async def get_identities():
    """Get all global identities with enhanced data"""
    if tracking_system is None:
        raise HTTPException(status_code=503, detail="Tracking system not initialized")
    
    # FIXED: Get identities directly from tracking system instead of matcher
    all_identities = tracking_system.global_identities
    enhanced_identities = []
    
    for global_id, identity in all_identities.items():
        # Get profile if exists
        profile = person_profiles.get(global_id, {})
        
        # Create timeline of camera appearances from tracks
        timeline = []
        camera_timestamps = {}  # {camera_id: [timestamps]}
        
        for track in identity.tracks:
            if track.camera_id not in camera_timestamps:
                camera_timestamps[track.camera_id] = []
            camera_timestamps[track.camera_id].append(track.first_seen)
            
            timeline.append({
                "camera_id": track.camera_id,
                "first_seen": track.first_seen,
                "last_seen": track.last_seen,
                "detections": track.detection_count
            })
        
        # Sort timeline by time
        timeline.sort(key=lambda x: x["first_seen"])
        
        enhanced_identity = {
            "global_id": identity.global_id,
            "confidence": identity.confidence,
            "cameras_seen": identity.cameras_seen,
            "first_seen": identity.first_seen,
            "last_seen": identity.last_seen,
            "observations": len(identity.tracks),
            "timeline": timeline,
            "profile": profile if profile else None,
            "snapshot_url": f"/snapshots/{global_id}.jpg" if os.path.exists(f"{tracking_system.snapshots_dir}/{global_id}.jpg") else None,
            "heatmap": camera_timestamps  # For heatmap visualization
        }
        
        enhanced_identities.append(enhanced_identity)
        
        # Check alert rules
        check_alert_rules(enhanced_identity)
    
    print(f"[DEBUG] /identities endpoint returning {len(enhanced_identities)} identities")
    
    return {
        "identities": enhanced_identities
    }

@app.post("/api/find_person")
async def find_person(image: UploadFile = File(...)):
    """Find a person across all cameras using Re-ID with improved accuracy"""
    if tracking_system is None:
        raise HTTPException(status_code=503, detail="System not fully initialized")
    
    try:
        # Read uploaded image
        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        
        if img is None:
            raise HTTPException(status_code=400, detail="Invalid image file")
        
        # Extract features from the uploaded image
        face_emb, body_emb, _ = tracking_system.extract_features(img, (0, 0, img.shape[1], img.shape[0]))
        
        if body_emb is None and face_emb is None:
            raise HTTPException(status_code=400, detail="Could not extract features from image")
        
        print(f"[Search] Extracted embeddings - Face: {face_emb is not None}, Body: {body_emb is not None}")
        
        # Search against ALL global identities directly (more accurate than vector DB)
        from scipy.spatial.distance import cosine
        matches = []
        
        for global_id, identity in tracking_system.global_identities.items():
            best_similarity = 0.0
            match_type = None
            
            # Compare body embeddings
            if body_emb is not None and len(identity.body_embeddings) > 0:
                for body_emb_stored in identity.body_embeddings:
                    if body_emb_stored is not None:
                        similarity = 1 - cosine(body_emb, body_emb_stored)
                        if similarity > best_similarity:
                            best_similarity = similarity
                            match_type = "body"
            
            # Compare face embeddings (more reliable, higher weight)
            if face_emb is not None and len(identity.face_embeddings) > 0:
                for face_emb_stored in identity.face_embeddings:
                    if face_emb_stored is not None:
                        similarity = 1 - cosine(face_emb, face_emb_stored)
                        # Face matching is more discriminative, so boost it slightly
                        if similarity > best_similarity:
                            best_similarity = similarity
                            match_type = "face"
            
            # Only include matches above threshold (0.70 for balanced precision/recall)
            if best_similarity >= 0.70:
                # Get most recent appearance info
                latest_track = max(identity.tracks, key=lambda t: t.last_seen) if identity.tracks else None
                
                matches.append({
                    "global_id": global_id,
                    "confidence": float(best_similarity),
                    "match_type": match_type,
                    "camera_id": latest_track.camera_id if latest_track else "unknown",
                    "timestamp": identity.last_seen,
                    "first_seen": identity.first_seen,
                    "cameras_seen": identity.cameras_seen,
                    "observations": len(identity.tracks),
                    "entity_id": global_id  # For compatibility
                })
        
        # Sort by confidence (highest first)
        matches.sort(key=lambda x: x["confidence"], reverse=True)
        
        print(f"[Search] Found {len(matches)} matches above threshold 0.70")
        if len(matches) == 0:
            print(f"[Search] No matches found. Total identities in system: {len(tracking_system.global_identities)}")
            # Log similarity scores for debugging
            for global_id, identity in list(tracking_system.global_identities.items())[:5]:
                print(f"  {global_id}: Face embeddings: {len(identity.face_embeddings)}, Body embeddings: {len(identity.body_embeddings)}")
        
        return {
            "matches": matches[:20],  # Return top 20 matches
            "total_matches": len(matches),
            "search_method": "global_identity_direct"
        }
        
    except Exception as e:
        print(f"Error in find_person: {e}")
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")

@app.post("/api/profiles/{global_id}")
async def create_profile(global_id: str, name: str, metadata: Optional[str] = None):
    """Create or update a person profile"""
    global person_profiles
    
    profile = {
        "name": name,
        "created_at": time.time(),
        "metadata": metadata or "{}"
    }
    
    person_profiles[global_id] = profile
    
    return {
        "message": f"Profile created for {global_id}",
        "profile": profile
    }

@app.get("/api/profiles/{global_id}")
async def get_profile(global_id: str):
    """Get a person's profile"""
    if global_id in person_profiles:
        return person_profiles[global_id]
    else:
        raise HTTPException(status_code=404, detail="Profile not found")

@app.post("/api/alerts/rules")
async def create_alert_rule(rule: dict):
    """Create a new alert rule"""
    global alert_rules
    
    alert_rules.append(rule)
    return {"message": "Alert rule created", "rule": rule}

@app.get("/api/alerts/rules")
async def get_alert_rules():
    """Get all alert rules"""
    return {"rules": alert_rules}

@app.get("/api/snapshots/{filename}")
async def get_snapshot(filename: str):
    """Serve snapshot images"""
    if tracking_system is None:
        raise HTTPException(status_code=503, detail="Tracking system not initialized")
    
    snapshot_path = os.path.join(tracking_system.snapshots_dir, filename)
    
    if not os.path.exists(snapshot_path):
        raise HTTPException(status_code=404, detail="Snapshot not found")
    
    return FileResponse(snapshot_path, media_type="image/jpeg")

@app.get("/api/analytics")
async def get_analytics():
    """Get comprehensive system analytics"""
    if tracking_system is None:
        raise HTTPException(status_code=503, detail="Tracking system not initialized")
    
    current_time = time.time()
    all_identities = tracking_system.global_identities
    
    # 1. Overview Metrics
    total_identities = len(all_identities)
    active_identities = len([id for id, identity in all_identities.items() 
                            if current_time - identity.last_seen < 300])  # Active in last 5 min
    
    # 2. Camera Analytics
    camera_stats = {}
    cameras = camera_manager.get_all_cameras() if camera_manager else []
    
    for camera in cameras:
        camera_id = str(camera.get('id', '')) if isinstance(camera, dict) else camera
        # Count unique people seen by this camera
        unique_visitors = len([id for id, identity in all_identities.items()
                              if camera_id in identity.cameras_seen])
        
        # Count observations in this camera
        total_observations = sum([len([t for t in identity.tracks if t.camera_id == camera_id])
                                 for identity in all_identities.values()])
        
        camera_stats[camera_id] = {
            "unique_visitors": unique_visitors,
            "total_observations": total_observations,
            "status": camera.get('status', 'unknown') if isinstance(camera, dict) else 'unknown'
        }
    
    # 3. Time-based Analytics
    # Calculate average dwell time
    dwell_times = []
    for identity in all_identities.values():
        dwell_time = identity.last_seen - identity.first_seen
        dwell_times.append(dwell_time)
    
    avg_dwell_time = sum(dwell_times) / len(dwell_times) if dwell_times else 0
    
    # 4. Movement Patterns
    cross_camera_movements = 0
    for identity in all_identities.values():
        if len(identity.cameras_seen) > 1:
            cross_camera_movements += 1
    
    # 5. Recent Activity (last hour)
    one_hour_ago = current_time - 3600
    recent_identities = len([id for id, identity in all_identities.items()
                            if identity.first_seen > one_hour_ago])
    
    # 6. Detection Quality
    avg_confidence = sum([identity.confidence for identity in all_identities.values()]) / total_identities if total_identities > 0 else 0
    
    # 7. System Performance
    system_stats = tracking_system.get_system_stats() if tracking_system else {}
    
    return {
        "overview": {
            "total_identities": total_identities,
            "active_identities": active_identities,
            "inactive_identities": total_identities - active_identities,
            "recent_identities_1h": recent_identities
        },
        "camera_analytics": camera_stats,
        "time_analytics": {
            "average_dwell_time_seconds": round(avg_dwell_time, 2),
            "average_dwell_time_minutes": round(avg_dwell_time / 60, 2)
        },
        "movement_analytics": {
            "cross_camera_movements": cross_camera_movements,
            "percentage_cross_camera": round((cross_camera_movements / total_identities * 100) if total_identities > 0 else 0, 2)
        },
        "quality_metrics": {
            "average_confidence": round(avg_confidence, 3),
            "high_confidence_count": len([i for i in all_identities.values() if i.confidence > 0.8]),
            "medium_confidence_count": len([i for i in all_identities.values() if 0.5 < i.confidence <= 0.8]),
            "low_confidence_count": len([i for i in all_identities.values() if i.confidence <= 0.5])
        },
        "system_performance": system_stats,
        "timestamp": current_time
    }

@app.websocket("/api/ws")
async def websocket_endpoint(websocket: WebSocket):
    """WebSocket endpoint for real-time alerts and updates"""
    await websocket.accept()
    active_connections.append(websocket)
    print(f"[WebSocket] Client connected. Total connections: {len(active_connections)}")
    
    # Track which identities we've already alerted for
    alerted_identities = set()
    
    try:
        while True:
            await asyncio.sleep(2)  # Check every 2 seconds
            
            # Send alerts for new global identities
            if tracking_system:
                identities = tracking_system.global_identities
                
                for global_id, identity in identities.items():
                    # Skip if we've already alerted for this identity
                    if global_id in alerted_identities:
                        continue
                    
                    # Alert for new high-confidence identity
                    if identity.confidence > 0.75:
                        alert = {
                            "type": "alert",
                            "alert_type": "new_identity",
                            "message": f"New person detected: {global_id[:12]}",
                            "timestamp": time.time(),
                            "data": {
                                "global_id": global_id,
                                "confidence": identity.confidence,
                                "cameras_seen": identity.cameras_seen,
                                "person_name": global_id[:12]
                            }
                        }
                        
                        try:
                            await websocket.send_json(alert)
                            alerted_identities.add(global_id)
                            print(f"[Alert] Sent new_identity alert for {global_id[:12]}")
                        except Exception as e:
                            print(f"[Alert] Error sending alert: {e}")
                            break
                
    except WebSocketDisconnect:
        print(f"[WebSocket] Client disconnected")
        if websocket in active_connections:
            active_connections.remove(websocket)
    except Exception as e:
        print(f"[WebSocket] Error: {e}")
        if websocket in active_connections:
            active_connections.remove(websocket)


@app.on_event("startup")
async def startup_event():
    try:
        initialize_system()
    except Exception as e:
        print(f"Failed to initialize Lucid system on startup: {e}")


if __name__ == "__main__":
    # Initialize system
    try:
        initialize_system()
    except Exception as e:
        print(f"Failed to initialize system: {e}")
        exit(1)
    
    # Run server
    uvicorn.run(app, host="0.0.0.0", port=8000)