detection-lib
A modular JavaScript detection library for face, QR, and future detection types with a clean, unified API.
Features
- Pluggable Detectors — Start with face detection and extend easily with QR, object, etc.(currently only have face).
- Unified Interface — Every detector implements the same async
detect(input)
method.
- Factory Pattern — Use
createDetector({ type })
to initialize the correct implementation.
- Works in Browser — Supports
<video>
, <canvas>
, and <img>
HTML elements.
Installation
This library currently runs in the browser using ES modules.
Installation
npm install detection-lib
Usage
import { createDetector } from 'detection-lib';
// Create a face detector
const detector = await createDetector({ type: 'face' });
// Detect faces in a video element
const result = await detector.detect(videoElement);
if (result.type === 'face' && result.boxes) {
result.boxes.forEach(box => {
// Draw box on canvas, etc.
console.log(box);
});
}
API
createDetector(options)
- options.type: 'face' | 'qr' | string — The type of detector to create.
- Returns a detector instance with an async detect(input) method
Detector Interface
All detectors implement:
async detect(input: HTMLVideoElement | HTMLImageElement | HTMLCanvasElement): Promise<DetectionResult>
DetectionResult
- type: string — The detector type ('face', 'qr', etc.)
- boxes: Array of { x, y, w, h, score? } (for face, etc.)
- data: Any extra data (for QR, etc.)
Extending
To add a new detector:
1. Create a new file in src/detectors/ (e.g., myDetector.js).
2. Implement a class with a detect() method.
3. Register it in the factory in src/DetectorFactory.js.