1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| // Sources :
// https://lonekorean.github.io/diff-cam-scratchpad/turret-security/
// https://codelabs.developers.google.com/codelabs/tensorflowjs-object-detection/#0
//
// www.smartblob.eu
const SCALE=10; // capture resolution over motion resolution
let _model=undefined;
let _debug=1;
let _isActivated=false;
let _isTargetInSight=false;
let _isKnockedOver=false;
let _lostTimeout;
let _lastClass=undefined;
class $$page
{ constructor()
{ this.frag=document.createDocumentFragment();
this.parent=undefined;
}
add({parent="",tag="",id="",text="",border="",width="",height="",classes=""})
{ let elem=document.createElement(tag);
if(id!="") elem.id=id;
if(text!="") elem.textContent=text;
if(width!="") elem.width=width;
if(height!="") elem.height=height;
if(classes) elem.className=classes;
if(this.parent==undefined)
{ this.frag.appendChild(elem);
this.parent=elem;
}
else
{ if(parent=="")
{ this.parent.appendChild(elem);
}
else
{ this.frag.querySelector("#"+parent).appendChild(elem);
}
}
}
render()
{ document.body.appendChild(this.frag);
}
}
function initSuccess()
{ DiffCamEngine.start();
}
function initError()
{ alert('Something went wrong.');
}
function startComplete()
{ setTimeout(activate,500);
}
function activate()
{ _isActivated=true;
debug("activated");
}
function capture(payload)
{ if (!_isActivated || _isKnockedOver)
{ return;
}
let box=payload.motionBox;
if (box)
{ // video is flipped, so we're positioning from right instead of left
let right=box.x.min*SCALE+1;
let top=box.y.min*SCALE+1;
let width=(box.x.max-box.x.min)*SCALE;
let height=(box.y.max-box.y.min)*SCALE;
let videoElem=document.querySelector("#video");
let motionElem=document.querySelector("#motionBox");
motionElem.style.position="absolute";
motionElem.style.right=right+"px";
motionElem.style.top=top+"px";
motionElem.style.width=width+"px";
motionElem.style.height=height+"px";
motionElem.style.border="2px solid red";
motionElem.style.visibility="visible";
/* Modèle mobilenet
_model.classify(payload.imageData).then(predictions =>
{ for(let i=0;i<predictions.length;i++)
{ debug("Predictions: ",predictions[i].className+"=>"+predictions[i].probability);
}
});
*/
// Modèle coco-ssd
_model.detect(payload.imageData).then(predictions =>
{ for(let i=0;i<predictions.length;i++)
{ if(predictions[i].score>.85)
{ if(predictions[i].class!=_lastClass)
{ _lastClass=predictions[i].class;
debug("Predictions: ",predictions[i].class+"=>"+predictions[i].score);
document.querySelector("#motionText").textContent=predictions[i].class+"=>"+predictions[i].score.toFixed(2)
speech(predictions[i].class);
}
}
else
{ document.querySelector("#motionText").textContent="";
}
}
});
if (!_isTargetInSight)
{ _isTargetInSight=true;
debug("seen");
}
else
{ //debug("fire");
}
clearTimeout(_lostTimeout);
_lostTimeout=setTimeout(declareLost,2000);
}
/*
// video is flipped, so (0, 0) is at top right
if (payload.checkMotionPixel(0, 0))
{ knockOver();
}
*/
}
function declareLost()
{ _isTargetInSight=false;
document.querySelector("#motionBox").style.visibility="hidden";
debug("target lost");
}
function knockOver()
{ _isKnockedOver=true;
clearTimeout(_lostTimeout);
document.querySelector("#motionBox").style.visibility="hidden";
debug("knock over");
}
function debug(text,...data)
{ if(_debug==1)
{ if(data.length!=0)
{ console.debug(new Date().toLocaleTimeString([],{hour:'2-digit',minute:'2-digit',second:'2-digit',fractionalSecondDigits:3})+" -> "+text,data);
}
else
{ console.debug(new Date().toLocaleTimeString([],{hour:'2-digit',minute:'2-digit',second:'2-digit',fractionalSecondDigits:3})+" -> "+text);
}
}
}
async function modelLoad()
{ _model=await cocoSsd.load();
}
async function main()
{ let $page=new $$page();
$page.add({tag:"div",id:"wrapper"});
$page.add({tag:"video",id:"video"});
$page.add({tag:"div",id:"motionBox"});
$page.add({parent:"motionBox",tag:"div",id:"motionText"});
$page.render();
document.querySelector('#video').autoplay=true;
DiffCamEngine.init({video:document.getElementById('video'),captureIntervalTime:50,includeMotionBox:true,includeMotionPixels:true,initSuccessCallback:initSuccess,initErrorCallback:initError,startCompleteCallback:startComplete,captureCallback:capture});
await modelLoad();
}
function speech(text)
{ speechSynthesis.speak(new SpeechSynthesisUtterance(text));
}
} |
Partager