Spaces:
Running
Running
Update src/App.js
Browse files- src/App.js +63 -28
src/App.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import React, { useState, useRef, useEffect } from 'react';
|
| 2 |
-
import Webcam from 'react-webcam';
|
| 3 |
import * as tf from '@tensorflow/tfjs';
|
| 4 |
import * as cocoSsd from '@tensorflow-models/coco-ssd';
|
| 5 |
import './App.css';
|
|
@@ -10,19 +9,21 @@ const App = () => {
|
|
| 10 |
const [predictions, setPredictions] = useState([]);
|
| 11 |
const [model, setModel] = useState(null);
|
| 12 |
const [detectionInterval, setDetectionInterval] = useState(null);
|
| 13 |
-
const
|
| 14 |
const canvasRef = useRef(null); // For drawing bounding boxes
|
|
|
|
| 15 |
|
| 16 |
const videoConstraints = {
|
| 17 |
width: 640,
|
| 18 |
height: 480,
|
| 19 |
-
facingMode: 'user' // Front camera for
|
| 20 |
};
|
| 21 |
|
| 22 |
// Load Coco SSD model on app start
|
| 23 |
useEffect(() => {
|
| 24 |
const loadModel = async () => {
|
| 25 |
try {
|
|
|
|
| 26 |
const loadedModel = await cocoSsd.load();
|
| 27 |
setModel(loadedModel);
|
| 28 |
console.log('Neural sensor model loaded – Ready for object detection.');
|
|
@@ -32,20 +33,28 @@ const App = () => {
|
|
| 32 |
}
|
| 33 |
};
|
| 34 |
loadModel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
}, []);
|
| 36 |
|
| 37 |
// Sensor action: Predict objects from video frames
|
| 38 |
const predictObjects = async () => {
|
| 39 |
-
if (
|
| 40 |
try {
|
| 41 |
-
const predictionsArray = await model.detect(
|
| 42 |
setPredictions(predictionsArray);
|
| 43 |
-
drawPredictions(predictionsArray);
|
| 44 |
-
// Trigger aural/mental actions
|
| 45 |
if (predictionsArray.length > 0) {
|
| 46 |
console.log('Detected objects:', predictionsArray.map(p => p.class));
|
| 47 |
-
// Example
|
| 48 |
-
|
|
|
|
| 49 |
}
|
| 50 |
} catch (err) {
|
| 51 |
console.error('Detection error:', err);
|
|
@@ -56,39 +65,56 @@ const App = () => {
|
|
| 56 |
// Draw bounding boxes and labels on canvas overlay
|
| 57 |
const drawPredictions = (preds) => {
|
| 58 |
const canvas = canvasRef.current;
|
| 59 |
-
if (!canvas) return;
|
| 60 |
const ctx = canvas.getContext('2d');
|
| 61 |
-
|
| 62 |
-
canvas.
|
|
|
|
| 63 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
preds.forEach(pred => {
|
| 66 |
const [x, y, width, height] = pred.bbox;
|
| 67 |
ctx.strokeStyle = '#00ff00';
|
| 68 |
ctx.lineWidth = 2;
|
| 69 |
-
ctx.strokeRect(x, y, width, height);
|
| 70 |
ctx.fillStyle = '#00ff00';
|
| 71 |
ctx.font = '16px Arial';
|
| 72 |
-
ctx.fillText(`${pred.class} (${Math.round(pred.score * 100)}%)`, x, y - 5);
|
| 73 |
});
|
|
|
|
|
|
|
| 74 |
};
|
| 75 |
|
| 76 |
-
// Start scanning with
|
| 77 |
const startScanning = async () => {
|
| 78 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
setIsScanning(true);
|
| 80 |
setError(null);
|
| 81 |
-
//
|
| 82 |
const interval = setInterval(predictObjects, 500);
|
| 83 |
setDetectionInterval(interval);
|
| 84 |
console.log('Front camera sensor activated – Neural detection started.');
|
| 85 |
} catch (err) {
|
| 86 |
-
setError('Failed to
|
| 87 |
console.error('Scan error:', err);
|
| 88 |
}
|
| 89 |
};
|
| 90 |
|
| 91 |
-
// Stop scanning and clear
|
| 92 |
const stopScanning = () => {
|
| 93 |
setIsScanning(false);
|
| 94 |
if (detectionInterval) {
|
|
@@ -96,8 +122,12 @@ const App = () => {
|
|
| 96 |
setDetectionInterval(null);
|
| 97 |
}
|
| 98 |
setPredictions([]);
|
| 99 |
-
if (
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
}
|
| 102 |
console.log('Sensor scan stopped – Neural processing halted.');
|
| 103 |
};
|
|
@@ -106,7 +136,7 @@ const App = () => {
|
|
| 106 |
<div className="App">
|
| 107 |
<header className="App-header">
|
| 108 |
<h1>Aural Mental Neural Sense App</h1>
|
| 109 |
-
<p>Front Camera Sensor with Object Detection</p>
|
| 110 |
{error && <p style={{ color: 'red' }}>{error}</p>}
|
| 111 |
|
| 112 |
{!isScanning ? (
|
|
@@ -114,13 +144,17 @@ const App = () => {
|
|
| 114 |
) : (
|
| 115 |
<div>
|
| 116 |
<div style={{ position: 'relative', display: 'inline-block' }}>
|
| 117 |
-
<
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
/>
|
| 125 |
<canvas
|
| 126 |
ref={canvasRef}
|
|
@@ -131,6 +165,7 @@ const App = () => {
|
|
| 131 |
width: '640px',
|
| 132 |
height: '480px',
|
| 133 |
pointerEvents: 'none',
|
|
|
|
| 134 |
border: '2px solid #00ff00'
|
| 135 |
}}
|
| 136 |
/>
|
|
|
|
| 1 |
import React, { useState, useRef, useEffect } from 'react';
|
|
|
|
| 2 |
import * as tf from '@tensorflow/tfjs';
|
| 3 |
import * as cocoSsd from '@tensorflow-models/coco-ssd';
|
| 4 |
import './App.css';
|
|
|
|
| 9 |
const [predictions, setPredictions] = useState([]);
|
| 10 |
const [model, setModel] = useState(null);
|
| 11 |
const [detectionInterval, setDetectionInterval] = useState(null);
|
| 12 |
+
const videoRef = useRef(null); // Native video element ref
|
| 13 |
const canvasRef = useRef(null); // For drawing bounding boxes
|
| 14 |
+
const streamRef = useRef(null); // To store the media stream
|
| 15 |
|
| 16 |
const videoConstraints = {
|
| 17 |
width: 640,
|
| 18 |
height: 480,
|
| 19 |
+
facingMode: 'user' // Front camera for neural scan
|
| 20 |
};
|
| 21 |
|
| 22 |
// Load Coco SSD model on app start
|
| 23 |
useEffect(() => {
|
| 24 |
const loadModel = async () => {
|
| 25 |
try {
|
| 26 |
+
await tf.setBackend('webgl'); // Optimize for browser performance
|
| 27 |
const loadedModel = await cocoSsd.load();
|
| 28 |
setModel(loadedModel);
|
| 29 |
console.log('Neural sensor model loaded – Ready for object detection.');
|
|
|
|
| 33 |
}
|
| 34 |
};
|
| 35 |
loadModel();
|
| 36 |
+
|
| 37 |
+
// Cleanup on unmount
|
| 38 |
+
return () => {
|
| 39 |
+
if (streamRef.current) {
|
| 40 |
+
streamRef.current.getTracks().forEach(track => track.stop());
|
| 41 |
+
}
|
| 42 |
+
};
|
| 43 |
}, []);
|
| 44 |
|
| 45 |
// Sensor action: Predict objects from video frames
|
| 46 |
const predictObjects = async () => {
|
| 47 |
+
if (videoRef.current && videoRef.current.videoWidth > 0 && model && isScanning) {
|
| 48 |
try {
|
| 49 |
+
const predictionsArray = await model.detect(videoRef.current);
|
| 50 |
setPredictions(predictionsArray);
|
| 51 |
+
drawPredictions(predictionsArray);
|
| 52 |
+
// Trigger aural/mental actions
|
| 53 |
if (predictionsArray.length > 0) {
|
| 54 |
console.log('Detected objects:', predictionsArray.map(p => p.class));
|
| 55 |
+
// Example aural feedback: Speech synthesis
|
| 56 |
+
const utterance = new SpeechSynthesisUtterance(`Detected ${predictionsArray[0].class}`);
|
| 57 |
+
speechSynthesis.speak(utterance);
|
| 58 |
}
|
| 59 |
} catch (err) {
|
| 60 |
console.error('Detection error:', err);
|
|
|
|
| 65 |
// Draw bounding boxes and labels on canvas overlay
|
| 66 |
const drawPredictions = (preds) => {
|
| 67 |
const canvas = canvasRef.current;
|
| 68 |
+
if (!canvas || !videoRef.current) return;
|
| 69 |
const ctx = canvas.getContext('2d');
|
| 70 |
+
const video = videoRef.current;
|
| 71 |
+
canvas.width = video.videoWidth;
|
| 72 |
+
canvas.height = video.videoHeight;
|
| 73 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 74 |
|
| 75 |
+
// Mirror the canvas for front camera (selfie effect)
|
| 76 |
+
ctx.save();
|
| 77 |
+
ctx.scale(-1, 1);
|
| 78 |
+
ctx.translate(-canvas.width, 0);
|
| 79 |
+
|
| 80 |
preds.forEach(pred => {
|
| 81 |
const [x, y, width, height] = pred.bbox;
|
| 82 |
ctx.strokeStyle = '#00ff00';
|
| 83 |
ctx.lineWidth = 2;
|
| 84 |
+
ctx.strokeRect(-x - width, y, width, height); // Adjust for mirror
|
| 85 |
ctx.fillStyle = '#00ff00';
|
| 86 |
ctx.font = '16px Arial';
|
| 87 |
+
ctx.fillText(`${pred.class} (${Math.round(pred.score * 100)}%)`, -x - width, y - 5);
|
| 88 |
});
|
| 89 |
+
|
| 90 |
+
ctx.restore();
|
| 91 |
};
|
| 92 |
|
| 93 |
+
// Start scanning with native getUser Media
|
| 94 |
const startScanning = async () => {
|
| 95 |
try {
|
| 96 |
+
const stream = await navigator.mediaDevices.getUser Media({
|
| 97 |
+
video: videoConstraints,
|
| 98 |
+
audio: false // Add audio: true for aural sensing later
|
| 99 |
+
});
|
| 100 |
+
streamRef.current = stream;
|
| 101 |
+
if (videoRef.current) {
|
| 102 |
+
videoRef.current.srcObject = stream;
|
| 103 |
+
videoRef.current.play();
|
| 104 |
+
}
|
| 105 |
setIsScanning(true);
|
| 106 |
setError(null);
|
| 107 |
+
// Start continuous detection interval
|
| 108 |
const interval = setInterval(predictObjects, 500);
|
| 109 |
setDetectionInterval(interval);
|
| 110 |
console.log('Front camera sensor activated – Neural detection started.');
|
| 111 |
} catch (err) {
|
| 112 |
+
setError('Failed to access front camera. Check permissions and HTTPS.');
|
| 113 |
console.error('Scan error:', err);
|
| 114 |
}
|
| 115 |
};
|
| 116 |
|
| 117 |
+
// Stop scanning and clear resources
|
| 118 |
const stopScanning = () => {
|
| 119 |
setIsScanning(false);
|
| 120 |
if (detectionInterval) {
|
|
|
|
| 122 |
setDetectionInterval(null);
|
| 123 |
}
|
| 124 |
setPredictions([]);
|
| 125 |
+
if (streamRef.current) {
|
| 126 |
+
streamRef.current.getTracks().forEach(track => track.stop());
|
| 127 |
+
streamRef.current = null;
|
| 128 |
+
}
|
| 129 |
+
if (videoRef.current) {
|
| 130 |
+
videoRef.current.srcObject = null;
|
| 131 |
}
|
| 132 |
console.log('Sensor scan stopped – Neural processing halted.');
|
| 133 |
};
|
|
|
|
| 136 |
<div className="App">
|
| 137 |
<header className="App-header">
|
| 138 |
<h1>Aural Mental Neural Sense App</h1>
|
| 139 |
+
<p>Front Camera Sensor with Object Detection (Native Fallback)</p>
|
| 140 |
{error && <p style={{ color: 'red' }}>{error}</p>}
|
| 141 |
|
| 142 |
{!isScanning ? (
|
|
|
|
| 144 |
) : (
|
| 145 |
<div>
|
| 146 |
<div style={{ position: 'relative', display: 'inline-block' }}>
|
| 147 |
+
<video
|
| 148 |
+
ref={videoRef}
|
| 149 |
+
autoPlay
|
| 150 |
+
muted
|
| 151 |
+
playsInline // For mobile compatibility
|
| 152 |
+
style={{
|
| 153 |
+
width: '100%',
|
| 154 |
+
maxWidth: '640px',
|
| 155 |
+
transform: 'scaleX(-1)', // Mirror for front camera
|
| 156 |
+
border: '2px solid #00ff00'
|
| 157 |
+
}}
|
| 158 |
/>
|
| 159 |
<canvas
|
| 160 |
ref={canvasRef}
|
|
|
|
| 165 |
width: '640px',
|
| 166 |
height: '480px',
|
| 167 |
pointerEvents: 'none',
|
| 168 |
+
transform: 'scaleX(-1)', // Mirror canvas too
|
| 169 |
border: '2px solid #00ff00'
|
| 170 |
}}
|
| 171 |
/>
|