|
| 1 | +var irs = (function() { |
| 2 | + function js_speak(phrase) { |
| 3 | + var msg = new SpeechSynthesisUtterance(phrase); |
| 4 | + window.speechSynthesis.speak(msg); |
| 5 | + } |
| 6 | + function chrome() { |
| 7 | + /* do some magical checking that chrome is available */ |
| 8 | + if (typeof (window.SpeechRecognition || window.webkitSpeechRecognition) === "undefined") { |
| 9 | + return null; |
| 10 | + } |
| 11 | + |
| 12 | + //Voice Recognition |
| 13 | + function listen(words, timeout, callback) { |
| 14 | + var SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; |
| 15 | + var recognition = new SpeechRecognition(); |
| 16 | + |
| 17 | + recognition.continuous = true; |
| 18 | + recognition.onresult = function(event) { |
| 19 | + txt = event.results[event.resultIndex][0].transcript; |
| 20 | + console.log(txt); |
| 21 | + recognised = txt.split(" "); |
| 22 | + for (word in recognised){ |
| 23 | + x = words.indexOf(recognised[word]); |
| 24 | + if (x!=-1){ |
| 25 | + if (callback){ |
| 26 | + callback(words.indexOf(recognised[word])); |
| 27 | + } |
| 28 | + callback = null; |
| 29 | + } |
| 30 | + } |
| 31 | + if (x == -1){ |
| 32 | + if (callback){callback(x);} |
| 33 | + callback = null; |
| 34 | + } |
| 35 | + recognition.stop(); |
| 36 | + } |
| 37 | + |
| 38 | + recognition.start(); |
| 39 | + setTimeout(function () { |
| 40 | + recognition.stop(); |
| 41 | + if (callback){ |
| 42 | + callback(-1); |
| 43 | + callback = null; |
| 44 | + } |
| 45 | + }, timeout); |
| 46 | + } |
| 47 | + |
| 48 | + //Image Capture Code |
| 49 | + function gotMedia(mediaStream) { |
| 50 | + mediaStreamTrack = mediaStream.getVideoTracks()[0]; |
| 51 | + imageCapture = new ImageCapture(mediaStreamTrack); |
| 52 | + console.log(imageCapture); |
| 53 | + } |
| 54 | + |
| 55 | + function drawCanvas(canvas, img) { |
| 56 | + canvas.width = getComputedStyle(canvas).width.split('px')[0]; |
| 57 | + canvas.height = getComputedStyle(canvas).height.split('px')[0]; |
| 58 | + let ratio = Math.min(canvas.width / img.width, canvas.height / img.height); |
| 59 | + let x = (canvas.width - img.width * ratio) / 2; |
| 60 | + let y = (canvas.height - img.height * ratio) / 2; |
| 61 | + canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height); |
| 62 | + canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height, |
| 63 | + x, y, img.width * ratio, img.height * ratio); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + function takePhoto(callback){ |
| 69 | + console.log("Say cheese") |
| 70 | + |
| 71 | + var htmlCanvas = document.createElement("canvas"); |
| 72 | + htmlCanvas.width = 1280; |
| 73 | + htmlCanvas.height = 720; |
| 74 | + htmlCanvas.style.visibility = "hidden"; |
| 75 | + document.body.appendChild(htmlCanvas); |
| 76 | + imageCapture.grabFrame() |
| 77 | + .then(imageBitmap => { |
| 78 | + drawCanvas(htmlCanvas, imageBitmap); |
| 79 | + callback(htmlCanvas.toDataURL()) |
| 80 | + }) |
| 81 | + .catch(error => console.log(error)); |
| 82 | + } |
| 83 | + |
| 84 | + navigator.mediaDevices.getUserMedia({video: true}) |
| 85 | + .then(gotMedia) |
| 86 | + .catch(error => console.error('getUserMedia() error:', error)); |
| 87 | + |
| 88 | + return { |
| 89 | + photo: takePhoto, |
| 90 | + listen: listen, |
| 91 | + say: js_speak, |
| 92 | + identify: function() { |
| 93 | + return 2; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + function robot() { |
| 99 | + //check if we are running on the robot |
| 100 | + if(typeof irs_raw === "undefined") { |
| 101 | + return null; |
| 102 | + } |
| 103 | + /* setup the robot callback handlers */ |
| 104 | + irs_raw.phraseSuccess = function(res) { |
| 105 | + console.log(res); |
| 106 | + } |
| 107 | + |
| 108 | + /* make the irs thingo */ |
| 109 | + return { |
| 110 | + identify: function() {return 1} |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + function basic() { |
| 115 | + return { |
| 116 | + identify: function() { |
| 117 | + return 0; |
| 118 | + }, |
| 119 | + say: js_speak, |
| 120 | + listen: function(_, _, cb) { |
| 121 | + console.error('irs.listen is not available on this platform. use chrome'); |
| 122 | + cb(-3); |
| 123 | + }, |
| 124 | + photo: function(cb) { |
| 125 | + console.error('irs.photo is not available on this platform. use chrome'); |
| 126 | + cb(null); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return robot() || chrome() || basic(); |
| 131 | +})() |
0 commit comments