| (function () { |
| const BENCHMARK_THROTTLE = 250; |
| const INTERVAL = 33; |
|
|
| const video = document.createElement('video'); |
| navigator.getUserMedia({ |
| audio: false, |
| video: { |
| width: {min: 480, ideal: 640}, |
| height: {min: 360, ideal: 480} |
| } |
| }, stream => { |
| video.autoplay = true; |
| video.src = window.URL.createObjectURL(stream); |
| |
| |
| stream.getTracks(); |
| video.addEventListener('play', () => { |
| video.width = video.videoWidth; |
| video.height = video.videoHeight; |
| }); |
| }, err => { |
| |
| console.log(err); |
| }); |
|
|
| const VideoMotion = window.Scratch3VideoSensingDebug.VideoMotion; |
| const VideoMotionView = window.Scratch3VideoSensingDebug.VideoMotionView; |
|
|
| |
| const motion = new VideoMotion(); |
|
|
| |
| |
| const OUTPUT = VideoMotionView.OUTPUT; |
| const outputKeys = Object.keys(OUTPUT); |
| const outputValues = Object.values(OUTPUT); |
| const views = outputValues |
| .map(output => new VideoMotionView(motion, output)); |
| const view = views[0]; |
|
|
| const defaultViews = [OUTPUT.INPUT, OUTPUT.XY_CELL, OUTPUT.T_CELL, OUTPUT.UV_CELL]; |
|
|
| |
| const activators = document.createElement('div'); |
| activators.style.userSelect = 'none'; |
| outputValues.forEach((output, index) => { |
| const checkboxLabel = document.createElement('label'); |
| const checkbox = document.createElement('input'); |
| checkbox.type = 'checkbox'; |
| checkbox.checked = defaultViews.indexOf(output) !== -1; |
| const checkboxSpan = document.createElement('span'); |
| checkboxSpan.innerText = outputKeys[index]; |
| checkboxLabel.appendChild(checkbox); |
| checkboxLabel.appendChild(checkboxSpan); |
|
|
| const _view = views[index]; |
| _view.canvas.style.display = checkbox.checked ? '' : 'none'; |
| _view.active = checkbox.checked; |
| checkbox.onchange = event => { |
| _view.canvas.style.display = checkbox.checked ? '' : 'none'; |
| _view.active = checkbox.checked; |
| event.preventDefault(); |
| return false; |
| }; |
|
|
| activators.appendChild(checkboxLabel); |
| }); |
| document.body.appendChild(activators); |
|
|
| |
| |
| const textContainer = document.createElement('div'); |
| const textHeader = document.createElement('div'); |
| textHeader.innerText = 'duration (us) :: motion amount :: motion direction'; |
| textContainer.appendChild(textHeader); |
| const textEl = document.createElement('div'); |
| textEl.innerText = `0 :: 0 :: 0`; |
| textContainer.appendChild(textEl); |
| document.body.appendChild(textContainer); |
| let textTimer = Date.now(); |
|
|
| |
| |
| views.forEach(_view => document.body.appendChild(_view.canvas)); |
|
|
| |
| |
| const tempCanvas = document.createElement('canvas'); |
| tempCanvas.width = view.canvas.width; |
| tempCanvas.height = view.canvas.height; |
| const ctx = tempCanvas.getContext('2d'); |
|
|
| const loop = function () { |
| const timeoutId = setTimeout(loop, INTERVAL); |
|
|
| try { |
| |
| ctx.scale(-1, 1); |
| ctx.drawImage( |
| video, |
| 0, 0, video.width || video.clientWidth, video.height || video.clientHeight, |
| -tempCanvas.width, 0, tempCanvas.width, tempCanvas.height |
| ); |
| ctx.resetTransform(); |
| const data = ctx.getImageData(0, 0, tempCanvas.width, tempCanvas.height); |
|
|
| |
| const b = performance.now(); |
| motion.addFrame(data.data); |
| motion.analyzeFrame(); |
|
|
| |
| |
| |
| if (Date.now() - textTimer > BENCHMARK_THROTTLE) { |
| const e = performance.now(); |
| const analyzeDuration = ((e - b) * 1000).toFixed(0); |
| const motionAmount = motion.motionAmount.toFixed(1); |
| const motionDirection = motion.motionDirection.toFixed(1); |
| textEl.innerText = `${analyzeDuration} :: ${motionAmount} :: ${motionDirection}`; |
| textTimer = Date.now(); |
| } |
| views.forEach(_view => _view.active && _view.draw()); |
| } catch (error) { |
| |
| console.error(error.stack || error); |
| clearTimeout(timeoutId); |
| } |
| }; |
|
|
| loop(); |
| }()); |
|
|