A sophisticated real-time virtual try-on platform that enables users to preview eyewear frames using advanced AI-powered facial recognition technology. Built with MediaPipe Face Mesh and Flask, this application delivers an immersive and accurate try-on experience directly in the browser.
- Real-Time Face Tracking: Utilizes MediaPipe Face Mesh for precise facial landmark detection
- Live Frame Overlay: Seamless overlay of eyewear frames that automatically adjust to facial movements
- Background Removal: Advanced OpenCV-based background removal for transparent frame rendering
- Responsive Positioning: Intelligent frame scaling and positioning based on eye distance and facial geometry
- Smooth Animations: EMA (Exponential Moving Average) smoothing for stable and natural frame tracking
- Multiple Frame Selection: Browse and switch between various eyewear styles instantly
- Camera Controls: Flip camera view for optimal positioning
- Screenshot Capture: Save your try-on results as PNG images
- Cross-Browser Support: Works on modern browsers without any plugin installation
- Responsive Design: Optimized for desktop and mobile devices
- Client-Side Processing: Face detection runs directly in the browser for privacy
- Server-Side Image Processing: Flask backend handles complex background removal
- CORS-Enabled API: Secure cross-origin resource sharing
- Classic UI/UX: Elegant, modest design with smooth transitions and professional aesthetics
- HTML5/CSS3: Modern semantic markup with advanced CSS gradients and animations
- JavaScript (ES6+): Asynchronous operations and DOM manipulation
- MediaPipe Face Mesh: Google's ML solution for facial landmark detection
- Canvas API: Real-time frame rendering and overlay composition
- Python 3.x: Server-side scripting
- Flask: Lightweight WSGI web application framework
- OpenCV: Computer vision library for image processing
- NumPy: Numerical computing for efficient array operations
- Pillow (PIL): Python Imaging Library for image manipulation
- Flask-CORS: Cross-Origin Resource Sharing support
- Python 3.7 or higher
- Modern web browser (Chrome, Firefox, Safari, Edge)
- Webcam access
- Internet connection (for MediaPipe CDN resources)
git clone https://github.com/yourusername/virtual-tryon-eyewear.git
cd virtual-tryon-eyewearpip install flask flask-cors opencv-python numpy pillowvision-try-eyewear/
βββ cv.py # Flask backend for background removal
βββ index.html # Main HTML structure
βββ style.css # Stylesheet with elegant design
βββ script.js # Frontend logic and MediaPipe integration
βββ frame1.png # Eyewear frame assets
βββ frame2.png
βββ frame3.png
βββ ... (additional frames)
βββ README.md # Project documentation
-
Start the Flask Backend
python cv.py
The Flask server will start on
http://127.0.0.1:5000 -
Launch the Frontend
- Open
index.htmlin a live server (e.g., VS Code Live Server) - Or access via
http://127.0.0.1:5500(default Live Server port)
- Open
-
Grant Camera Permissions
- Allow browser access to your webcam when prompted
- Select a Frame: Click on any frame thumbnail from the sidebar
- Position Your Face: Center your face in the camera preview
- Remove Background (Optional): Click "Make Selected Transparent" to remove white backgrounds from frames
- Adjust View: Use the "Flip Camera" button if needed
- Capture: Click "Capture Screenshot" to save your try-on image
Edit script.js to customize frame behavior:
const SMA_ALPHA = 0.25; // Smoothing factor (0-1)
const DEFAULT_SCALE = 2.5; // Frame width multiplier
const FRAME_VERTICAL_OFFSET = -0.15; // Vertical position adjustmentModify cv.py for different background removal sensitivity:
def remove_white_background(pil_image, threshold=240, edge_blur=3):
# threshold: 0-255 (higher = stricter white detection)
# edge_blur: smoothing amount (0-10)- User selects eyewear frame
- Camera feed initializes via getUserMedia API
- MediaPipe Face Mesh detects facial landmarks
- JavaScript calculates frame position and rotation
- Canvas overlay renders frame in real-time
- Frontend sends frame image to Flask endpoint
- Python converts image to NumPy array
- OpenCV processes image (background removal)
- Processed image encoded as base64 PNG
- JSON response returned to frontend
Removes background from eyewear frame images.
Request:
- Method:
POST - Content-Type:
multipart/form-data - Body:
frame(image file)
Response:
{
"imageBase64": "data:image/png;base64,iVBORw0KGgoAAAANS..."
}- Add frame images to the project directory (PNG/JPG format)
- Update
index.htmlwith new frame buttons:<button class="frame-thumb" data-src="your-frame.png" data-scale="2.5"> <img src="your-frame.png" alt="Frame Name"> </button>
- Edit
style.cssto customize colors, fonts, and animations - Color scheme uses CSS custom properties for easy theming
- Primary accent:
#e6b366(gold/bronze)
- No Data Collection: Camera feed processed locally in browser
- No Server Storage: Images not saved on backend
- Session-Only Processing: Background removal results cached in browser session
- CORS Protection: API restricted to authorized origins
- Ensure browser has camera permissions
- Check if another application is using the camera
- Try refreshing the page
- Verify Flask server is running on port 5000
- Check browser console for JavaScript errors
- Ensure frame image paths are correct
- Confirm Flask dependencies are installed
- Check server console for Python errors
- Verify image format is supported (PNG, JPG, AVIF)
- Face mesh runs at ~30 FPS on modern hardware
- Canvas rendering optimized for 60 FPS
- EMA smoothing reduces jitter without lag
- Background removal processed on-demand (not real-time)
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Vidushi Agarwal
- Google MediaPipe for facial landmark detection
- OpenCV community for computer vision tools
- Flask framework contributors
- Design inspiration from modern e-commerce platforms
For questions, suggestions, or support:
- Email: support@virtualtryon.com
- GitHub Issues: Project Issues
Made with β€οΈ for better online eyewear shopping experiences