Skip to content

CodeRafay/realtime-object-detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎯 Real-Time Object Detection Web App

A high-performance, privacy-first web application for real-time object detection that runs entirely in your browser using WebGPU acceleration and ONNXRuntime-Web. No server required, no data leaves your device.

React ONNX Runtime

✨ Features

  • πŸš€ High-Speed Inference: 30-60 FPS on laptops, 15-30 FPS on modern mobile devices
  • πŸ”’ Complete Privacy: All processing happens locally, zero data transmission
  • ⚑ WebGPU Accelerated: Leverages GPU for maximum performance
  • πŸ“± Mobile Compatible: Works on both desktop and mobile browsers
  • πŸŽ₯ Multi-Camera Support: Switch between front/back cameras
  • 🎨 Real-Time Visualization: Live bounding boxes with class labels and confidence scores
  • πŸ“Š Performance Monitoring: Real-time FPS counter and execution provider display
  • 🌐 Offline Capable: Works without internet after initial load

πŸ—οΈ Architecture

Technology Stack

  • Frontend Framework: React 18.2 with Hooks
  • Build Tool: Vite 5.0 (fast HMR and optimized builds)
  • AI Runtime: ONNX Runtime Web 1.17
  • Execution Provider: WebGPU (with WebAssembly fallback)
  • Model: YOLOv8n (80 COCO classes)

Project Structure

realTimeOD/
β”œβ”€β”€ public/
β”‚   └── models/
β”‚       └── yolov8n.onnx          # Place your ONNX model here
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ App.jsx               # Main application component
β”‚   β”‚   β”œβ”€β”€ CameraView.jsx        # Video display with overlays
β”‚   β”‚   β”œβ”€β”€ BoundingBoxCanvas.jsx # Detection visualization
β”‚   β”‚   β”œβ”€β”€ Controls.jsx          # UI controls
β”‚   β”‚   └── FPSMonitor.jsx        # Performance metrics
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”œβ”€β”€ useCamera.js          # Camera management logic
β”‚   β”‚   └── useObjectDetection.js # Detection pipeline logic
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ constants.js          # Configuration and constants
β”‚   β”‚   β”œβ”€β”€ modelLoader.js        # ONNX model loading
β”‚   β”‚   β”œβ”€β”€ preprocessing.js      # Image preprocessing
β”‚   β”‚   └── postprocessing.js     # NMS and output processing
β”‚   β”œβ”€β”€ main.jsx                  # Application entry point
β”‚   └── index.css                 # Global styles
β”œβ”€β”€ index.html
β”œβ”€β”€ package.json
β”œβ”€β”€ vite.config.js
└── README.md

πŸš€ Quick Start

Prerequisites

  • Node.js 18+ installed
  • Modern browser with WebGPU support (Chrome 113+, Edge 113+)
  • YOLOv8n ONNX model file

Installation

  1. Clone or navigate to the project directory:

    cd realTimeOD
  2. Install dependencies:

    npm install
  3. Download the YOLOv8n ONNX model:

    You can export YOLOv8n to ONNX format using the Ultralytics library:

    pip install ultralytics
    from ultralytics import YOLO
    
    # Load YOLOv8n model
    model = YOLO('yolov8n.pt')
    
    # Export to ONNX format
    model.export(format='onnx', opset=12, simplify=True, dynamic=False, imgsz=640)

    Then move the generated yolov8n.onnx file to public/models/:

    mkdir -p public/models
    mv yolov8n.onnx public/models/
  4. Start the development server:

    npm run dev
  5. Open your browser and navigate to http://localhost:3000

First-Time Usage

  1. Allow camera access when prompted
  2. Click "Load Model" to initialize the ONNX model
  3. Click "Start Detection" to begin real-time object detection
  4. Use the camera selector to switch between devices
  5. Monitor FPS and detection count in the sidebar

πŸŽ›οΈ Configuration

Model Configuration

Edit src/utils/constants.js to adjust detection parameters:

export const MODEL_CONFIG = {
  modelPath: "/models/yolov8n.onnx",
  inputWidth: 640,
  inputHeight: 640,
  confidenceThreshold: 0.25, // Minimum confidence to show detections
  iouThreshold: 0.45, // IoU threshold for NMS
  maxDetections: 100, // Maximum number of detections
};

