
Product
Socket Now Protects the Chrome Extension Ecosystem
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
Face detection and recognition library using TensorFlow.js and MediaPipe along with helpful React hooks and components
A high-performance TypeScript library for face detection and recognition, optimized for in-browser execution with native GPU and CPU support.
Also has helpful React hooks and components for face detection and recognition.
npm install facenet-js
or with yarn:
yarn add facenet-js
import { FaceDetector } from 'facenet-js';
// Create a face detector instance with GPU acceleration (or CPU fallback)
const detector = new FaceDetector({
device: 'GPU', // 'GPU' for WebGL/WebGPU acceleration, 'CPU' for compatibility
mode: 'IMAGE', // 'IMAGE' for photos, 'VIDEO' for real-time streams
minDetectionConfidence: 0.5,
embeddingModelPath: '/models/facenet.tflite' // Path to FaceNet model
});
// Initialize the detector (loads models into browser memory)
await detector.initialize();
// Detect faces in an image
const imageElement = document.getElementById('myImage') as HTMLImageElement;
const detections = await detector.detectFromImage(imageElement);
// Get face embeddings for recognition
if (detections.length > 0) {
const embedding = await detector.embed({
source: imageElement,
detection: detections[0]
});
}
import { ImageFaceDetectorProvider, useFaceDetector, useFaceSimilarity } from 'facenet-js/react';
function App() {
return (
<ImageFaceDetectorProvider
options={{
device: 'GPU',
minDetectionConfidence: 0.5,
embeddingModelPath: '/models/facenet.tflite'
}}
>
<FaceDetectionComponent />
</ImageFaceDetectorProvider>
);
}
function FaceDetectionComponent() {
const { faceDetector, isLoading, error } = useFaceDetector();
// The face detector is automatically initialized by the provider
// Use faceDetector.detectFromImage() or faceDetector.detectFromVideo()
}
FaceDetector
The main class for face detection and recognition.
new FaceDetector(options: FaceDetectionOptions)
Options:
device
: 'CPU'
| 'GPU'
- Select computation device
'GPU'
: Leverages WebGL/WebGPU for maximum performance'CPU'
: Fallback option for broader compatibilitymode
: 'IMAGE'
| 'VIDEO'
- Detection mode
'IMAGE'
: Optimized for static images'VIDEO'
: Optimized for real-time video streamsminDetectionConfidence
: number
- Minimum confidence threshold (0-1)embeddingModelPath
: string
- Path to the FaceNet embedding model (required for face recognition)detectionModelPath?
: string
- Optional custom path to face detection modelwasmPath?
: string
- Optional custom path to WASM filesinitialize(): Promise<void>
Initializes and loads the face detection models into browser memory. Must be called before detection.
detectFromImage(imageElement: HTMLImageElement): Promise<Detection[]>
Detects faces in a static image.
detectFromVideo(videoElement: HTMLVideoElement, timestamp: number): Promise<Detection[]>
Detects faces in a video frame.
embed(request: EmbeddingRequest): Promise<ImageEmbedderResult | null>
Generates face embeddings for recognition.
EmbeddingRequest:
{
source: HTMLImageElement | HTMLVideoElement;
detection: Detection;
timestamp?: number; // Required for video
}
ImageFaceDetectorProvider
A React context provider for image-based face detection.
<ImageFaceDetectorProvider options={options}>
{children}
</ImageFaceDetectorProvider>
VideoFaceDetectorProvider
A React context provider for video-based face detection.
<VideoFaceDetectorProvider options={options}>
{children}
</VideoFaceDetectorProvider>
useFaceDetector()
Access the face detector instance and its state.
const { faceDetector, isLoading, error } = useFaceDetector();
Returns:
faceDetector
: The FaceDetector instanceisLoading
: Boolean indicating if models are loadingerror
: Error object if initialization faileduseFaceSimilarity(a, b, threshold?)
Calculate similarity between two face embeddings.
const similarity = useFaceSimilarity(embedding1, embedding2, 0.5);
Returns:
similarity
: Cosine similarity score (-1 to 1)isMatch
: Boolean indicating if faces match (similarity > threshold)message
: Human-readable similarity messageuseWebcam()
Manage webcam access for face detection.
const { videoRef, isActive, error, startWebcam, stopWebcam } = useWebcam();
import { FaceDetector } from 'facenet-js';
async function detectFaces() {
const detector = new FaceDetector({
device: 'GPU',
mode: 'IMAGE',
minDetectionConfidence: 0.7,
embeddingModelPath: '/models/facenet.tflite'
});
await detector.initialize();
const img = document.querySelector('#photo') as HTMLImageElement;
const faces = await detector.detectFromImage(img);
console.log(`Found ${faces.length} faces`);
faces.forEach((face, index) => {
console.log(`Face ${index + 1}:`, {
confidence: face.score,
boundingBox: face.boundingBox
});
});
}
import { FaceDetector } from 'facenet-js';
async function startVideoDetection() {
const detector = new FaceDetector({
device: 'GPU',
mode: 'VIDEO',
minDetectionConfidence: 0.5,
embeddingModelPath: '/models/facenet.tflite'
});
await detector.initialize();
const video = document.querySelector('#webcam') as HTMLVideoElement;
// Detection loop
async function detect() {
const faces = await detector.detectFromVideo(video, performance.now());
// Process detected faces
faces.forEach(face => {
// Draw bounding boxes, etc.
});
requestAnimationFrame(detect);
}
detect();
}
import { FaceDetector } from 'facenet-js';
async function compareFaces(img1: HTMLImageElement, img2: HTMLImageElement) {
const detector = new FaceDetector({
device: 'GPU',
mode: 'IMAGE',
minDetectionConfidence: 0.5,
embeddingModelPath: '/models/facenet.tflite'
});
await detector.initialize();
// Detect faces in both images
const faces1 = await detector.detectFromImage(img1);
const faces2 = await detector.detectFromImage(img2);
if (faces1.length === 0 || faces2.length === 0) {
console.log('No faces detected');
return;
}
// Get embeddings
const embedding1 = await detector.embed({
source: img1,
detection: faces1[0]
});
const embedding2 = await detector.embed({
source: img2,
detection: faces2[0]
});
if (embedding1 && embedding2) {
// Calculate cosine similarity
const similarity = cosineSimilarity(
embedding1.embeddings[0],
embedding2.embeddings[0]
);
console.log(`Face similarity: ${similarity}`);
}
}
function cosineSimilarity(a: Float32Array, b: Float32Array): number {
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
import { ImageFaceDetectorProvider, VideoFaceDetectorProvider, useFaceDetector, useFaceSimilarity } from 'facenet-js/react';
import { useState } from 'react';
function FaceComparisonApp() {
const [imageEmbedding, setImageEmbedding] = useState(null);
const [videoEmbedding, setVideoEmbedding] = useState(null);
const similarity = useFaceSimilarity(imageEmbedding, videoEmbedding, 0.5);
return (
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '20px' }}>
<ImageFaceDetectorProvider
options={{
device: 'GPU',
minDetectionConfidence: 0.5,
embeddingModelPath: '/models/facenet.tflite'
}}
>
<ImageUploader onEmbedding={setImageEmbedding} />
</ImageFaceDetectorProvider>
<VideoFaceDetectorProvider
options={{
device: 'GPU',
minDetectionConfidence: 0.5,
embeddingModelPath: '/models/facenet.tflite'
}}
>
<WebcamDetector onEmbedding={setVideoEmbedding} />
</VideoFaceDetectorProvider>
{similarity && (
<div style={{ gridColumn: 'span 2', textAlign: 'center' }}>
<h3>{similarity.isMatch ? '✅ Match!' : '❌ No Match'}</h3>
<p>{similarity.message}</p>
</div>
)}
</div>
);
}
This library uses:
Note: The FaceNet model file needs to be hosted and accessible. Place it in your public directory as /models/facenet.tflite
.
FaceNet.js is designed as a browser-first library with excellent cross-browser compatibility:
Performance Notes:
# Install dependencies
npm install
# Build the library
npm run build
# Run in development mode
npm run dev
# Run tests
npm test
# Lint code
npm run lint
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
Face detection and recognition library using TensorFlow.js and MediaPipe along with helpful React hooks and components
The npm package facenet-js receives a total of 3 weekly downloads. As such, facenet-js popularity was classified as not popular.
We found that facenet-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.