• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Core engine for building motion detection web apps.

diff-cam-engine

Core engine for building motion detection web apps.

Usage

diff-cam-engine.js provides a DiffCamEngine object that accesses the webcam, captures images from it, and evaluates motion.

You'll want to use the adapter.js shim, which is available here: https://github.com/webrtc/adapter. Add it before diff-cam-engine.js.

With that in place, call DiffCamEngine.init() to initialize. This will set things up and ask the user for permission to access the webcam.

DiffCamEngine.init({
	// config options go here
});

Then call start() to begin the actual motion sensing. Do not call start() before init() has finished. It's a good idea to call start() from initSuccessCallback() (more on this later).

Call stop() to turn things off.

Configuration

You can customize how DiffCamEngine behaves by passing an object of name/value pairs into init(). For example:

DiffCamEngine.init({
	video: myVideo,
	captureIntervalTime: 50,
	captureCallback: myCaptureHandler
	// etc.
});
Variables

The following variables can be passed into init():

Name Default Description
video internal <video> (not visible) The <video> element for showing the live webcam stream
motionCanvas internal <canvas> (not visible) The <canvas> element for showing the visual motion heatmap
captureIntervalTime 100 Number of ms between capturing images from the stream
captureWidth 640 Width of captured images from stream
captureHeight 480 Height of capture images from stream
diffWidth 64 Width of (usually downsized) images used for diffing and showing motion
diffHeight 48 Height of (usually downsized) images used for diffing and showing motion
pixelDiffThreshold 32 Minimum difference in a pixel to be considered changed
scoreThreshold 16 Minimum number of changed pixels for an image to be considered as having motion
includeMotionBox false Flag to calculate and display (on motionCanvas) the bounding box of motion
includeMotionPixels false Flag to include data indicating all the changed pixels
Callbacks

There are also a couple callback functions you can specify. This is the primary way of interacting with DiffCamEngine during execution. Pass these into init() just like the variables above.

Name Description
initSuccessCallback Called when init has successfully completed
initErrorCallback Called when init fails
startCompleteCallback Called once the webcam has begun streaming
captureCallback Called after a captured image from the stream has been evaluated

captureCallback is the only one with a parameter. This parameter is an object with multiple properties on it. These properties are:

Property Description
imageData The imageData object for the captured image
score The evaluated score for the captured image
hasMotion Whether or not the score meets the score threshold for motion
motionBox An object containg x min/max and y min/max for a box wrapping the region in which motion occurred, only returned if includeMotionBox is true
motionPixels An object containg each pixel in the image that changed (indicating motion), only returned if includeMotionPixels is true
getURL Convenience function, returns imageData converted to a URL suitable for setting as the src of an image
checkMotionPixel Convenience function, takes in an x and y cooridnate, returns a boolean indicating if the pixel at that location has changed

Functions

DiffCamEngine exposes the following public functions:

Function Description
init Initializes everything and requests permission to access the webcam
start Begin streaming from the webcam
stop Stop streaming from the webcam
getPixelDiffThreshold Get pixelDiffThreshold during execution
setPixelDiffThreshold Set pixelDiffThreshold during execution
getScoreThreshold Get scoreThreshold during execution
setScoreThreshold Set scoreThreshold during execution

Examples

Check out https://github.com/lonekorean/diff-cam-scratchpad (specifically /diff-cam-example and /turret-security) for examples that use diff-cam-engine. Check out https://github.com/lonekorean/diff-cam-feed for a more complex web app powered by diff-cam-engine.

More Repositories

1

wordpress-export-to-markdown

Converts a WordPress export XML file into Markdown files.
JavaScript
1,075
star
2

highlight-within-textarea

jQuery plugin for highlighting bits of text within a textarea.
JavaScript
285
star
3

gist-syntax-themes

Collection of syntax theme stylesheets for GitHub Gists.
CSS
112
star
4

diff-cam-feed

Web app that uses motion detection to take a snapshot when something moves, then uploads the image to Twitter.
JavaScript
88
star
5

diff-cam-scratchpad

Various quick demos and experiments showing the concepts behind Diff Cam.
JavaScript
75
star
6

regex-storm

Regex Storm, online .NET-based regular expressions tester and resource.
C#
62
star
7

mini-preview

jQuery plugin for adding live mini-previews to links on hover.
JavaScript
57
star
8

hello-houdini

Some demos using Houdini's CSS Paint API.
HTML
56
star
9

atom-autocomplete-boilerplate

Boilerplate package for creating your own custom autocomplete provider for Atom.
JavaScript
34
star
10

background-scroll-physics

Nifty physics-based background scroll effects for your web pages.
26
star
11

atom-autocomplete-font-awesome

Font Awesome autocomplete and icon previews for Atom.
CSS
18
star
12

animation-workshop

Various demos highlighting CSS animation performance.
HTML
17
star
13

super-feed

Fetches content from various social media feeds to combine into one super feed.
C#
11
star
14

javascript-physics

2D physics JavaScript demos using Matter.js.
HTML
10
star
15

web-fish-daily

Web Fish Daily, site for sharing web dev links on a daily basis.
JavaScript
4
star
16

gist-to-prismjs

Converts embedded GitHub gists into PrismJS code blocks.
JavaScript
4
star
17

auto-cache-buster

Automatically generate cache-busting URLs when refreshing a page.
JavaScript
3
star
18

atom-autocomplete-math

Atom autocomplete package that solves math expressions.
JavaScript
2
star
19

heart-light

Simple Netlify site for controlling a light in the real world.
JavaScript
1
star
20

super-toast-story

Just a piece of toast, flying through space.
JavaScript
1
star