Skip to content

A real-time movement detection application built with C# and OpenCvSharp that provides comprehensive motion monitoring and automatic video recording capabilities.

Notifications You must be signed in to change notification settings

arhadnane/MovementDetection

Repository files navigation

๐ŸŽฅ Movement Detection System

A real-time movement detection application built with C# and OpenCvSharp that provides comprehensive motion monitoring and automatic video recording capabilities.

Platform .NET License OpenCV

๐Ÿš€ Features

Core Functionality

  • Real-time Motion Detection: Advanced computer vision algorithms using background subtraction
  • Dual Video Display: Live feed and motion detection overlay side by side
  • Auto-Recording: Automatically records video when motion is detected
  • Manual Recording: Start/stop recording on demand
  • Configurable Sensitivity: Real-time adjustment from 1-100
  • Background Learning: Automatically adapts to lighting and scene changes

User Interface

  • Professional GUI: Clean Windows Forms interface
  • Real-time Controls: Start/Stop detection, sensitivity adjustment, background reset
  • Status Monitoring: Live motion detection status and event logging
  • Recording Controls: Manual recording toggle and auto-record checkbox

Advanced Features

  • Event Logging: Comprehensive logging with timestamps
  • Configuration System: JSON-based settings management
  • Error Handling: Robust camera and processing error management
  • Extensible Design: Modular architecture for easy feature additions

๐Ÿ“‹ Requirements

  • OS: Windows 10/11
  • Framework: .NET 8.0 or higher
  • Memory: 2GB RAM minimum
  • Camera: DirectShow compatible webcam
  • Storage: 100MB for application + space for recordings

๐Ÿ› ๏ธ Installation

Quick Start

  1. Clone the repository:

    git clone <repository-url>
    cd movement-detection
  2. Run the application:

    # Option 1: Use the demo script
    .\demo.ps1
    
    # Option 2: Use the batch file
    .\run.bat
    
    # Option 3: Direct command
    dotnet run --project MovementDetection.csproj

Build from Source

# Restore dependencies
dotnet restore

# Build the project
dotnet build

# Run the application
dotnet run

๐ŸŽฏ Usage

Basic Operation

  1. Start the Application - Camera initializes automatically
  2. Click "Start Detection" - Begin motion monitoring
  3. Adjust Sensitivity - Use the slider (recommended: 25-50)
  4. Enable Auto-Record - Check the "Auto Record" box for automatic recording
  5. Monitor Status - Watch real-time updates in the status panel

Motion Detection

  • Red rectangles highlight detected motion areas
  • Green contours show exact movement boundaries
  • Status updates appear in real-time
  • Event logging tracks all motion events with timestamps

Recording Features

  • Auto-Recording: Starts when motion detected, stops 5 seconds after motion ends
  • Manual Recording: Click "Start Recording" for continuous recording
  • File Management: Videos saved to Recordings/ with timestamp filenames
  • Event Logs: All activities logged to Logs/ directory

โš™๏ธ Configuration

Edit config.json to customize behavior:

{
  "Motion": {
    "Threshold": 25.0,           // Detection sensitivity
    "MinContourArea": 500.0,     // Minimum motion area
    "LearningRate": 0.01,        // Background learning rate
    "LearningFrames": 30         // Frames to learn background
  },
  "Camera": {
    "DeviceIndex": 0,            // Camera device number
    "FrameWidth": 640,           // Video resolution width
    "FrameHeight": 480,          // Video resolution height
    "Fps": 30.0                  // Frame rate
  },
  "Recording": {
    "AutoRecord": true,          // Enable auto-recording
    "OutputDirectory": "Recordings",
    "VideoFormat": "avi",
    "VideoCodec": "MJPG"
  }
}

๐Ÿ“ Project Structure

