Graduate

Resize stream input

......@@ -21,6 +21,26 @@
<script type='text/javascript' src="{{url_for('static', filename='js/utils.js')}}"></script>
<script type='text/javascript' src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type='text/javascript'>
function init()
{
let video = document.getElementById("videoInput");
if (navigator.mediaDevices.getUserMedia){
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
video.addEventListener('canplay', () => {
video.width = video.videoWidth;
video.height = video.videoHeight;
load_cascade();
});
}).catch(function (err0r) {
console.log("Something went wrong!");
streaming = false;
});
}
}
function load_cascade()
{
let faceCascadeFile = 'haarcascade_frontalface_default.xml'
......@@ -31,22 +51,12 @@ function load_cascade()
});
}
function main()
{
let video = document.getElementById("videoInput");
let canvasOutput = document.getElementById("canvasOutput");
let canvasContext = canvasOutput.getContext('2d');
if (navigator.mediaDevices.getUserMedia){
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
}).catch(function (err0r) {
console.log("Something went wrong!");
streaming = false;
});
}
let src = new cv.Mat(video.height, video.width, cv.CV_8UC4);
let dst = new cv.Mat(video.height, video.width, cv.CV_8UC4);
let gray = new cv.Mat();
......@@ -151,6 +161,7 @@ function processVideo() {
});
}
}
// to do resize preview
cv.imshow('canvasOutput', dst);
// schedule the next one.
let delay = 1000/FPS - (Date.now() - begin);
......@@ -163,10 +174,10 @@ setTimeout(processVideo, 0);
}
</script>
</head>
<body onload="cv['onRuntimeInitialized']=()=>{ load_cascade(); };">
<body onload="cv['onRuntimeInitialized']=()=>{ init(); };">
<div id="container">
<video autoplay="true" id="videoInput" width=640 height=480 style="display: none;"></video>
<canvas id="canvasOutput" width=640 height=480></canvas>
<video autoplay="true" id="videoInput" style="display: none; object-fit: cover;"></video>
<canvas id="canvasOutput"></canvas>
</div>
</body>
</html>
......
......@@ -72,9 +72,9 @@ function detect_face()
let sender = document.getElementById("sender");
sender.disabled = false;
}
if (dst.cols > $(window).width())
if (dst.cols > $(window).width() || dst.rows > $(window).height())
{
let ratio = $(window).width() / parseFloat(dst.cols);
let ratio = Math.min($(window).width() / parseFloat(dst.cols), $(window).height() / parseFloat(dst.rows));
let dsize = new cv.Size(dst.cols * ratio, dst.rows * ratio);
cv.resize(dst, dst, dsize, 0, 0, cv.INTER_AREA);
}
......