A production-ready React + TypeScript web application that uses Google MediaPipe for real-time object detection via webcam. Built with Vite and Bun for optimal performance.
- π₯ Real-time Webcam Detection: Uses native
getUserMediaAPI for webcam access - π€ AI-Powered Object Detection: Leverages MediaPipe's efficient object detection models
- β‘ On-Device Processing: All AI processing happens locally in the browser
- π¨ Modern UI: Clean, responsive interface with Tailwind CSS
- π± Mobile Friendly: Responsive design that works on all devices
- π§ TypeScript: Full type safety and better development experience
- Frontend: React 19 + TypeScript
- Build Tool: Vite
- Runtime: Bun
- AI Framework: MediaPipe Tasks Vision
- Styling: Tailwind CSS
- Webcam: Native
getUserMediaAPI
- Bun installed on your system
- Modern web browser with webcam support
- HTTPS connection (required for webcam access in production)
- Clone the repository:
git clone <your-repo-url>
cd machine-learning- Install dependencies:
bun install- Start the development server:
bun run dev- Open your browser and navigate to
http://localhost:5173
- Load the Model: The app automatically loads the MediaPipe object detection model
- Start Webcam: Click the "Search with camera" button to activate your webcam
- Detect Objects: Point your camera at objects to see real-time detection results
- View Results: See detected objects with confidence scores displayed below the video
src/
βββ components/
β βββ ObjectDetectionApp.tsx # Main app component
β βββ VideoCanvasDisplay.tsx # Video and canvas display
β βββ DetectionResults.tsx # Detection results display
βββ hooks/
β βββ useMediaPipeObjectDetection.ts # Custom hook for MediaPipe logic
βββ utils/
β βββ drawDetections.ts # Canvas drawing utilities
β βββ initializeObjectDetector.ts # MediaPipe initialization
β βββ detectObjectsAndDraw.ts # Detection and drawing logic
β βββ cleanupMediaPipeResources.ts # Resource cleanup
βββ App.tsx # Root component
- Model Initialization: The app loads MediaPipe's object detection model from Google's CDN
- Webcam Access: Uses the
getUserMediaAPI to access the user's webcam - Real-time Processing: Continuously processes video frames for object detection
- Canvas Overlay: Draws detection boxes and labels on an HTML canvas overlaid on the video
- Results Display: Shows detected objects with confidence scores in a clean list format
The app uses MediaPipe's EfficientDet Lite0 model, which provides:
- Fast inference speed suitable for real-time applications
- Good accuracy for common object detection tasks
- Small model size for quick loading
- Support for 90+ object categories
- Chrome 88+
- Firefox 85+
- Safari 14+
- Edge 88+
- Model Loading: Initial model download may take a few seconds
- Frame Rate: Detection runs at browser's refresh rate (typically 60fps)
- Memory Usage: Efficient memory management with proper cleanup
- CPU Usage: Optimized for modern devices with hardware acceleration
To use a different MediaPipe model, update the MODEL_PATH in src/App.tsx:
const MODEL_PATH =
"https://storage.googleapis.com/mediapipe-models/your-model-path";Modify the detection parameters in src/hooks/useMediaPipeObjectDetection.ts:
objectDetectorRef.current = await initializeObjectDetector(
modelPath,
0.5, // scoreThreshold - higher = more confident detections
5 // maxResults - maximum number of objects to detect
);The app uses Tailwind CSS for styling. Customize the appearance by modifying the component files or extending the Tailwind configuration.
- Ensure you're using HTTPS (required for webcam access)
- Check browser permissions for camera access
- Try refreshing the page and granting permissions again
- Check your internet connection
- Verify the model URL is accessible
- Check browser console for error messages
- Close other browser tabs to free up resources
- Ensure hardware acceleration is enabled
- Try reducing the video resolution in the webcam constraints
bun run dev- Start development serverbun run build- Build for productionbun run serve- Preview production buildbun run lint- Run ESLint
- Create new components in the
components/directory - Add custom hooks in the
hooks/directory - Implement utility functions in the
utils/directory - Update types and interfaces as needed
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Google MediaPipe for the AI framework
- LogRocket Blog for the implementation guide
- Vite for the build tool
- Bun for the runtime environment