🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

detection-lib

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

detection-lib

> A modular JavaScript detection library for face, QR, and future detection types with a clean, unified API.

1.0.20
latest
npm
Version published
Weekly downloads
275
-57.63%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Description

It is a modular JavaScript face detection library built on top of MediaPipe Face Detection. It provides an easy-to-use interface to detect faces from various input sources like video, image, or canvas, with built-in initialization, error handling, and bounding box extraction.

How it Works

currently only face detection part is implemented
Internally, the library uses @mediapipe/face_detection to detect faces. The workflow is:

  • Load the detector (loads the MediaPipe model).
import { createDetector } from 'detection-lib';
const detector = await createDetector({ type: 'face' });
  • Initialize it (it send dummy static image so that all files needed are loaded).
await detector.initialize();
  • Run Detection on any HTML media element (input can be any image,HTMLCanvasElement,HTMLImageElement).
 const result = await detectorRef.current.detect(input);
  • Receive Results as an object with:
  • status: Numeric status code

  • message: Human-readable string

  • boxes: Array of bounding boxes with x, y, w, h, and score

Installation

This library currently runs in the browser using ES modules.

Installation

npm install detection-lib

Usage

import { useEffect, useRef, useState } from 'react';
import { createDetector } from 'detection-lib';

// Create references and state
const detectorRef = useRef(null);
const [detectorCreated, setDetectorCreated] = useState(false);
const [detectorReady, setDetectorReady] = useState(false);

// Step 1: Create the detector when component mounts
useEffect(() => {
  const create = async () => {
    const detector = await createDetector({ type: 'face' });
    detectorRef.current = detector;
    setDetectorCreated(true);
  };
  create();

  // Cleanup on unmount
  return () => {
    setDetectorCreated(false);
    detectorRef.current = null;
  };
}, []);

// Step 2: Initialize the detector once created
useEffect(() => {
  const initialize = async () => {
    if (detectorCreated && detectorRef.current) {
      await detectorRef.current.initialize();
      setDetectorReady(true);
    }
  };
  initialize();

  // Cleanup
  return () => setDetectorReady(false);
}, [detectorCreated]);

// Step 3: Run detection (call this function after detector is ready)
const runDetection = async (input) => {
  if (detectorReady && detectorRef.current) {
    const result = await detectorRef.current.detect(input);

    if (result.type === 'face' && result.boxes) {
      result.boxes.forEach((box) => {
        // Example: Draw box or process coordinates
        console.log('Detected Face Box:', box);
      });
    }
  }
};

Output Format

{
  status: 200, // or other status code
  message: 'OK', // or descriptive error
  boxes: [
    {
      x: Number, // top-left x
      y: Number, // top-left y
      w: Number, // width
      h: Number, // height
      score: Number // confidence score (optional)
    },
    ...
  ]
}

status Codes

CodeMessageDescription
2000Single face detectedOK
2004No face detectedNo face was found in the frame
2002Multiple faces detectedMore than one face detected
4010FaceDetector not initializedYou must call .initialize() first
5000FaceDetector model errorAn error occurred while running the model
4015Image load errorAn error occurred while loading image

Detector Interface

All detectors implement:

async detect(input: HTMLVideoElement | HTMLImageElement | HTMLCanvasElement): Promise<DetectionResult>

Internals

  • Uses @mediapipe/face_detection under the hood

  • Loads model assets from jsDelivr CDN

  • Initialization uses a built-in static image to "warm up" the model

  • Implements result caching to optimize repeated calls

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.

Keywords

face

FAQs

Package last updated on 19 Jun 2025

Did you know?

Socket

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.

Install

Related posts