diff --git a/.idea/workspace.xml b/.idea/workspace.xml index fe039a7..e9bf8bb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,10 @@ + + + @@ -21,20 +24,11 @@ - - - - - - - - - - - + + @@ -54,8 +48,8 @@ - - + + @@ -75,8 +69,20 @@ - - + + + + + + + + + + + + + + @@ -85,7 +91,7 @@ - + @@ -105,8 +111,8 @@ - - + + @@ -114,11 +120,11 @@ - + - - + + @@ -138,6 +144,9 @@ resizecompleted complete this.canvasref + this.videoref + this.videoRef + this.video @@ -158,7 +167,6 @@ @@ -282,12 +291,12 @@ - + - @@ -347,16 +356,6 @@ - - - - - - - - - - @@ -430,16 +429,6 @@ - - - - - - - - - - @@ -447,16 +436,6 @@ - - - - - - - - - - @@ -473,8 +452,28 @@ - - + + + + + + + + + + + + + + + + + + + + + + @@ -484,7 +483,17 @@ - + + + + + + + + + + + @@ -493,15 +502,15 @@ - - + + - + - - + + diff --git a/src/components/App.js b/src/components/App.js index 7ec739b..16daa6b 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -18,14 +18,26 @@ class App extends React.Component { this.state = { cvLoaded: false, launchWindow: false, - currentPage: "welcome" + currentPage: "welcome", + showVideo: false } this.targetScreenManager = new TargetScreenManager(); + this.videoRef = React.createRef(); } componentDidMount() { window.onOpenCV = () => {console.log("OpenCV Loaded");this.setState({ cvLoaded: true })}; + + const video = this.videoRef.current; + navigator.mediaDevices.getUserMedia({ video: true, audio: false }) + .then((stream) => { + video.srcObject = stream; + video.play(); + }) + .catch((err) => { + console.log("An error occurred! " + err); + }); } changePage(name) { @@ -34,6 +46,12 @@ class App extends React.Component { }) } + toggleVideo() { + this.setState({ + showVideo: !this.state.showVideo + }) + } + getComponent(name) { if (name === "welcome") { return ( @@ -46,12 +64,14 @@ class App extends React.Component { ) } else if (name === "calibrateLaser") { return ( - {this.changePage(name)}}/> + {this.changePage(name)}} + videoRef={this.videoRef}/> ) } else if (name === "calibrateProjector") { return ( this.changePage(name)} /> + changePage={(name) => this.changePage(name)} + videoRef={this.videoRef}/> ) } else if (name === "targetSelection") { return ( @@ -62,7 +82,8 @@ class App extends React.Component { } else if (name === "shoot") { return ( {this.changePage(name)}} /> + changePage={(name) => {this.changePage(name)}} + videoRef={this.videoRef}/> ) } else { return ( @@ -78,6 +99,21 @@ class App extends React.Component { {this.state.launchWindow && {this.changePage("calibrateLaser")}}/> } +
+
+
+ Webcam Feed +
+
+ {this.toggleVideo()}}> + {this.state.showVideo ? "Hide" : "Show"} + +
+
+
+
+
) } diff --git a/src/components/ProjectorScreen.js b/src/components/ProjectorScreen.js index 46e2612..29d83ce 100644 --- a/src/components/ProjectorScreen.js +++ b/src/components/ProjectorScreen.js @@ -58,7 +58,7 @@ class ProjectorScreenInner extends React.Component { <> -

SharpShooter Target Screen

+

Take Aim Target Screen

diff --git a/src/components/ShotFeed.js b/src/components/ShotFeed.js index daf73dc..a1a2e81 100644 --- a/src/components/ShotFeed.js +++ b/src/components/ShotFeed.js @@ -1,6 +1,6 @@ import React from 'react'; import ShotDetector from "../util/ShotDetector" -import {Button} from "react-bootstrap"; +import {Button, Form} from "react-bootstrap"; import cookie from 'react-cookies' class ShotFeed extends React.Component { @@ -10,20 +10,12 @@ class ShotFeed extends React.Component { running: false } - this.videoRef = React.createRef(); this.canvasRef = React.createRef(); } - componentDidMount() { - const video = this.videoRef.current; - navigator.mediaDevices.getUserMedia({ video: true, audio: false }) - .then((stream) => { - video.srcObject = stream; - video.play(); - }) - .catch((err) => { - console.log("An error occurred! " + err); - }); + updateNoise(e) { + let checked = e.target.checked; + this.shotDetector.filterNoise = checked; } startProcessing() { @@ -44,13 +36,13 @@ class ShotFeed extends React.Component { if (webcamConfig === null) { console.error("Missing webcam config"); } - this.shotDetector = new ShotDetector(this.videoRef.current, laserConfig.h, laserConfig.s, laserConfig.v, + this.shotDetector = new ShotDetector(this.props.videoRef.current, laserConfig.h, laserConfig.s, laserConfig.v, laserConfig.hRadius, laserConfig.sRadius, laserConfig.vRadius, webcamConfig.corners, outputDimensions, 200); } - const scaleRows = .5 - const scaleColumns = .5 + const scaleRows = .75 + const scaleColumns = .75 const canvas = this.canvasRef.current; canvas.width = outputDimensions.columns * scaleColumns; canvas.height = outputDimensions.rows * scaleRows; @@ -69,14 +61,14 @@ class ShotFeed extends React.Component { render() { return ( -
+
-
+
+ {this.updateNoise(e)}}/>
diff --git a/src/components/pages/CalibrateLaser.js b/src/components/pages/CalibrateLaser.js index d1e23f2..8ad16ac 100644 --- a/src/components/pages/CalibrateLaser.js +++ b/src/components/pages/CalibrateLaser.js @@ -18,7 +18,6 @@ class CalibrateLaser extends React.Component { // startButton: true }; - this.videoRef = React.createRef(); this.canvasRef = React.createRef(); const config = cookie.load("laserConfig") @@ -27,30 +26,12 @@ class CalibrateLaser extends React.Component { } } - componentDidMount() { - const video = this.videoRef.current; - navigator.mediaDevices.getUserMedia({ video: true, audio: false }) - .then((stream) => { - video.srcObject = stream; - video.onplay = () => { - setTimeout(() => { - this.startCalibrating() - }, 1000) - } - video.play(); - - }) - .catch((err) => { - console.log("An error occurred! " + err); - }); - } - startCalibrating() { this.setState({ startButton: false }, () => { const canvas = this.canvasRef.current; - const video = this.videoRef.current; + const video = this.props.videoRef.current; this.calibrator = new LaserCalibrator(video, canvas) this.calibrator.init(); @@ -62,6 +43,10 @@ class CalibrateLaser extends React.Component { }) } + componentDidMount() { + this.startCalibrating() + } + doneCalibrating() { const config = { h: this.state.h, @@ -103,15 +88,8 @@ class CalibrateLaser extends React.Component { - -
diff --git a/src/components/pages/CalibrateWebcam.js b/src/components/pages/CalibrateWebcam.js index 98a22d5..e29adbd 100644 --- a/src/components/pages/CalibrateWebcam.js +++ b/src/components/pages/CalibrateWebcam.js @@ -12,7 +12,6 @@ class CalibrateWebcam extends React.Component { corner: 1 } - this.videoRef = React.createRef(); this.canvasRef = React.createRef(); const config = cookie.load("webcamConfig") @@ -25,26 +24,12 @@ class CalibrateWebcam extends React.Component { } componentDidMount() { - const video = this.videoRef.current; - navigator.mediaDevices.getUserMedia({ video: true, audio: false }) - .then((stream) => { - video.srcObject = stream; - video.onplay = () => { - setTimeout(() => { - this.startCalibrating() - }, 1000) - } - video.play(); - - }) - .catch((err) => { - console.log("An error occurred! " + err); - }); + this.startCalibrating() } startCalibrating() { console.log("Calibrating!!") - this.calibrator = new WebcamCalibrator(this.videoRef.current, this.canvasRef.current, this.props.targetScreenManager, this.corners) + this.calibrator = new WebcamCalibrator(this.props.videoRef.current, this.canvasRef.current, this.props.targetScreenManager, this.corners) this.setState({ loading: false @@ -84,7 +69,6 @@ class CalibrateWebcam extends React.Component { - diff --git a/src/components/pages/Shoot.js b/src/components/pages/Shoot.js index 462347e..f0e64ba 100644 --- a/src/components/pages/Shoot.js +++ b/src/components/pages/Shoot.js @@ -19,7 +19,7 @@ class Shoot extends React.Component { - + diff --git a/src/util/ShotDetector.js b/src/util/ShotDetector.js index 705ff39..de59c27 100644 --- a/src/util/ShotDetector.js +++ b/src/util/ShotDetector.js @@ -18,6 +18,7 @@ class ShotDetector { this.hRadius = hRadius this.sRadius = sRadius this.vRadius = vRadius + this.filterNoise = false; this.targetCorners = targetCorners @@ -39,6 +40,7 @@ class ShotDetector { // Setup for processVideo this.readFrame = new window.cv.Mat(this.video.videoHeight, this.video.videoWidth, window.cv.CV_8UC4); + this.warpedFrame = new window.cv.Mat(this.outputDimensions.rows, this.outputDimensions.columns, window.cv.CV_8UC4); //const minThresholdValue = new window.cv.Scalar(20, 100, 200); //const maxThresholdValue = new window.cv.Scalar(160, 255, 256); @@ -50,8 +52,8 @@ class ShotDetector { Math.min(this.s + this.sRadius, 255), Math.min(this.v + this.vRadius, 255)); - this.minThreshold = new window.cv.Mat(this.video.videoHeight, this.video.videoWidth, window.cv.CV_8UC3, minThresholdValue); - this.maxThreshold = new window.cv.Mat(this.video.videoHeight, this.video.videoWidth, window.cv.CV_8UC3, maxThresholdValue); + this.minThreshold = new window.cv.Mat(this.outputDimensions.rows, this.outputDimensions.columns, window.cv.CV_8UC3, minThresholdValue); + this.maxThreshold = new window.cv.Mat(this.outputDimensions.rows, this.outputDimensions.columns, window.cv.CV_8UC3, maxThresholdValue); this.hsvImage = new window.cv.Mat(); this.thresholdImage = new window.cv.Mat(); @@ -88,7 +90,7 @@ class ShotDetector { 0, 0, this.outputDimensions.columns, 0, this.outputDimensions.columns, this.outputDimensions.rows, - 0, this.outputDimensions.columns + 0, this.outputDimensions.rows ] let targetCorners = window.cv.matFromArray(4, 2, window.cv.CV_32SC1, targetCornersArray); @@ -116,6 +118,7 @@ class ShotDetector { clearInterval(this.interval) this.readFrame.delete(); + this.warpedFrame.delete(); this.minThreshold.delete(); this.maxThreshold.delete(); this.hsvImage.delete(); @@ -141,9 +144,9 @@ class ShotDetector { try { this.videoCapture.read(this.readFrame) - window.cv.warpPerspective(this.readFrame, this.readFrame, this.hMatrix, new window.cv.Size(this.outputDimensions.columns, this.outputDimensions.rows)) + window.cv.warpPerspective(this.readFrame, this.warpedFrame, this.hMatrix, new window.cv.Size(this.outputDimensions.columns, this.outputDimensions.rows)) - window.cv.cvtColor(this.readFrame, this.hsvImage, window.cv.COLOR_BGR2HSV); + window.cv.cvtColor(this.warpedFrame, this.hsvImage, window.cv.COLOR_BGR2HSV); window.cv.inRange(this.hsvImage, this.minThreshold, this.maxThreshold, this.thresholdImage) window.cv.findContours(this.thresholdImage, this.contours, this.hierarchy, window.cv.RETR_CCOMP, window.cv.CHAIN_APPROX_SIMPLE); @@ -163,17 +166,21 @@ class ShotDetector { let cnt = this.contours.get(0); let circle = window.cv.minEnclosingCircle(cnt); - if (circle.radius < 1 || circle.radius > 20) { - if (circle.radius < 2) { - console.log("Radius too small!") - } - else { - console.log("Radius too big!") + + if (this.filterNoise){ + if (circle.radius < 1 || circle.radius > 20) { + if (circle.radius < 2) { + console.log("Radius too small!") + } + else { + console.log("Radius too big!") + } + this.shotInLastFrame = false; + this.currentlyProcessing = false; + return null; } - this.shotInLastFrame = false; - this.currentlyProcessing = false; - return null; } + this.lastShot = new Date(); this.currentlyProcessing = false;