Spaces:
Running
Running
Update src/App.js
Browse files- src/App.js +14 -22
src/App.js
CHANGED
|
@@ -9,21 +9,20 @@ const App = () => {
|
|
| 9 |
const [predictions, setPredictions] = useState([]);
|
| 10 |
const [model, setModel] = useState(null);
|
| 11 |
const [detectionInterval, setDetectionInterval] = useState(null);
|
| 12 |
-
const videoRef = useRef(null);
|
| 13 |
-
const canvasRef = useRef(null);
|
| 14 |
-
const streamRef = useRef(null);
|
| 15 |
|
| 16 |
const videoConstraints = {
|
| 17 |
width: 640,
|
| 18 |
height: 480,
|
| 19 |
-
facingMode: 'user'
|
| 20 |
};
|
| 21 |
|
| 22 |
-
// Load Coco SSD model on app start
|
| 23 |
useEffect(() => {
|
| 24 |
const loadModel = async () => {
|
| 25 |
try {
|
| 26 |
-
await tf.setBackend('webgl');
|
| 27 |
const loadedModel = await cocoSsd.load();
|
| 28 |
setModel(loadedModel);
|
| 29 |
console.log('Neural sensor model loaded – Ready for object detection.');
|
|
@@ -34,7 +33,6 @@ const App = () => {
|
|
| 34 |
};
|
| 35 |
loadModel();
|
| 36 |
|
| 37 |
-
// Cleanup on unmount
|
| 38 |
return () => {
|
| 39 |
if (streamRef.current) {
|
| 40 |
streamRef.current.getTracks().forEach(track => track.stop());
|
|
@@ -42,19 +40,18 @@ const App = () => {
|
|
| 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 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
}
|
| 59 |
} catch (err) {
|
| 60 |
console.error('Detection error:', err);
|
|
@@ -62,7 +59,6 @@ const App = () => {
|
|
| 62 |
}
|
| 63 |
};
|
| 64 |
|
| 65 |
-
// Draw bounding boxes and labels on canvas overlay
|
| 66 |
const drawPredictions = (preds) => {
|
| 67 |
const canvas = canvasRef.current;
|
| 68 |
if (!canvas || !videoRef.current) return;
|
|
@@ -72,7 +68,6 @@ const App = () => {
|
|
| 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);
|
|
@@ -81,7 +76,7 @@ const App = () => {
|
|
| 81 |
const [x, y, width, height] = pred.bbox;
|
| 82 |
ctx.strokeStyle = '#00ff00';
|
| 83 |
ctx.lineWidth = 2;
|
| 84 |
-
ctx.strokeRect(-x - width, y, width, height);
|
| 85 |
ctx.fillStyle = '#00ff00';
|
| 86 |
ctx.font = '16px Arial';
|
| 87 |
ctx.fillText(`${pred.class} (${Math.round(pred.score * 100)}%)`, -x - width, y - 5);
|
|
@@ -90,12 +85,11 @@ const App = () => {
|
|
| 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
|
| 99 |
});
|
| 100 |
streamRef.current = stream;
|
| 101 |
if (videoRef.current) {
|
|
@@ -104,7 +98,6 @@ const App = () => {
|
|
| 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.');
|
|
@@ -114,7 +107,6 @@ const App = () => {
|
|
| 114 |
}
|
| 115 |
};
|
| 116 |
|
| 117 |
-
// Stop scanning and clear resources
|
| 118 |
const stopScanning = () => {
|
| 119 |
setIsScanning(false);
|
| 120 |
if (detectionInterval) {
|
|
@@ -148,11 +140,11 @@ const App = () => {
|
|
| 148 |
ref={videoRef}
|
| 149 |
autoPlay
|
| 150 |
muted
|
| 151 |
-
playsInline
|
| 152 |
style={{
|
| 153 |
width: '100%',
|
| 154 |
maxWidth: '640px',
|
| 155 |
-
transform: 'scaleX(-1)',
|
| 156 |
border: '2px solid #00ff00'
|
| 157 |
}}
|
| 158 |
/>
|
|
@@ -165,7 +157,7 @@ const App = () => {
|
|
| 165 |
width: '640px',
|
| 166 |
height: '480px',
|
| 167 |
pointerEvents: 'none',
|
| 168 |
-
transform: 'scaleX(-1)',
|
| 169 |
border: '2px solid #00ff00'
|
| 170 |
}}
|
| 171 |
/>
|
|
|
|
| 9 |
const [predictions, setPredictions] = useState([]);
|
| 10 |
const [model, setModel] = useState(null);
|
| 11 |
const [detectionInterval, setDetectionInterval] = useState(null);
|
| 12 |
+
const videoRef = useRef(null);
|
| 13 |
+
const canvasRef = useRef(null);
|
| 14 |
+
const streamRef = useRef(null);
|
| 15 |
|
| 16 |
const videoConstraints = {
|
| 17 |
width: 640,
|
| 18 |
height: 480,
|
| 19 |
+
facingMode: 'user'
|
| 20 |
};
|
| 21 |
|
|
|
|
| 22 |
useEffect(() => {
|
| 23 |
const loadModel = async () => {
|
| 24 |
try {
|
| 25 |
+
await tf.setBackend('webgl');
|
| 26 |
const loadedModel = await cocoSsd.load();
|
| 27 |
setModel(loadedModel);
|
| 28 |
console.log('Neural sensor model loaded – Ready for object detection.');
|
|
|
|
| 33 |
};
|
| 34 |
loadModel();
|
| 35 |
|
|
|
|
| 36 |
return () => {
|
| 37 |
if (streamRef.current) {
|
| 38 |
streamRef.current.getTracks().forEach(track => track.stop());
|
|
|
|
| 40 |
};
|
| 41 |
}, []);
|
| 42 |
|
|
|
|
| 43 |
const predictObjects = async () => {
|
| 44 |
if (videoRef.current && videoRef.current.videoWidth > 0 && model && isScanning) {
|
| 45 |
try {
|
| 46 |
const predictionsArray = await model.detect(videoRef.current);
|
| 47 |
setPredictions(predictionsArray);
|
| 48 |
drawPredictions(predictionsArray);
|
|
|
|
| 49 |
if (predictionsArray.length > 0) {
|
| 50 |
console.log('Detected objects:', predictionsArray.map(p => p.class));
|
| 51 |
+
if ('speechSynthesis' in window) {
|
| 52 |
+
const utterance = new SpeechSynthesisUtterance(`Detected ${predictionsArray[0].class}`);
|
| 53 |
+
speechSynthesis.speak(utterance);
|
| 54 |
+
}
|
| 55 |
}
|
| 56 |
} catch (err) {
|
| 57 |
console.error('Detection error:', err);
|
|
|
|
| 59 |
}
|
| 60 |
};
|
| 61 |
|
|
|
|
| 62 |
const drawPredictions = (preds) => {
|
| 63 |
const canvas = canvasRef.current;
|
| 64 |
if (!canvas || !videoRef.current) return;
|
|
|
|
| 68 |
canvas.height = video.videoHeight;
|
| 69 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 70 |
|
|
|
|
| 71 |
ctx.save();
|
| 72 |
ctx.scale(-1, 1);
|
| 73 |
ctx.translate(-canvas.width, 0);
|
|
|
|
| 76 |
const [x, y, width, height] = pred.bbox;
|
| 77 |
ctx.strokeStyle = '#00ff00';
|
| 78 |
ctx.lineWidth = 2;
|
| 79 |
+
ctx.strokeRect(-x - width, y, width, height);
|
| 80 |
ctx.fillStyle = '#00ff00';
|
| 81 |
ctx.font = '16px Arial';
|
| 82 |
ctx.fillText(`${pred.class} (${Math.round(pred.score * 100)}%)`, -x - width, y - 5);
|
|
|
|
| 85 |
ctx.restore();
|
| 86 |
};
|
| 87 |
|
|
|
|
| 88 |
const startScanning = async () => {
|
| 89 |
try {
|
| 90 |
const stream = await navigator.mediaDevices.getUser Media({
|
| 91 |
video: videoConstraints,
|
| 92 |
+
audio: false
|
| 93 |
});
|
| 94 |
streamRef.current = stream;
|
| 95 |
if (videoRef.current) {
|
|
|
|
| 98 |
}
|
| 99 |
setIsScanning(true);
|
| 100 |
setError(null);
|
|
|
|
| 101 |
const interval = setInterval(predictObjects, 500);
|
| 102 |
setDetectionInterval(interval);
|
| 103 |
console.log('Front camera sensor activated – Neural detection started.');
|
|
|
|
| 107 |
}
|
| 108 |
};
|
| 109 |
|
|
|
|
| 110 |
const stopScanning = () => {
|
| 111 |
setIsScanning(false);
|
| 112 |
if (detectionInterval) {
|
|
|
|
| 140 |
ref={videoRef}
|
| 141 |
autoPlay
|
| 142 |
muted
|
| 143 |
+
playsInline
|
| 144 |
style={{
|
| 145 |
width: '100%',
|
| 146 |
maxWidth: '640px',
|
| 147 |
+
transform: 'scaleX(-1)',
|
| 148 |
border: '2px solid #00ff00'
|
| 149 |
}}
|
| 150 |
/>
|
|
|
|
| 157 |
width: '640px',
|
| 158 |
height: '480px',
|
| 159 |
pointerEvents: 'none',
|
| 160 |
+
transform: 'scaleX(-1)',
|
| 161 |
border: '2px solid #00ff00'
|
| 162 |
}}
|
| 163 |
/>
|