From 562363299ffc2a92bb190e7d985d2fa8e1a80efe Mon Sep 17 00:00:00 2001 From: Neil Gershenfeld <gersh@cba.mit.edu> Date: Sat, 6 Jan 2018 16:33:04 -0500 Subject: [PATCH] start motion detect --- files.html | 1 + modules/convert/rgba/png | 3 - modules/image/motion detect | 184 ++++++++++++++++++++++++++++++++++++ modules/index.html | 1 + 4 files changed, 186 insertions(+), 3 deletions(-) create mode 100755 modules/image/motion detect diff --git a/files.html b/files.html index 52ce998..9d617d4 100644 --- a/files.html +++ b/files.html @@ -61,6 +61,7 @@ <a href='./modules/image/dogbone'>dogbone</a><br> <a href='./modules/image/edge%20detect'>edge detect</a><br> <a href='./modules/image/function'>function</a><br> + <a href='./modules/image/motion%20detect'>motion detect</a><br> <a href='./modules/image/offset'>offset</a><br> <a href='./modules/image/orient%20edges'>orient edges</a><br> <a href='./modules/image/palette'>palette</a><br> diff --git a/modules/convert/rgba/png b/modules/convert/rgba/png index 73b3f4e..d5ff0ed 100755 --- a/modules/convert/rgba/png +++ b/modules/convert/rgba/png @@ -104,13 +104,10 @@ var interface = function(div){ // info div // var info = document.createElement('div') - info.setAttribute('id',div.id+'info') info.appendChild(document.createTextNode('file name: ')) var input = document.createElement('input') input.type = 'text' input.size = 6 - input.addEventListener('input',function(){ - }) info.appendChild(input) mod.nametext = input info.appendChild(document.createElement('br')) diff --git a/modules/image/motion detect b/modules/image/motion detect new file mode 100755 index 0000000..4f10247 --- /dev/null +++ b/modules/image/motion detect @@ -0,0 +1,184 @@ +// +// motion detect +// +// Neil Gershenfeld +// (c) Massachusetts Institute of Technology 2017 +// +// This work may be reproduced, modified, distributed, performed, and +// displayed for any purpose, but must acknowledge the mods +// project. Copyright is retained and must be preserved. The work is +// provided as is; no warranty is provided, and users accept all +// liability. +// +// closure +// +(function(){ +// +// module globals +// +var mod = {} +// +// name +// +var name = 'image motion detect' +// +// initialization +// +var init = function() { + } +// +// inputs +// +var inputs = { + image:{type:'RGBA', + event:function(evt){ + var ctx = mod.img.getContext("2d") + ctx.canvas.width = evt.detail.width + ctx.canvas.height = evt.detail.height + ctx.putImageData(evt.detail,0,0) + mod.pxtext.nodeValue = evt.detail.width+' x '+evt.detail.height+' px' + convert_image() + }}} +// +// outputs +// +var outputs = { + image:{type:'RGBA', + event:function(){ + mods.output(mod,'image',img)}}, + imageInfo:{type:'object', + event:function(){ + var obj = {} + obj.name = mod.name.nodeValue + obj.dpi = parseFloat(mod.dpitext.value) + obj.width = mod.img.width + obj.height = mod.img.height + mods.output(mod,'imageInfo',obj)}}, + trigger:{type:'event', + event:function(){ + mods.output(mod,'image',img)}}} +// +// interface +// +var interface = function(div){ + mod.div = div + // + // on-screen drawing canvas + // + var canvas = document.createElement('canvas') + canvas.width = mods.ui.canvas + canvas.height = mods.ui.canvas + canvas.style.backgroundColor = 'rgb(255,255,255)' + div.appendChild(canvas) + mod.canvas = canvas + div.appendChild(document.createElement('br')) + // + // off-screen image canvas + // + var canvas = document.createElement('canvas') + mod.img = canvas + // + // view button + // + var btn = document.createElement('button') + btn.style.padding = mods.ui.padding + btn.style.margin = 1 + btn.appendChild(document.createTextNode('view')) + btn.addEventListener('click',function(){ + var win = window.open('') + var btn = document.createElement('button') + btn.appendChild(document.createTextNode('close')) + btn.style.padding = mods.ui.padding + btn.style.margin = 1 + btn.addEventListener('click',function(){ + win.close() + }) + win.document.body.appendChild(btn) + win.document.body.appendChild(document.createElement('br')) + var canvas = document.createElement('canvas') + canvas.width = mod.img.width + canvas.height = mod.img.height + win.document.body.appendChild(canvas) + var ctx = canvas.getContext("2d") + ctx.drawImage(mod.img,0,0) + }) + div.appendChild(btn) + div.appendChild(document.createTextNode(' ')) + // + // info div + // + var info = document.createElement('div') + info.appendChild(document.createElement('br')) + var text = document.createTextNode('change (0-1): ') + info.appendChild(text) + mod.change = text + info.appendChild(document.createTextNode('threshold (0-1): ')) + var input = document.createElement('input') + input.type = 'text' + input.size = 6 + info.appendChild(input) + mod.threshold = input + info.appendChild(document.createElement('br')) + info.appendChild(document.createTextNode('latency (s): ')) + var input = document.createElement('input') + input.type = 'text' + input.size = 6 + info.appendChild(input) + mod.time = input + div.appendChild(info) + } +// +// local functions +// +function convert_image() { + // + // preview + // + var h = mod.img.height + var w = mod.img.width + if (w > h) { + var x0 = 0 + var y0 = mod.canvas.height*.5*(1-h/w) + var wd = mod.canvas.width + var hd = mod.canvas.width*h/w + } + else { + var x0 = mod.canvas.width*.5*(1-w/h) + var y0 = 0 + var wd = mod.canvas.height*w/h + var hd = mod.canvas.height + } + var ctx = mod.canvas.getContext("2d") + ctx.clearRect(0,0,mod.canvas.width,mod.canvas.height) + ctx.drawImage(mod.img,x0,y0,wd,hd) + // + // convert and save + // + mod.img.toBlob(function(blob){ + var url = URL.createObjectURL(blob) + var link = document.createElement('a') + link.download = mod.nametext.value + link.href = url + document.body.appendChild(link) + link.click() + document.body.removeChild(link) + URL.revokeObjectURL(url) + },'image/png') + } +function update_info() { + mod.dpi = parseFloat(mod.dpitext.value) + mod.mmtext.nodeValue = (25.4*mod.img.width/mod.dpi).toFixed(3) + +' x '+(25.4*mod.img.height/mod.dpi).toFixed(3)+' mm' + mod.intext.nodeValue = (mod.img.width/mod.dpi).toFixed(3) + +' x '+(mod.img.height/mod.dpi).toFixed(3)+' in' + } +// return values +// +return ({ + name:name, + init:init, + inputs:inputs, + outputs:outputs, + interface:interface + }) +}()) diff --git a/modules/index.html b/modules/index.html index 16dd328..b06b2bb 100644 --- a/modules/index.html +++ b/modules/index.html @@ -44,6 +44,7 @@ <a href="javascript:handler('modules/image/dogbone')">dogbone</a><br> <a href="javascript:handler('modules/image/edge%20detect')">edge detect</a><br> <a href="javascript:handler('modules/image/function')">function</a><br> + <a href="javascript:handler('modules/image/motion%20detect')">motion detect</a><br> <a href="javascript:handler('modules/image/offset')">offset</a><br> <a href="javascript:handler('modules/image/orient%20edges')">orient edges</a><br> <a href="javascript:handler('modules/image/palette')">palette</a><br> -- GitLab