Camera Configuration

Adjust camera settings in src/utils/constants.js:

export const CAMERA_CONFIG = {
  defaultWidth: 640,
  defaultHeight: 480,
  idealWidth: 1280,
  idealHeight: 720,
  facingMode: "user", // 'user' for front, 'environment' for back
};

πŸ“¦ Building for Production

  1. Build the application:

    npm run build
  2. Preview the production build:

    npm run preview

The optimized build will be in the dist/ directory, ready for deployment.

🌐 Deployment

Static Hosting (Recommended)

Deploy to any static hosting service:

  • Netlify: Drag and drop dist/ folder or connect Git repo
  • Vercel: vercel deploy
  • GitHub Pages: Push dist/ to gh-pages branch
  • Cloudflare Pages: Connect repo and deploy

Important Deployment Notes

  1. HTTPS Required: Camera access requires HTTPS in production
  2. Headers for ONNX Runtime: Some hosts may need CORS headers configured
  3. Model File: Ensure yolov8n.onnx is included in deployment

Example Netlify Configuration

Create netlify.toml:

[build]
  command = "npm run build"
  publish = "dist"

[[headers]]
  for = "/*"
  [headers.values]
    Cross-Origin-Embedder-Policy = "require-corp"
    Cross-Origin-Opener-Policy = "same-origin"

πŸ§ͺ Testing Different Models

To use a different YOLO model:

  1. Export your model to ONNX format with input size 640x640
  2. Place it in public/models/
  3. Update modelPath in src/utils/constants.js
  4. Adjust output processing if using different YOLO version

πŸ› Troubleshooting

Camera Not Working

  • Ensure you're using HTTPS (or localhost)
  • Check browser permissions for camera access
  • Try a different browser

Model Not Loading

  • Verify yolov8n.onnx exists in public/models/
  • Check browser console for error messages
  • Ensure model was exported correctly

Low FPS

  • Check if WebGPU is enabled (see execution provider in UI)
  • Try reducing camera resolution
  • Close other GPU-intensive applications
  • Consider using a lighter model (YOLOv8n is already the smallest)

WebGPU Not Available

The app automatically falls back to WebAssembly. Performance will be lower but still functional.

To enable WebGPU:

  • Chrome/Edge: Should be enabled by default in version 113+
  • Firefox: Not yet supported (use WASM fallback)
  • Safari: Limited support, may need enabling in settings

🎨 Customization

Adding Custom Classes

Edit src/utils/constants.js to modify the class list:

export const COCO_CLASSES = [
  "person",
  "bicycle",
  "car", // ... your classes
];

Changing Colors

Modify the color palette in src/utils/constants.js:

export const COLORS = [
  "#FF6B6B",
  "#4ECDC4", // ... your colors
];

Styling

All component styles are in separate CSS files:

  • src/App.css - Main layout
  • src/components/*.css - Component-specific styles

πŸ“Š Performance Optimization Tips

  1. Reduce Input Resolution: Lower resolution = faster inference
  2. Adjust Confidence Threshold: Higher threshold = fewer detections to process
  3. Use OffscreenCanvas: Automatically used when available
  4. Optimize Camera Settings: Lower camera resolution if not needed
  5. Close Unnecessary Tabs: Free up GPU resources

πŸ”’ Privacy & Security

  • No Data Collection: Nothing is sent to any server
  • Local Processing: All inference happens on your device
  • No Cookies: No tracking or analytics
  • Open Source: Fully inspectable code

πŸ“„ License

Apache 2.0 License - feel free to use this project for any purpose.

Acknowledgments

πŸ“ž Support

For issues, questions, or contributions:

  • Open an issue on GitHub
  • Check existing documentation
  • Review troubleshooting section

πŸš€ Future Enhancements

Potential improvements:

  • Multiple model support (YOLOv8s, YOLOv8m, etc.)
  • Video file upload support
  • Screenshot/recording functionality
  • Detection statistics and analytics
  • Custom model training integration
  • Pose estimation support
  • Segmentation support

Built with ❀️ for privacy-conscious AI applications

About

Real-time object detection web app with WebGPU using YoLOv8 for detecting objects and drawing bounded boxes with confidence score

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors