Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

54 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🏠 HMS (Home Monitoring System)

HMS_inverted (1)

An advanced AI-powered home surveillance system integrating computer vision, real-time processing, and intelligent notifications

Next.js Python YOLOv8 FastAPI TailwindCSS License

πŸ“– Project Overview

HMS is a sophisticated home monitoring solution that leverages the power of artificial intelligence and edge computing to provide real-time surveillance capabilities. Built on the Raspberry Pi 5 platform, it combines state-of-the-art object detection with efficient video streaming and intelligent notification systems.

Key Objectives

  • Real-time human detection and tracking
  • Low-latency video streaming
  • Resource-efficient edge computing
  • Intelligent alert system
  • User-friendly web interface

🌟 Technical Architecture

System Components

1. Computer Vision Pipeline

  • YOLOv8 Neural Network

    • Optimized NCNN model for edge deployment
    • Real-time object detection at ~20-30 FPS
    • Custom-trained on surveillance scenarios
    • Efficient model quantization for Raspberry Pi
  • Image Processing

    • OpenCV-based frame processing
    • Real-time video stream optimization
    • Adaptive frame rate control
    • Memory-efficient buffer management

2. Backend Infrastructure

  • FastAPI Server

    app = FastAPI()
    
    @app.get("/")
    def video_feed():
        return StreamingResponse(
            generate_frames(),
            media_type="multipart/x-mixed-replace; boundary=frame"
        )
    • Asynchronous request handling
    • MJPEG streaming implementation
    • RESTful API endpoints
    • Websocket support for real-time updates
  • Camera Interface

    picam2 = Picamera2()
    picam2.preview_configuration.main.size = (640, 480)
    picam2.preview_configuration.main.format = "RGB888"
    picam2.configure("preview")
    • Direct hardware access
    • Configurable resolution and format
    • Optimized frame capture
    • Hardware-accelerated processing

3. Frontend Architecture

  • Next.js Application

    • Server-side rendering for optimal performance
    • Dynamic route handling
    • Client-side state management
    • Responsive design implementation
  • Real-time Updates

    const streamUrl = process.env.NEXT_PUBLIC_CAMERA_URL;
    const videoRef = useRef<HTMLImageElement>(null);
    
    useEffect(() => {
      if (videoRef.current) {
        videoRef.current.src = streamUrl;
      }
    }, [streamUrl]);
    • Live stream integration
    • Dynamic content updates
    • Efficient DOM manipulation
    • Browser compatibility handling

4. Notification System

def send_email():
    global last_email_time
    current_time = time.time()
    
    if current_time - last_email_time < 120:  # 2-minute cooldown
        return
        
    try:
        yag = yagmail.SMTP(SENDER_EMAIL, APP_PASSWORD)
        yag.send(
            to=RECEIVER_EMAIL,
            subject="Alert: Human Detected!",
            contents="Human detected by surveillance system."
        )
        last_email_time = current_time
    except Exception as e:
        print(f"Failed to send email: {e}")
  • Rate-limited notifications
  • SMTP email integration
  • Error handling and retry logic
  • Customizable alert templates

πŸ› οΈ Technical Implementation

AI Model Optimization

# YOLOv8 Configuration
model = YOLO("yolov8n_ncnn_model")
results = model.predict(
    frame,
    imgsz=320,    # Reduced size for performance
    conf=0.5,     # Confidence threshold
    iou=0.4,      # NMS IoU threshold
    max_det=10    # Max detections per frame
)

Performance Metrics

  • Inference Speed: 20-30 FPS on Raspberry Pi 5
  • Detection Accuracy: >90% for human detection
  • Latency: <100ms end-to-end
  • Memory Usage: ~500MB RAM during operation

Stream Processing Pipeline

  1. Frame Capture
    frame = picam2.capture_array()
  2. AI Processing
    results = model.predict(frame)
  3. Frame Annotation
    annotated_frame = results[0].plot()
  4. Stream Encoding
    ret, buffer = cv2.imencode('.jpg', annotated_frame)

πŸ“‹ Prerequisites

Hardware Requirements

  • Raspberry Pi 5 (4GB+ RAM)
  • Raspberry Pi Camera Module v3
  • Adequate cooling solution
  • Stable power supply (3A recommended)

Software Requirements

  • Raspberry Pi OS (64-bit recommended)
  • Python 3.9+
  • Node.js 18+
  • Git

Network Requirements

  • Stable network connection
  • Port forwarding (if remote access needed)
  • Sufficient bandwidth (>2Mbps upload)

πŸš€ Installation

1. System Preparation

# Update system packages
sudo apt update && sudo apt upgrade

# Install system dependencies
sudo apt install -y python3-pip python3-venv nodejs npm git

2. Clone Repository

git clone https://github.com/recursionReaper/HMS.git
cd HMS

3. Backend Setup

# Create virtual environment
python3 -m venv venv
source venv/bin/activate

# Install Python dependencies
pip install -r requirements.txt

# Install NCNN dependencies
sudo apt install -y cmake build-essential

4. Frontend Setup

cd hms-next
npm install

5. Environment Configuration

# Create .env file
cat > .env << EOL
NEXT_PUBLIC_CAMERA_URL=http://localhost:8000
NOTIFICATION_COOLDOWN=120
DETECTION_CONFIDENCE=0.5
EOL

πŸ”§ Advanced Configuration

YOLOv8 Fine-tuning

# Model configuration options
model_config = {
    'imgsz': 320,          # Input image size
    'conf': 0.5,           # Confidence threshold
    'iou': 0.4,           # NMS IoU threshold
    'max_det': 10,        # Maximum detections
    'device': 'cpu',      # Inference device
    'classes': [0],       # Filter for humans only
}

Stream Quality Settings

# Camera configuration
picam2.preview_configuration.main.size = (640, 480)
picam2.preview_configuration.main.format = "RGB888"
picam2.preview_configuration.align()

# JPEG encoding parameters
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]

Notification Configuration

# Email settings
EMAIL_CONFIG = {
    'SENDER': 'your-email@gmail.com',
    'PASSWORD': 'app-specific-password',
    'RECEIVER': 'recipient@gmail.com',
    'COOLDOWN': 120,  # seconds
    'RETRY_ATTEMPTS': 3
}

πŸ” Performance Optimization

Memory Management

  • Frame buffer optimization
  • Garbage collection tuning
  • Resource monitoring
  • Cache management

CPU Utilization

  • Thread pool management
  • Process priority setting
  • Background task scheduling
  • Load balancing

Network Optimization

  • Frame compression
  • Bandwidth throttling
  • Connection pooling
  • Error recovery

🀝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Development Workflow

  1. Fork the repository
  2. Create a feature branch
  3. Implement changes
  4. Write/update tests
  5. Submit pull request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘₯ Team

  • Aniket Desai - @recursionReaper

    • System Architecture
    • Hardware Integration
    • Backend architecture
    • Performance Optimization
  • Yash Ogale - @yashogale30

    • AI Model Development
    • Frontend Development
    • Stream Processing
    • System Integration

πŸ™ Acknowledgments


Made with ❀️ by Team HMS

Report Bug Β· Request Feature

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages