""" 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)