function appReady() {
- drawImage('assets/inputsmall.jpg', 'input', function (width, height) {
+
+ if (navigator.mediaDevices) {
+ navigator.mediaDevices.getUserMedia({
+ video: {
+ width: {min: 1280},
+ height: {min: 720},
+ //facingMode: {exact: "environment"}
+ }, audio: false
+ }).then(function (stream) {
+ video.srcObject = stream;
+ video.play();
+
+ video.addEventListener('loadedmetadata', function () {
+ recognizeAttempt();
+ });
+ });
+ }
+}
+
+function recognizeAttempt() {
+ var input = document.querySelector('#input');
+ input.width = video.videoWidth;
+ input.height = video.videoHeight;
+ console.log('attempt');
+ console.log('-------');
+ input.getContext('2d').drawImage(video, 0, 0, input.width, input.height);
+ var start = Date.now();
+ var contour = detectBoard('input', input.width, input.height, true);
+ if (contour !== false) {
+ console.log(Date.now() - start);
+ correctPerspective(contour);
+ console.log(Date.now() - start);
+ recognizeChars('perspective');
+ } else {
setTimeout(function () {
- var start = Date.now();
- var contour = detectBoard('input', width, height, true);
- console.log(Date.now() - start);
- correctPerspective(contour);
- console.log(Date.now() - start);
- recognizeChars('perspective');
- }, 500)
- });
+ recognizeAttempt();
+ }, 250);
+ }
}
// We know that the board is delimited by two 5 sides contours. We find these two contours and select the smallest
let boardContour;
- if (filterByVertices.length === 1) {
- boardContour = filterByVertices[0];
- } else if (filterByVertices.length === 2) {
+ if (filterByVertices.length === 2) {
// If we just have two contours, no need to go further
boardContour = filterByVertices[1];
} else {
}
}
+ if (boardContour === undefined) {
+ return false;
+ }
if (draw) {
drawContours('contour', [boardContour], 'approx', width, height);
}
}
function drawContours(canvas, contours, type, width, height) {
- let tmp = new cv.MatVector();
- for (let i = 0; i < contours.length; i++) {
- tmp.push_back(contours[i][type]);
- }
+ try {
+
+
+ let tmp = new cv.MatVector();
+ for (let i = 0; i < contours.length; i++) {
+ tmp.push_back(contours[i][type]);
+ }
- var dst = cv.Mat.zeros(height, width, cv.CV_8UC3);
- for (let i = 0; i < tmp.size(); ++i) {
- let color = new cv.Scalar(Math.round(Math.random() * 255), Math.round(Math.random() * 255),
- Math.round(Math.random() * 255));
+ var dst = cv.Mat.zeros(height, width, cv.CV_8UC3);
+ for (let i = 0; i < tmp.size(); ++i) {
+ let color = new cv.Scalar(Math.round(Math.random() * 255), Math.round(Math.random() * 255),
+ Math.round(Math.random() * 255));
+
+ cv.drawContours(dst, tmp, i, color, 1, cv.LINE_8);
+ }
+ drawCV(canvas, width, height, dst);
+ } catch (e) {
- cv.drawContours(dst, tmp, i, color, 1, cv.LINE_8);
}
- drawCV(canvas, width, height, dst);
}
function drawCV(canvas, width, height, src) {
c = document.getElementById(canvas);
}
return c;
+}
+
+function wait(ms) {
+ return new Promise((resolve, reject) => {
+ setTimeout(() => {
+ console.log("Done waiting");
+ resolve(ms)
+ }, ms)
+ })
}
\ No newline at end of file