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

webcam-face-detector

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webcam-face-detector

Face and pupil detection library

0.0.6-alpha
alpha
latest
Source
npm
Version published
Weekly downloads
5
66.67%
Maintainers
0
Weekly downloads
 
Created
Source

Face Detector Library

A framework-agnostic face detection library.

Installation

Via NPM

npm install webcam-face-detector
import { FaceDetector } from 'webcam-face-detector';

Via CDN

<script src="https://unpkg.com/webcam-face-detector/dist/webcam-face-detector.umd.js"></script>

Configuration Options

OptionTypeDefaultDescription
timeoutDurationnumber3000Duration in milliseconds before triggering timeout callbacks
captureIntervalnumber2000Interval in milliseconds between image captures
onFaceDetectedfunction() => {}Callback when face is detected, receives detection data
onFaceTimeoutfunction() => {}Callback when face is not detected for timeoutDuration
onPupilDetectedfunction() => {}Callback when pupil is detected, receives coordinates and eye type
onPupilTimeoutfunction() => {}Callback when pupils are not detected for timeoutDuration
onInitfunction() => {}Callback when detector is initialized
onImageCapturedfunction() => {}Callback when an image is captured, receives imageData object with dataUrl, timestamp, format, width, and height
showFaceCirclebooleantrueShow/hide face detection circle
showPupilPointsbooleantrueShow/hide pupil detection points
faceCircleColorstring'#ff0000'Color of face detection circle
pupilPointsColorstring'#ff0000'Color of pupil detection points
faceCircleLineWidthnumber3Line width of face detection circle
pupilPointsLineWidthnumber3Line width of pupil detection points
detectionModestring'both'Detection mode: 'face', 'pupil', or 'both'
resourcesobject{ facefinder: './resources/facefinder.bin', puploc: './resources/puploc.bin' }Paths to required detection model files

Usage

Vanilla JS

<!DOCTYPE html>
<html>
<head>
    <title>Face Detector Demo</title>
    <style>
        .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 20px;
        }
        video {
            display: none;
        }
        canvas {
            border: 2px solid #333;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Face Detector Demo</h1>
        <video id="video"></video>
        <canvas id="canvas" width="640" height="480"></canvas>
    </div>
    
    <script src="https://unpkg.com/webcam-face-detector/dist/webcam-face-detector.umd.js"></script>
    <script>
        async function initFaceDetector() {
            const detector = new FaceDetector({
                timeoutDuration: 3000, // 
                captureInterval: 2000, // take a picture every 2 second
                onFaceDetected: (detection) => console.log(detection),
                showFaceCircle: true,    // optional: show/hide face circle
                showPupilPoints: true,   // optional: show/hide pupil points
                detectionMode: 'both',
                resources: {
                    facefinder : 'https://cdn.jsdelivr.net/gh/saifulriza/face-detector@main/src/resources/facefinder.bin',
                    puploc :'https://cdn.jsdelivr.net/gh/saifulriza/face-detector@main/src/resources/puploc.bin'
                },
                 onImageCaptured: (imageData) => {
                    console.log('image taken:', imageData.dataUrl);
                    // imageData memiliki: dataUrl, timestamp, format, width, height  
                },
            });

            const video = document.getElementById('video');
            const canvas = document.getElementById('canvas');

            try {
                const stream = await navigator.mediaDevices.getUserMedia({ video: true });
                video.srcObject = stream;
                await video.play();
                
                await detector.init(canvas);
                detector.startCapturing(video);
                detector.start(video);
            } catch (err) {
                console.error('Error:', err);
            }
        }

        // Start the face detector when page loads
        window.addEventListener('load', initFaceDetector);
    </script>
</body>
</html>

React Example

import { useEffect, useRef } from 'react';

function FaceDetectorComponent() {
    const canvasRef = useRef(null);
    const videoRef = useRef(null);

    useEffect(() => {
        const detector = new FaceDetector({
            onFaceDetected: (detection) => console.log(detection),
            showFaceCircle: true,    // optional: show/hide face circle
            showPupilPoints: false   // optional: show/hide pupil points
        });

        async function initDetector() {
            try {
                const stream = await navigator.mediaDevices.getUserMedia({ video: true });
                videoRef.current.srcObject = stream;
                await videoRef.current.play();
                
                await detector.init(canvasRef.current);
                // detector.startCapturing(videoRef);
                detector.start(videoRef.current);
            } catch (err) {
                console.error('Error:', err);
            }
        }

        initDetector();

        return () => detector.stop();
    }, []);

    return (
        <div>
            <video ref={videoRef} style={{ display: 'none' }} />
            <canvas ref={canvasRef} width={640} height={480} />
        </div>
    );
}

Vue Example

<template>
    <div>
        <video ref="video" style="display: none"></video>
        <canvas ref="canvas" width="640" height="480"></canvas>
    </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';

const video = ref(null);
const canvas = ref(null);
let detector;

onMounted(async () => {
    detector = new FaceDetector({
        onFaceDetected: (detection) => console.log(detection),
        showFaceCircle: true,    // optional: show/hide face circle
        showPupilPoints: false   // optional: show/hide pupil points
    });

    try {
        const stream = await navigator.mediaDevices.getUserMedia({ video: true });
        video.value.srcObject = stream;
        await video.value.play();
        
        await detector.init(canvas.value);
        // detector.startCapturing(video.value)
        detector.start(video.value);
    } catch (err) {
        console.error('Error:', err);
    }
});

onBeforeUnmount(() => {
    if (detector) {
        detector.stop();
    }
});
</script>

Keywords

face-detection

FAQs

Package last updated on 08 Mar 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