Coder Social home page Coder Social logo

desertsniper87 / blink-detection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from theankurkedia/blink-detection

0.0 1.0 0.0 14.02 MB

Detect the user's blink and wink using machine learning

Home Page: https://blink-detection.vercel.app

License: GNU General Public License v3.0

JavaScript 100.00%

blink-detection's Introduction

Blink Detection

Use machine learning in JavaScript to detect a blink and build blink-controlled experiences!

Demo

Visit https://blink-detection.vercel.app (Works on mobile too!!)

Uses Tensorflow.js's face landmark detection model.

Detection

This tool detects when the user blinks. It can also detect a wink and separate eye blinks as well.

Installation

Via npm:

Using yarn:

$ yarn add blink-detection

How to use

Code sample

Start by importing it:

import blink from 'blink-detection';

Load the machine learning model:

await blink.loadModel();

Then, set up the camera feed needed for the detection. The setUpCamera method needs a video HTML element and, optionally, a camera device ID if you are using more than the default webcam.

const videoElement = document.querySelector('video');

const init = async () => {
  // Using the default webcam
  await gaze.setUpCamera(videoElement);

  // Or, using more camera input devices
  const mediaDevices = await navigator.mediaDevices.enumerateDevices();
  const camera = mediaDevices.find(
    (device) =>
      device.kind === 'videoinput' &&
      device.label.includes(/* The label from the list of available devices*/)
  );

  await gaze.setUpCamera(videoElement, camera.deviceId);
};

Run the predictions:

const predict = async () => {
  const blinkPrediction = await blink.getBlinkPrediction();
  console.log('Blink: ', blinkPrediction); // will return an object indicating the booleans for different states
  // expect blinkPrediction to be {
  //  blink: boolean,
  //  wink: boolean,
  //  longBlink: boolean,
  //  left: boolean,
  //  right: boolean,
  //  rate: number
  // }
  if (blinkPrediction.blink) {
    // do something when the user blinks
  }
  let raf = requestAnimationFrame(predict);
};
predict();

Stop the detection:

cancelAnimationFrame(raf);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.