npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-selfie

v0.1.38

Published

A simple selfie library

Downloads

649

Readme

Simple Selfie

Vanilla JS Demo: https://shvl.github.io/simple-selfie

Vanilla JS Demo AR (add glasses): https://shvl.github.io/simple-selfie?selfie-glasses

React Demo: https://github.com/shvl/react-simple-selfie

Selfie is a JavaScript library that adds selfie support to your website. It leverages modern face detection algorithms to provide a seamless user experience.

Key Features

  • Face Detection: Selfie.js uses the face-api.js library to detect faces in real-time. It can handle multiple faces at once, but focuses on the most prominent face for selfie purposes. See Face and Selfie classes for more details.

  • Face Size and Position Detection: The library can calculate the size and position of the detected face relative to the frame. It uses these calculations to ensure the face is properly centered and sized within the selfie frame. See the faceWidth and lastFaceFrame properties in the Selfie class.

  • Face Direction Detection: Selfie.js can determine the direction the face is looking at. This feature is implemented in the FaceDirection class and the IFaceDirection interface.

  • Overlay to Center Face: An overlay is provided to guide the user to center their face within the frame. The overlay visibility is controlled based on the deviation of the face position from the center of the frame. See the overlayVisible property in the Selfie class.

  • Selfie Capture: The library can capture a selfie when the user is centered within the frame. The captured selfie can be procssed with various filters and effects. See processors to crop and resize the selfie, detect blur with laplacian filter.

Installation

npm install simple-selfie

Usage

This project uses the Selfie class to detect the face in a video stream and display an overlay if the face deviates from a certain position or size.

HTML Structure

The HTML structure should include a container for the video stream and overlay, as well as elements for each face direction:

<div class="container">
  <div class="overlay"></div>
  <div class="face-position__direction_top"></div>
  <div class="face-position__direction_left"></div>
  <div class="face-position__direction_bottom"></div>
  <div class="face-position__direction_right"></div>
</div>

JavaScript

Create a new instance of the Selfie class, passing in the container and a callback function for when a face frame is processed:

import { Selfie } from 'simple-selfie';

const container = document.querySelector('.container');
const overlay = document.querySelector('.overlay');

const facePositions = {
  isLookUp: document.querySelector('.face-position__direction_top'),
  isLookRight: document.querySelector('.face-position__direction_left'),
  isLookDown: document.querySelector('.face-position__direction_bottom'),
  isLookLeft: document.querySelector('.face-position__direction_right'),
};

const selfie = new Selfie({
  container,
  onFaceFrameProcessed: ({ face, faceFrame, overlayVisible, detection }) => {
    // Handle face frame processing here
  },
});

Face Frame Processing

In the onFaceFrameProcessed callback, you can add or remove the overlay_visible class to the overlay based on the face's position and size. You can also add the face-position__direction_visible class to the appropriate face direction element:

onFaceFrameProcessed: ({ face, faceFrame, overlayVisible, detection }) => {
  if (face) {
    const faceWidth = face.getWidth();
    const deviationFaceWidth = Math.abs(FACE_WIDTH - faceWidth);
    const deviationFacePosition = face.getFacePosiotion();
    const overlayVisible = deviationFaceWidth > FACE_DEVIATION || deviationFacePosition > FACE_DEVIATION;
    if (overlayVisible) {
      overlay.classList.add('overlay_visible');
    } else {
      overlay.classList.remove('overlay_visible');
    }
  }

  for (const key in facePositions) {
    if (face.direction[key]()) {
      facePositions[key].classList.add('face-position__direction_visible');
    } else {
      facePositions[key].classList.remove('face-position__direction_visible');
    }
  }
};

Selfie Capture

The Selfie class also includes functionality for capturing a selfie. This can be done by calling the capture method:

import { Processors } from "simple-selfie";

const selfieImage = selfie.capture();
const frame = {
  width: selfie.video.videoWidth,
  height: selfie.video.videoHeight,
};
const faceFrame = {
  width: 200,
  height: 200,
};
// captureImage returns Uint8Array
const data = selfie.captureImage();
// convert uint8array to image
const image = await Processors.toImage(frame, data);

// use processors to process the image to detect blur
const cropped = await Processors.cropFrame(frame, lastFaceFrame, data);
const resized = await Processors.resizeFrame(lastFaceFrame, faceFrame, cropped);
const laplacian = await Processors.laplacian(faceFrame, resized);
const laplacianImage = await Processors.toImage(faceFrame, laplacian);
const blurVarianceResult = await Processors.variance(laplacian);

resultImage.src = image;
resultLaplacian.src = laplacianImage;

resultImageBlurred.textContent = 'Not blurred ' + Math.round(blurVarianceResult);
if (blurVarianceResult < 1100) {
  resultImageBlurred.classList.add('result-image__blurred_blurred');
  resultImageBlurred.textContent = 'Blurred ' + blurVarianceResult;
} else {
  resultImageBlurred.classList.remove('result-image__blurred_blurred');
}