โ”œโ”€โ”€ Source Files
โ”‚   โ”œโ”€โ”€ MainForm.cs              # Main application window and UI logic
โ”‚   โ”œโ”€โ”€ MotionDetector.cs        # Core motion detection algorithms
โ”‚   โ”œโ”€โ”€ VideoRecorder.cs         # Video recording functionality
โ”‚   โ”œโ”€โ”€ MotionEventLogger.cs     # Event logging system
โ”‚   โ”œโ”€โ”€ AppConfig.cs             # Configuration management
โ”‚   โ””โ”€โ”€ Program.cs               # Application entry point
โ”œโ”€โ”€ Configuration
โ”‚   โ”œโ”€โ”€ MovementDetection.csproj # Project file with dependencies
โ”‚   โ”œโ”€โ”€ config.json              # Runtime configuration
โ”‚   โ””โ”€โ”€ .gitignore               # Git ignore rules
โ”œโ”€โ”€ Documentation
โ”‚   โ”œโ”€โ”€ README.md                # This file
โ”‚   โ”œโ”€โ”€ USER_GUIDE.md            # Detailed user manual
โ”‚   โ””โ”€โ”€ PROJECT_SUMMARY.md       # Development summary
โ”œโ”€โ”€ Scripts
โ”‚   โ”œโ”€โ”€ demo.ps1                 # PowerShell demo script
โ”‚   โ””โ”€โ”€ run.bat                  # Simple startup script
โ””โ”€โ”€ Output Directories
    โ”œโ”€โ”€ Recordings/              # Recorded video files
    โ””โ”€โ”€ Logs/                    # Event and system logs

๐Ÿ”ง Technical Details

Motion Detection Algorithm

  1. Frame Capture: 30 FPS video processing
  2. Preprocessing: Grayscale conversion and Gaussian blur
  3. Background Modeling: Adaptive background subtraction
  4. Motion Detection: Frame differencing with binary thresholding
  5. Noise Reduction: Morphological operations (opening/closing)
  6. Contour Analysis: Area-based motion filtering
  7. Visual Feedback: Bounding boxes and contour overlay

Dependencies

  • OpenCvSharp4: Computer vision library for .NET
  • OpenCvSharp4.runtime.win: Windows runtime for OpenCV
  • OpenCvSharp4.Extensions: Additional OpenCV extensions
  • System.Text.Json: JSON configuration management

๐ŸŽฌ Use Cases

Security & Surveillance

  • Home security monitoring
  • Office/warehouse surveillance
  • Perimeter intrusion detection
  • Automated security recording

Smart Home Integration

  • Motion-activated lighting systems
  • Automated recording for events
  • Pet/wildlife monitoring
  • Visitor detection and logging

Research & Development

  • Computer vision algorithm testing
  • Motion analysis research
  • Educational computer vision demonstrations
  • Prototype surveillance systems

๐Ÿšง Troubleshooting

Common Issues

"Could not open camera!" error

  • Ensure webcam is connected and recognized by Windows
  • Close other applications using the camera (Teams, Skype, etc.)
  • Try different camera device index in config.json

Poor motion detection accuracy

  • Adjust sensitivity slider (try values 20-50)
  • Reset background when lighting changes
  • Ensure stable camera mounting (reduce vibrations)
  • Check for adequate lighting conditions

Performance issues

  • Close unnecessary applications
  • Reduce video resolution in config.json
  • Check available system memory

๐Ÿ”ฎ Future Enhancements

Planned Features

  • Multiple camera support
  • Email/SMS notifications
  • Web-based remote monitoring
  • Mobile app companion
  • Object tracking and classification
  • Face recognition integration

Advanced Capabilities

  • Machine learning-based detection
  • Cloud storage integration
  • Motion pattern analysis
  • Automated alert systems
  • Integration with home automation platforms

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.

Development Setup

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes and test thoroughly
  4. Submit a pull request with detailed description

๐Ÿ“„ License

This project is open source and available under the MIT License.

๐Ÿ™ Acknowledgments

  • OpenCvSharp: Excellent .NET wrapper for OpenCV
  • OpenCV Community: Amazing computer vision library
  • Microsoft: .NET framework and development tools

Built with โค๏ธ using C# and OpenCV

About

A real-time movement detection application built with C# and OpenCvSharp that provides comprehensive motion monitoring and automatic video recording capabilities.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published