Face Detection with React, TensorFlow.js, and Webcam

๐ง Real-Time Face Detection in the Browser with TensorFlow.js and CocoSSD
๐ Introduction
This project demonstrates how to perform real-time object detection โ including basic face/person detection โ directly in the browser using TensorFlow.js and the CocoSSD model. By accessing the user's webcam and running inference locally, the application respects privacy and requires no backend server.
๐ค What Is CocoSSD?
CocoSSD is a pre-trained object detection model trained on the COCO dataset, which includes 90 classes like:
- Person
- Bicycle
- Car
- Dog
- Chair
While itโs not a dedicated face detection model, it can reliably identify people in camera frames, making it suitable for simple face detection use cases.
๐ง Why TensorFlow.js?
TensorFlow.js enables running ML models in the browser using JavaScript. It offers:
- No backend required
- GPU acceleration (WebGL)
- Real-time predictions
- Privacy โ all processing is local to the user's device
๐ธ Webcam Integration
Using the navigator.mediaDevices.getUserMedia() API, we stream video from the userโs webcam into a <video> element.
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
video.srcObject = stream;
});
The video is then used as input to the TensorFlow model for continuous inference.
๐งช Real-Time Detection Workflow
-
Load the CocoSSD model:
const model = await cocoSsd.load(); -
Run detection on each frame:
model.detect(video).then(predictions => { // Draw bounding boxes }); -
Draw the results on a
<canvas>overlay.
๐ Technologies Used
- TensorFlow.js (via CDN)
- CocoSSD object detection model
- JavaScript / HTML5
<video>and<canvas>APIs- nodejs
- ReactJS
๐ How to Run
-
Clone the repo:
git clone https://github.com/erevos-13/coco-ssd-img cd coco-ssd-img npm i -
Serve the app locally:
#nodejs npm run dev -
Open the browser and allow webcam access.
๐ก Limitations
The Coco-SSD model in TensorFlow (often used via TensorFlow.js) is a lightweight object detection model trained on the COCO dataset. While it's convenient and works well for many use cases, it comes with several limitations:
๐น 1. Limited Object Classes
- Only 90 object categories: Itโs trained on the COCO dataset which includes common objects like person, car, dog, bicycle, etc.
- Cannot detect custom objects unless you retrain or fine-tune another model on a different dataset.
๐น 2. Lower Accuracy Compared to Heavier Models
- Coco-SSD uses a MobileNet backbone, which trades off detection accuracy for speed.
- It's less accurate than heavier models like Faster R-CNN, YOLOv4, or EfficientDetโespecially in complex or cluttered scenes.
๐น 3. No Fine-Grained Detection
- The model struggles with small objects, overlapping instances, and fine-grained classification (e.g., different dog breeds).
- Bounding boxes can be less precise than anchor-based or two-stage detectors.
๐น 4. Single Scale Detection
- It lacks multi-scale feature fusion (unlike SSD with FPN or YOLOv5), which affects performance on varying object sizes.
๐น 5. Limited Edge Performance
- While itโs relatively light, real-time inference can still be too slow on very low-end hardware (e.g., old smartphones or Raspberry Pi without acceleration).
๐น 6. No Instance Segmentation
- It provides bounding boxes only, not pixel-level masks like models from the Mask R-CNN family.
๐น 7. No Training Support in TF.js
- TensorFlow.js version supports inference onlyโyou can't train or fine-tune the model in-browser.
When to Use Coco-SSD
Use Coco-SSD when:
- You need fast and reasonably accurate object detection.
- You're building a web-based or lightweight mobile app.
- Your objects fall within the 90 COCO categories.
Avoid it when:
- You need high accuracy, custom objects, or segmentation.
Would you like alternatives or guidance on how to retrain a custom object detector using TensorFlow?
For better face-focused features, consider using:
๐ Conclusion
This project is a simple but powerful demonstration of whatโs possible with machine learning in the browser. Using just JavaScript and TensorFlow.js, we built a real-time face/person detection tool that runs on any modern device โ no installation, no servers, no compromise on privacy.