| const {createReadStream} = require('fs'); |
| const {join} = require('path'); |
|
|
| const {PNG} = require('pngjs'); |
| const {test} = require('tap'); |
|
|
| const {wrapClamp} = require('../../src/util/math-util'); |
|
|
| const VideoSensing = require('../../src/extensions/scratch3_video_sensing/index.js'); |
| const VideoMotion = require('../../src/extensions/scratch3_video_sensing/library.js'); |
|
|
| |
| |
| |
| |
| const pngPrefix = 'extension_video_sensing_'; |
|
|
| |
| |
| |
| |
| const framesMap = { |
| center: 'center', |
| left: 'left-5', |
| left2: 'left-10', |
| down: 'down-10' |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| const readPNG = name => ( |
| new Promise((resolve, reject) => { |
| const png = new PNG(); |
| createReadStream(join(__dirname, `${pngPrefix}${name}.png`)) |
| .pipe(png) |
| .on('parsed', () => { |
| |
| |
| resolve(new Uint32Array(new Uint8ClampedArray(png.data).buffer)); |
| }) |
| .on('error', reject); |
| }) |
| ); |
|
|
| |
| |
| |
| |
| |
| const readFrames = (() => { |
| |
| |
| let _promise = null; |
|
|
| return () => { |
| if (_promise === null) { |
| _promise = Promise.all(Object.keys(framesMap).map(key => readPNG(framesMap[key]))) |
| .then(pngs => ( |
| Object.keys(framesMap).reduce((frames, key, i) => { |
| frames[key] = pngs[i]; |
| return frames; |
| }, {}) |
| )); |
| } |
| return _promise; |
| }; |
| })(); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const isNearAngle = (actual, expect, optMargin = 10) => ( |
| (wrapClamp(actual - expect, 0, 359) < optMargin) || |
| (wrapClamp(actual - expect, 0, 359) > 360 - optMargin) |
| ); |
|
|
| |
| |
| const fakeDrawable = { |
| updateCPURenderAttributes () {}, |
|
|
| getFastBounds () { |
| return { |
| left: -120, |
| top: 60, |
| right: 0, |
| bottom: -60 |
| }; |
| }, |
|
|
| isTouching () { |
| return true; |
| } |
| }; |
|
|
| |
| |
| |
| const fakeMotionState = { |
| motionFrameNumber: -1, |
| motionAmount: -1, |
| motionDirection: -Infinity |
| }; |
|
|
| |
| const fakeTarget = { |
| drawableID: 0, |
|
|
| getCustomState () { |
| return fakeMotionState; |
| }, |
| setCustomState () {} |
| }; |
|
|
| const fakeRuntime = { |
| targets: [fakeTarget], |
|
|
| |
| |
| ioDevices: null, |
|
|
| renderer: { |
| _allDrawables: [ |
| fakeDrawable |
| ] |
| } |
| }; |
|
|
| const fakeBlockUtility = { |
| target: fakeTarget |
| }; |
|
|
| test('detect motionAmount between frames', t => { |
| t.plan(6); |
|
|
| return readFrames() |
| .then(frames => { |
| const detect = new VideoMotion(); |
|
|
| |
| const framePairs = [ |
| [frames.center, frames.left], |
| [frames.center, frames.left2], |
| [frames.left, frames.left2], |
| [frames.left, frames.center], |
| [frames.center, frames.down], |
| [frames.down, frames.center] |
| ]; |
|
|
| |
| let index = 0; |
| for (const [frame1, frame2] of framePairs) { |
| detect.addFrame(frame1); |
| detect.addFrame(frame2); |
|
|
| detect.analyzeFrame(); |
| t.ok( |
| detect.motionAmount > 10, |
| `frame pair ${index + 1} has motion ${detect.motionAmount} over threshold (10)` |
| ); |
| index += 1; |
| } |
|
|
| t.end(); |
| }); |
| }); |
|
|
| test('detect local motionAmount between frames', t => { |
| t.plan(6); |
|
|
| return readFrames() |
| .then(frames => { |
| const detect = new VideoMotion(); |
|
|
| |
| const framePairs = [ |
| [frames.center, frames.left], |
| [frames.center, frames.left2], |
| [frames.left, frames.left2], |
| [frames.left, frames.center], |
| [frames.center, frames.down], |
| [frames.down, frames.center] |
| ]; |
|
|
| |
| let index = 0; |
| for (const [frame1, frame2] of framePairs) { |
| detect.addFrame(frame1); |
| detect.addFrame(frame2); |
|
|
| detect.analyzeFrame(); |
| detect.getLocalMotion(fakeDrawable, fakeMotionState); |
| t.ok( |
| fakeMotionState.motionAmount > 10, |
| `frame pair ${index + 1} has motion ${fakeMotionState.motionAmount} over threshold (10)` |
| ); |
| index += 1; |
| } |
|
|
| t.end(); |
| }); |
| }); |
|
|
| test('detect motionDirection between frames', t => { |
| t.plan(6); |
|
|
| return readFrames() |
| .then(frames => { |
| const detect = new VideoMotion(); |
|
|
| |
| |
| const directionMargin = 10; |
| const framePairs = [ |
| { |
| frames: [frames.center, frames.left], |
| direction: -90 |
| }, |
| { |
| frames: [frames.center, frames.left2], |
| direction: -90 |
| }, |
| { |
| frames: [frames.left, frames.left2], |
| direction: -90 |
| }, |
| { |
| frames: [frames.left, frames.center], |
| direction: 90 |
| }, |
| { |
| frames: [frames.center, frames.down], |
| direction: 180 |
| }, |
| { |
| frames: [frames.down, frames.center], |
| direction: 0 |
| } |
| ]; |
|
|
| |
| |
| let index = 0; |
| for (const {frames: [frame1, frame2], direction} of framePairs) { |
| detect.addFrame(frame1); |
| detect.addFrame(frame2); |
|
|
| detect.analyzeFrame(); |
| t.ok( |
| isNearAngle(detect.motionDirection, direction, directionMargin), |
| `frame pair ${index + 1} is ${detect.motionDirection.toFixed(0)} ` + |
| `degrees and close to ${direction} degrees` |
| ); |
| index += 1; |
| } |
|
|
| t.end(); |
| }); |
| }); |
|
|
| test('detect local motionDirection between frames', t => { |
| t.plan(6); |
|
|
| return readFrames() |
| .then(frames => { |
| const detect = new VideoMotion(); |
|
|
| |
| |
| const directionMargin = 10; |
| const framePairs = [ |
| { |
| frames: [frames.center, frames.left], |
| direction: -90 |
| }, |
| { |
| frames: [frames.center, frames.left2], |
| direction: -90 |
| }, |
| { |
| frames: [frames.left, frames.left2], |
| direction: -90 |
| }, |
| { |
| frames: [frames.left, frames.center], |
| direction: 90 |
| }, |
| { |
| frames: [frames.center, frames.down], |
| direction: 180 |
| }, |
| { |
| frames: [frames.down, frames.center], |
| direction: 0 |
| } |
| ]; |
|
|
| |
| |
| let index = 0; |
| for (const {frames: [frame1, frame2], direction} of framePairs) { |
| detect.addFrame(frame1); |
| detect.addFrame(frame2); |
|
|
| detect.analyzeFrame(); |
| detect.getLocalMotion(fakeDrawable, fakeMotionState); |
| const motionDirection = fakeMotionState.motionDirection; |
| t.ok( |
| isNearAngle(motionDirection, direction, directionMargin), |
| `frame pair ${index + 1} is ${motionDirection.toFixed(0)} degrees and close to ${direction} degrees` |
| ); |
| index += 1; |
| } |
|
|
| t.end(); |
| }); |
| }); |
|
|
| test('videoOn returns value dependent on arguments', t => { |
| t.plan(4); |
|
|
| return readFrames() |
| .then(frames => { |
| const sensing = new VideoSensing(fakeRuntime); |
|
|
| |
| |
| sensing.detect.addFrame(frames.center); |
| sensing.detect.addFrame(frames.left); |
|
|
| const motionAmount = sensing.videoOn({ |
| ATTRIBUTE: VideoSensing.SensingAttribute.MOTION, |
| SUBJECT: VideoSensing.SensingSubject.STAGE |
| }, fakeBlockUtility); |
| t.ok( |
| motionAmount > 10, |
| `stage motionAmount ${motionAmount} is over the threshold (10)` |
| ); |
|
|
| const localMotionAmount = sensing.videoOn({ |
| ATTRIBUTE: VideoSensing.SensingAttribute.MOTION, |
| SUBJECT: VideoSensing.SensingSubject.SPRITE |
| }, fakeBlockUtility); |
| t.ok( |
| localMotionAmount > 10, |
| `sprite motionAmount ${localMotionAmount} is over the threshold (10)` |
| ); |
|
|
| const motionDirection = sensing.videoOn({ |
| ATTRIBUTE: VideoSensing.SensingAttribute.DIRECTION, |
| SUBJECT: VideoSensing.SensingSubject.STAGE |
| }, fakeBlockUtility); |
| t.ok( |
| isNearAngle(motionDirection, -90), |
| `stage motionDirection ${motionDirection.toFixed(0)} degrees is close to ${90} degrees` |
| ); |
|
|
| const localMotionDirection = sensing.videoOn({ |
| ATTRIBUTE: VideoSensing.SensingAttribute.DIRECTION, |
| SUBJECT: VideoSensing.SensingSubject.SPRITE |
| }, fakeBlockUtility); |
| t.ok( |
| isNearAngle(localMotionDirection, -90), |
| `sprite motionDirection ${localMotionDirection.toFixed(0)} degrees is close to ${90} degrees` |
| ); |
|
|
| t.end(); |
| }); |
| }); |
|
|
| test('whenMotionGreaterThan returns true if local motion meets target', t => { |
| t.plan(2); |
|
|
| return readFrames() |
| .then(frames => { |
| const sensing = new VideoSensing(fakeRuntime); |
|
|
| |
| |
| sensing.detect.addFrame(frames.center); |
| sensing.detect.addFrame(frames.left); |
|
|
| const over20 = sensing.whenMotionGreaterThan({ |
| REFERENCE: 20 |
| }, fakeBlockUtility); |
| t.ok( |
| over20, |
| `enough motion in drawable bounds to reach reference of 20` |
| ); |
|
|
| const over80 = sensing.whenMotionGreaterThan({ |
| REFERENCE: 80 |
| }, fakeBlockUtility); |
| t.notOk( |
| over80, |
| `not enough motion in drawable bounds to reach reference of 80` |
| ); |
|
|
| t.end(); |
| }); |
| }); |
|
|