Skip to content
Snippets Groups Projects
Commit 608c12a5 authored by dronus's avatar dronus
Browse files

code cleanup

parent e65cb14d
Branches
No related tags found
No related merge requests found
...@@ -32,27 +32,10 @@ ...@@ -32,27 +32,10 @@
faceapi.loadTinyFaceDetectorModel('/models'); faceapi.loadTinyFaceDetectorModel('/models');
// faceapi.loadFaceLandmarkModel('/models') // faceapi.loadFaceLandmarkModel('/models')
var mode='none';
var markers;
var running;
var countdown=0;
var restart_timeout_handle=false;
function restart_timeout()
{
if(restart_timeout_handle) clearTimeout(restart_timeout_handle);
restart_timeout_handle=setTimeout(function(){document.location.reload();},60000);
}
var time=0,frame_time=0; // running time var time=0,frame_time=0; // running time
var preview_cycle=0;
var preview_enabled=false;
var screenshot_cycle=0;
var preview_canvas=null;
var canvas_photo=document.getElementById('photo'); var canvas_photo=document.getElementById('photo');
var chain=null;
// main update function, shows video frames via glfx.js canvas
var update = async function() var update = async function()
{ {
// enqueue next update // enqueue next update
...@@ -63,44 +46,20 @@ ...@@ -63,44 +46,20 @@
frame_time=frame_time*0.9 + (current_time-time)*0.1; frame_time=frame_time*0.9 + (current_time-time)*0.1;
time=current_time; time=current_time;
//grab the context from your destination canvas //grab the context from your destination canvas
video=get_video(0); video=get_video(0);
/* if(!canvas_photo.width) {canvas_photo.width=video.width;canvas_photo.height=video.height}; var detection = await faceapi.detectSingleFace(video,new faceapi.TinyFaceDetectorOptions()); //.withFaceLandmarks();
*/ var detection = await faceapi.detectSingleFace(video,new faceapi.TinyFaceDetectorOptions()); //.withFaceLandmarks();
if(detection) if(detection && detection.box.height>150)
{ {
/* var getCenter=function(points){
var cx=0,cy=0;
for(var point of points){
cx+=point.x; cy+=point.y;
}
cx/=points.length; cy/=points.length;
return [cx,cy];
}
markers=[getCenter(detection.landmarks.getMouth()), getCenter(detection.landmarks.getLeftEye()), getCenter(detection.landmarks.getRightEye())];
*/
// console.log(JSON.stringify(detection));
var box=detection.box; var box=detection.box;
var pad=box.width*0.5; var pad=box.width*0.1;
canvas_photo.width=box.width+2*pad; canvas_photo.height=box.height+2*pad; canvas_photo.width=box.width+2*pad; canvas_photo.height=box.height+2*pad;
var ctx2d = canvas_photo.getContext('2d'); var ctx2d = canvas_photo.getContext('2d');
ctx2d.drawImage(video, box.x-pad, box.y-pad,box.width+2*pad,box.height+2*pad,0,0,box.width+2*pad, box.height+2*pad); ctx2d.drawImage(video, box.x-pad, box.y-pad,box.width+2*pad,box.height+2*pad,0,0,box.width+2*pad, box.height+2*pad);
} }
if(window.gc) window.gc();
}; };
// get the video feed from a capture device name by source_index into source_ids
// opens the capture device and starts streaming on demand
// the consumer may receive a still starting <video> object, so it has to add and handle 'canplay' event before properties like videoWidth are read.
//
var videos={}; var videos={};
var get_video=function(source_index,width,height) var get_video=function(source_index,width,height)
{ {
...@@ -119,7 +78,7 @@ ...@@ -119,7 +78,7 @@
var constraints = { var constraints = {
video: video:
{ {
optional: [{sourceId: source_ids.video[source_index]}] deviceId: source_ids.videoinput[source_index]
}, },
audio:false audio:false
}; };
...@@ -131,18 +90,16 @@ ...@@ -131,18 +90,16 @@
} }
// initalize getUserMedia() camera capture // initalize getUserMedia() camera capture
var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.oGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; navigator.getUserMedia(constraints, function(stream)
getUserMedia.call(navigator, constraints, function(stream){ {
console.log("Got camera!"); console.log("Got camera!");
// capture device was successfully acquired
video.autoplay = true; video.autoplay = true;
video.muted=true; video.muted=true;
video.srcObject=stream; video.srcObject=stream;
video.stream=stream; video.stream=stream;
// create a MediaSource / MediaRecorder / Video element for delayed playback
var source=new MediaSource(); var source=new MediaSource();
source.onsourceopen=function(){ source.onsourceopen=function(){
var sourceBuffer=source.addSourceBuffer("video/webm;codecs=vp8"); var sourceBuffer=source.addSourceBuffer("video/webm;codecs=vp8");
var recorder=new MediaRecorder(stream); var recorder=new MediaRecorder(stream);
...@@ -155,16 +112,12 @@ ...@@ -155,16 +112,12 @@
} }
video2=document.getElementById('video2'); video2=document.getElementById('video2');
video2.src=URL.createObjectURL(source); video2.src=URL.createObjectURL(source);
var capture_stream=video.captureStream(); var capture_stream=video.captureStream();
setTimeout(function(){ setTimeout(function(){
video2.play(); video2.play();
},5000); },5000);
// start the camera video
video.play(); video.play();
document.body.appendChild(video); document.body.appendChild(video);
}, function(err){ }, function(err){
...@@ -172,24 +125,21 @@ ...@@ -172,24 +125,21 @@
}); });
} }
// enumerate the available sources at startup and start update loop if found // enumerate the available sources at startup and start update loop if found
var source_ids={audio:[],video:[]}; var source_ids={audioinput:[],videoinput:[]};
function onSourcesAcquired(sources) function onSourcesAcquired(sources)
{ {
for (var i = 0; i != sources.length; ++i) { for (var i = 0; i != sources.length; ++i) {
var source = sources[i]; var source = sources[i];
source_ids[source.kind].push(source.id); if(source_ids[source.kind]) source_ids[source.kind].push(source.deviceId);
} }
// start frequent canvas updates // start frequent canvas updates
update(); update();
} }
setTimeout(function(){
if(MediaStreamTrack.getSources) navigator.mediaDevices.enumerateDevices().then(function(devices){
MediaStreamTrack.getSources(onSourcesAcquired); onSourcesAcquired(devices);
else });
onSourcesAcquired([]);
},1000);
</script> </script>
</body> </body>
</html> </html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment