Spaces:
Running
Running
Update src/App.js
Browse files- src/App.js +113 -423
src/App.js
CHANGED
|
@@ -1,461 +1,151 @@
|
|
| 1 |
-
import React, { useState, useEffect } from 'react';
|
|
|
|
|
|
|
|
|
|
| 2 |
import './App.css';
|
| 3 |
|
| 4 |
const App = () => {
|
| 5 |
const [isScanning, setIsScanning] = useState(false);
|
| 6 |
-
const [scanResults, setScanResults] = useState(null);
|
| 7 |
const [error, setError] = useState(null);
|
| 8 |
-
const [
|
| 9 |
-
const [
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
const
|
| 13 |
-
|
| 14 |
-
const
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
name: 'Aural Sense (Headset Audio)',
|
| 19 |
-
description: 'Scan Bluetooth headsets for audio biofeedback and noise sensing...',
|
| 20 |
-
icon: '🎧',
|
| 21 |
-
color: '#FF6B6B',
|
| 22 |
-
services: ['00001108-0000-1000-8000-00805f9b34fb'] // HFP for audio headsets
|
| 23 |
-
},
|
| 24 |
-
neural: {
|
| 25 |
-
name: 'Neural Sense (Headset EEG)',
|
| 26 |
-
description: 'Scan EEG-enabled headsets for brainwave/neural activity...',
|
| 27 |
-
icon: '🧠',
|
| 28 |
-
color: '#4ECDC4',
|
| 29 |
-
services: ['0000fee0-0000-1000-8000-00805f9b34fb'] // Example Muse EEG service
|
| 30 |
-
},
|
| 31 |
-
mental: {
|
| 32 |
-
name: 'Mental Sense (Headset Bio)',
|
| 33 |
-
description: 'Scan headsets for cognitive/mental state via sensors...',
|
| 34 |
-
icon: '🧘',
|
| 35 |
-
color: '#45B7D1',
|
| 36 |
-
services: ['heart_rate', 'battery_service'] // Heart rate for mental stress proxy
|
| 37 |
-
}
|
| 38 |
};
|
| 39 |
|
| 40 |
-
//
|
| 41 |
useEffect(() => {
|
| 42 |
-
|
| 43 |
-
}, []);
|
| 44 |
-
|
| 45 |
-
const checkPermissions = async () => {
|
| 46 |
-
// Check Bluetooth
|
| 47 |
-
if (!navigator.bluetooth) {
|
| 48 |
-
setBluetoothStatus('unsupported');
|
| 49 |
-
} else {
|
| 50 |
try {
|
| 51 |
-
const
|
| 52 |
-
|
|
|
|
| 53 |
} catch (err) {
|
| 54 |
-
|
|
|
|
| 55 |
}
|
| 56 |
-
}
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
} else {
|
| 62 |
try {
|
| 63 |
-
const
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
setTimeout(() => {
|
| 72 |
-
setShowPermissions(bluetoothStatus !== 'ready' || locationStatus !== 'ready');
|
| 73 |
-
}, 100);
|
| 74 |
-
};
|
| 75 |
-
|
| 76 |
-
const requestBluetoothPermission = async () => {
|
| 77 |
-
if (!navigator.bluetooth) {
|
| 78 |
-
setError('Web Bluetooth not supported. Use Chrome/Edge.');
|
| 79 |
-
return;
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
try {
|
| 83 |
-
setBluetoothStatus('checking');
|
| 84 |
-
// Lightweight request to trigger prompt (without full scan)
|
| 85 |
-
const device = await navigator.bluetooth.requestDevice({
|
| 86 |
-
filters: [{ services: ['battery_service'] }],
|
| 87 |
-
optionalServices: ['battery_service'],
|
| 88 |
-
acceptAllDevices: false
|
| 89 |
-
});
|
| 90 |
-
if (device) {
|
| 91 |
-
setBluetoothStatus('ready');
|
| 92 |
-
// Disconnect immediately
|
| 93 |
-
if (device.gatt?.connected) {
|
| 94 |
-
device.gatt.disconnect();
|
| 95 |
}
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
setBluetoothStatus('needed');
|
| 99 |
-
}
|
| 100 |
-
} catch (err) {
|
| 101 |
-
console.error('Bluetooth request failed:', err);
|
| 102 |
-
setBluetoothStatus('needed');
|
| 103 |
-
if (err.name === 'SecurityError') {
|
| 104 |
-
setError('Bluetooth access denied. Allow in browser settings.');
|
| 105 |
}
|
| 106 |
}
|
| 107 |
};
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
(
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
}
|
| 128 |
-
},
|
| 129 |
-
{ enableHighAccuracy: false, timeout: 5000, maximumAge: 0 }
|
| 130 |
-
);
|
| 131 |
};
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
setError('Please enable Bluetooth and Location permissions first.');
|
| 136 |
-
return;
|
| 137 |
-
}
|
| 138 |
-
|
| 139 |
-
if (!await checkBluetoothSupport()) return;
|
| 140 |
-
|
| 141 |
-
setIsScanning(true);
|
| 142 |
-
setError(null);
|
| 143 |
-
setSelectedSense(senseKey);
|
| 144 |
-
setScanResults(null);
|
| 145 |
-
setConnectedDevice(null);
|
| 146 |
-
|
| 147 |
try {
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
],
|
| 155 |
-
optionalServices: ['battery_service', 'device_information', ...senses[senseKey].services],
|
| 156 |
-
acceptAllDevices: false
|
| 157 |
-
});
|
| 158 |
-
|
| 159 |
-
if (!device) {
|
| 160 |
-
throw new Error('No headset selected. Ensure your Bluetooth headset is discoverable.');
|
| 161 |
-
}
|
| 162 |
-
|
| 163 |
-
console.log(`Connecting to headset: ${device.name || device.id}`);
|
| 164 |
-
setConnectedDevice(device.name || `Headset ${device.id.slice(-4)}`);
|
| 165 |
-
|
| 166 |
-
const server = await device.gatt.connect();
|
| 167 |
-
console.log('Connected to headset GATT server');
|
| 168 |
-
|
| 169 |
-
let headsetData = {};
|
| 170 |
-
|
| 171 |
-
try {
|
| 172 |
-
const batteryService = await server.getPrimaryService('battery_service');
|
| 173 |
-
const batteryChar = await batteryService.getCharacteristic('battery_level');
|
| 174 |
-
const batteryValue = await batteryChar.readValue();
|
| 175 |
-
const batteryLevel = batteryValue.getUint8(0);
|
| 176 |
-
|
| 177 |
-
headsetData = generateHeadsetSensorData(senseKey, batteryLevel);
|
| 178 |
-
} catch (serviceError) {
|
| 179 |
-
console.log('Headset services not fully available, using mock data');
|
| 180 |
-
headsetData = generateHeadsetSensorData(senseKey, Math.floor(Math.random() * 100));
|
| 181 |
-
}
|
| 182 |
-
|
| 183 |
-
await new Promise(resolve => setTimeout(resolve, 2000));
|
| 184 |
-
|
| 185 |
-
const scanResult = {
|
| 186 |
-
deviceName: device.name || `BLE Headset ${device.id.slice(-4)}`,
|
| 187 |
-
deviceId: device.id,
|
| 188 |
-
senseType: senses[senseKey].name,
|
| 189 |
-
...headsetData,
|
| 190 |
-
status: 'Headset Scan Complete',
|
| 191 |
-
timestamp: new Date().toLocaleTimeString(),
|
| 192 |
-
connection: 'Bluetooth Low Energy (BLE)',
|
| 193 |
-
pairingTip: 'Tip: For full audio, pair via OS Bluetooth settings after scanning.'
|
| 194 |
-
};
|
| 195 |
-
|
| 196 |
-
setScanResults(scanResult);
|
| 197 |
-
await server.disconnect();
|
| 198 |
-
console.log('Disconnected from headset');
|
| 199 |
-
|
| 200 |
} catch (err) {
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
if (err.name === 'NotFoundError') {
|
| 204 |
-
errorMessage += 'No compatible headsets found. Put your headset in pairing mode (hold power button).';
|
| 205 |
-
} else if (err.name === 'SecurityError') {
|
| 206 |
-
errorMessage += 'Bluetooth permission denied. Allow access in browser settings.';
|
| 207 |
-
} else if (err.name === 'NetworkError') {
|
| 208 |
-
errorMessage += 'Connection lost. Ensure headset is within 10m and charged.';
|
| 209 |
-
} else {
|
| 210 |
-
errorMessage += err.message;
|
| 211 |
-
}
|
| 212 |
-
setError(errorMessage);
|
| 213 |
-
} finally {
|
| 214 |
-
setIsScanning(false);
|
| 215 |
-
setConnectedDevice(null);
|
| 216 |
}
|
| 217 |
};
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
| 223 |
}
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
setError('Bluetooth is disabled. Use the Enable Bluetooth button above.');
|
| 228 |
-
return false;
|
| 229 |
-
}
|
| 230 |
-
} catch (err) {
|
| 231 |
-
console.warn('Bluetooth availability check failed:', err);
|
| 232 |
-
}
|
| 233 |
-
return true;
|
| 234 |
-
};
|
| 235 |
-
|
| 236 |
-
const generateHeadsetSensorData = (senseKey, baseValue) => {
|
| 237 |
-
const timestamp = Date.now();
|
| 238 |
-
const variance = Math.floor(Math.random() * 20 - 10);
|
| 239 |
-
|
| 240 |
-
switch (senseKey) {
|
| 241 |
-
case 'aural':
|
| 242 |
-
return {
|
| 243 |
-
audioLevel: `${Math.max(20, Math.min(100, baseValue + variance))}%`,
|
| 244 |
-
noiseCancellation: Math.random() > 0.5 ? 'Active (ANC On)' : 'Passive',
|
| 245 |
-
leftEarBalance: `${50 + Math.floor(Math.random() * 20)}%`,
|
| 246 |
-
rightEarBalance: `${50 + Math.floor(Math.random() * 20)}%`,
|
| 247 |
-
micSensitivity: `${-40 + Math.floor(Math.random() * 20)}dB`,
|
| 248 |
-
frequencyResponse: '20Hz - 20kHz'
|
| 249 |
-
};
|
| 250 |
-
|
| 251 |
-
case 'neural':
|
| 252 |
-
return {
|
| 253 |
-
eegSignal: `${Math.max(10, Math.min(90, baseValue + variance))}%`,
|
| 254 |
-
alphaWaves: `${8 + Math.random() * 4}Hz (Relaxed)`,
|
| 255 |
-
betaWaves: `${13 + Math.random() * 17}Hz (Focused)`,
|
| 256 |
-
deltaWaves: `${0.5 + Math.random() * 4}Hz (Deep Sleep)`,
|
| 257 |
-
headsetContact: 'Good (Electrodes Aligned)',
|
| 258 |
-
neuralFocus: Math.random() > 0.4 ? 'High' : 'Medium'
|
| 259 |
-
};
|
| 260 |
-
|
| 261 |
-
case 'mental':
|
| 262 |
-
return {
|
| 263 |
-
mentalClarity: `${Math.max(30, Math.min(100, baseValue + variance))}%`,
|
| 264 |
-
stressIndicator: Math.random() > 0.6 ? 'Low' : Math.random() > 0.3 ? 'Medium' : 'High',
|
| 265 |
-
heartRate: `${60 + Math.floor(Math.random() * 40)} bpm`,
|
| 266 |
-
relaxationScore: `${Math.floor(Math.random() * 100)}%`,
|
| 267 |
-
attentionSpan: `${Math.floor(Math.random() * 100)}%`
|
| 268 |
-
};
|
| 269 |
-
|
| 270 |
-
default:
|
| 271 |
-
return { battery: `${baseValue}%` };
|
| 272 |
-
}
|
| 273 |
-
};
|
| 274 |
-
|
| 275 |
-
const getSenseButton = (key) => (
|
| 276 |
-
<button
|
| 277 |
-
key={key}
|
| 278 |
-
onClick={() => scanBluetoothHeadset(key)}
|
| 279 |
-
disabled={isScanning || bluetoothStatus !== 'ready' || locationStatus !== 'ready'}
|
| 280 |
-
className="sense-button"
|
| 281 |
-
style={{ backgroundColor: isScanning && selectedSense === key ? '#6c757d' : senses[key].color }}
|
| 282 |
-
>
|
| 283 |
-
<span className="button-icon">{senses[key].icon}</span>
|
| 284 |
-
{isScanning && selectedSense === key ? 'Scanning Headset...' : `Scan ${senses[key].name}`}
|
| 285 |
-
</button>
|
| 286 |
-
);
|
| 287 |
-
|
| 288 |
-
const clearResults = () => {
|
| 289 |
-
setScanResults(null);
|
| 290 |
-
setError(null);
|
| 291 |
-
setSelectedSense('');
|
| 292 |
-
};
|
| 293 |
-
|
| 294 |
-
const getStatusIcon = (status) => {
|
| 295 |
-
switch (status) {
|
| 296 |
-
case 'ready': return '✅';
|
| 297 |
-
case 'needed': return '⚠️';
|
| 298 |
-
case 'checking': return '⏳';
|
| 299 |
-
case 'unsupported': return '❌';
|
| 300 |
-
default: return '❓';
|
| 301 |
}
|
|
|
|
| 302 |
};
|
| 303 |
|
| 304 |
return (
|
| 305 |
<div className="App">
|
| 306 |
<header className="App-header">
|
| 307 |
-
<h1>
|
| 308 |
-
<
|
| 309 |
-
<p
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
<div
|
| 315 |
-
<
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
{
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
<button onClick={requestLocationPermission} className="permission-button location-btn">
|
| 337 |
-
Grant Location Access
|
| 338 |
-
</button>
|
| 339 |
-
)}
|
| 340 |
-
{locationStatus === 'unsupported' && <span className="unsupported-text">Not supported in this browser</span>}
|
| 341 |
-
</div>
|
| 342 |
</div>
|
| 343 |
-
|
| 344 |
-
<
|
| 345 |
-
|
| 346 |
-
<
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
{!showPermissions && (
|
| 352 |
-
<div className="permissions-ready">
|
| 353 |
-
<p>✅ Permissions ready! You can now scan headsets.</p>
|
| 354 |
</div>
|
| 355 |
)}
|
| 356 |
-
|
| 357 |
-
<div className="scan-info">
|
| 358 |
-
<p>Put your Bluetooth headset in pairing mode and select a sense type to scan:</p>
|
| 359 |
-
</div>
|
| 360 |
-
|
| 361 |
-
<div className="senses-grid">
|
| 362 |
-
{Object.keys(senses).map(key => (
|
| 363 |
-
<div key={key} className="sense-card">
|
| 364 |
-
<div className="sense-icon" style={{ color: senses[key].color }}>
|
| 365 |
-
{senses[key].icon}
|
| 366 |
-
</div>
|
| 367 |
-
<h3>{senses[key].name}</h3>
|
| 368 |
-
<p>{senses[key].description}</p>
|
| 369 |
-
{getSenseButton(key)}
|
| 370 |
-
</div>
|
| 371 |
-
))}
|
| 372 |
-
</div>
|
| 373 |
-
|
| 374 |
-
{isScanning && (
|
| 375 |
-
<div className="scanning-status">
|
| 376 |
-
<div className="loading-spinner"></div>
|
| 377 |
-
<div className="loading-text">
|
| 378 |
-
<p>Scanning for {senses[selectedSense]?.name}...</p>
|
| 379 |
-
{connectedDevice && <p>Connected to: {connectedDevice}</p>}
|
| 380 |
-
<small>Ensure headset is powered on and discoverable.</small>
|
| 381 |
-
</div>
|
| 382 |
-
</div>
|
| 383 |
-
)}
|
| 384 |
-
|
| 385 |
-
{error && (
|
| 386 |
-
<div className="error-container">
|
| 387 |
-
<div className="error">
|
| 388 |
-
<strong>⚠️ Headset Error:</strong> {error}
|
| 389 |
-
</div>
|
| 390 |
-
<button onClick={clearResults} className="retry-button">
|
| 391 |
-
Retry Scan
|
| 392 |
-
</button>
|
| 393 |
-
</div>
|
| 394 |
-
)}
|
| 395 |
-
|
| 396 |
-
{scanResults && (
|
| 397 |
-
<div className="results-container">
|
| 398 |
-
<div className="results">
|
| 399 |
-
<h3>📡 Headset Scan Results</h3>
|
| 400 |
-
<div className="result-header">
|
| 401 |
-
<strong>{scanResults.senseType}</strong>
|
| 402 |
-
<span className="status-badge">{scanResults.status}</span>
|
| 403 |
-
</div>
|
| 404 |
-
|
| 405 |
-
<div className="result-details">
|
| 406 |
-
<div className="detail-row">
|
| 407 |
-
<span className="label">Headset:</span>
|
| 408 |
-
<span className="value">{scanResults.deviceName}</span>
|
| 409 |
-
</div>
|
| 410 |
-
<div className="detail-row">
|
| 411 |
-
<span className="label">Connection:</span>
|
| 412 |
-
<span className="value">{scanResults.connection}</span>
|
| 413 |
-
</div>
|
| 414 |
-
<div className="detail-row">
|
| 415 |
-
<span className="label">Scan Time:</span>
|
| 416 |
-
<span className="value">{scanResults.timestamp}</span>
|
| 417 |
-
</div>
|
| 418 |
-
<div className="detail-row">
|
| 419 |
-
<span className="label">Pairing Tip:</span>
|
| 420 |
-
<span className="value">{scanResults.pairingTip}</span>
|
| 421 |
-
</div>
|
| 422 |
-
</div>
|
| 423 |
-
|
| 424 |
-
<div className="sensor-data">
|
| 425 |
-
<h4>Headset Sensor Readings:</h4>
|
| 426 |
-
{Object.entries(scanResults).map(([key, value]) => {
|
| 427 |
-
if (['deviceName', 'deviceId', 'senseType', 'status', 'timestamp', 'connection', 'pairingTip'].includes(key)) {
|
| 428 |
-
return null;
|
| 429 |
-
}
|
| 430 |
-
return (
|
| 431 |
-
<div key={key} className="sensor-reading">
|
| 432 |
-
<span className="sensor-label">{key.replace(/([A-Z])/g, ' $1').trim()}:</span>
|
| 433 |
-
<span className="sensor-value">{value}</span>
|
| 434 |
-
</div>
|
| 435 |
-
);
|
| 436 |
-
})}
|
| 437 |
-
</div>
|
| 438 |
-
|
| 439 |
-
<div className="result-actions">
|
| 440 |
-
<button onClick={clearResults} className="clear-button">
|
| 441 |
-
Clear Results
|
| 442 |
-
</button>
|
| 443 |
-
<button onClick={() => scanBluetoothHeadset(selectedSense)} className="rescan-button">
|
| 444 |
-
Rescan Headset
|
| 445 |
-
</button>
|
| 446 |
-
</div>
|
| 447 |
-
</div>
|
| 448 |
-
</div>
|
| 449 |
-
)}
|
| 450 |
-
</main>
|
| 451 |
-
|
| 452 |
-
<footer className="App-footer">
|
| 453 |
-
<div className="footer-content">
|
| 454 |
-
<p><strong>Copyright © nanoSOFT TECH</strong></p>
|
| 455 |
-
<p>Operating System: <strong>nCONNECT OS</strong> | BLE Headset Compatible</p>
|
| 456 |
-
<p className="version">Version 2.0 | For Audio & Bio-Sensor Headsets</p>
|
| 457 |
-
</div>
|
| 458 |
-
</footer>
|
| 459 |
</div>
|
| 460 |
);
|
| 461 |
};
|
|
|
|
| 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';
|
| 6 |
|
| 7 |
const App = () => {
|
| 8 |
const [isScanning, setIsScanning] = useState(false);
|
|
|
|
| 9 |
const [error, setError] = useState(null);
|
| 10 |
+
const [predictions, setPredictions] = useState([]);
|
| 11 |
+
const [model, setModel] = useState(null);
|
| 12 |
+
const [detectionInterval, setDetectionInterval] = useState(null);
|
| 13 |
+
const webcamRef = useRef(null);
|
| 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 user-facing neural scan
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.');
|
| 29 |
} catch (err) {
|
| 30 |
+
setError('Failed to load detection model.');
|
| 31 |
+
console.error('Model load error:', err);
|
| 32 |
}
|
| 33 |
+
};
|
| 34 |
+
loadModel();
|
| 35 |
+
}, []);
|
| 36 |
|
| 37 |
+
// Sensor action: Predict objects from video frames
|
| 38 |
+
const predictObjects = async () => {
|
| 39 |
+
if (webcamRef.current && model && isScanning) {
|
|
|
|
| 40 |
try {
|
| 41 |
+
const predictionsArray = await model.detect(webcamRef.current.video);
|
| 42 |
+
setPredictions(predictionsArray);
|
| 43 |
+
drawPredictions(predictionsArray); // Visual feedback
|
| 44 |
+
// Trigger aural/mental actions based on detections
|
| 45 |
+
if (predictionsArray.length > 0) {
|
| 46 |
+
console.log('Detected objects:', predictionsArray.map(p => p.class));
|
| 47 |
+
// Example: Play audio alert for "mental" response
|
| 48 |
+
// new Audio('alert.mp3').play(); // Add aural sensor here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
+
} catch (err) {
|
| 51 |
+
console.error('Detection error:', err);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
}
|
| 54 |
};
|
| 55 |
|
| 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 |
+
canvas.width = 640;
|
| 62 |
+
canvas.height = 480;
|
| 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 continuous detection
|
| 77 |
+
const startScanning = async () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
try {
|
| 79 |
+
setIsScanning(true);
|
| 80 |
+
setError(null);
|
| 81 |
+
// Interval for sensor actions (every 500ms)
|
| 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 start sensor scan.');
|
| 87 |
+
console.error('Scan error:', err);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
}
|
| 89 |
};
|
| 90 |
|
| 91 |
+
// Stop scanning and clear detection
|
| 92 |
+
const stopScanning = () => {
|
| 93 |
+
setIsScanning(false);
|
| 94 |
+
if (detectionInterval) {
|
| 95 |
+
clearInterval(detectionInterval);
|
| 96 |
+
setDetectionInterval(null);
|
| 97 |
}
|
| 98 |
+
setPredictions([]);
|
| 99 |
+
if (webcamRef.current && webcamRef.current.stream) {
|
| 100 |
+
webcamRef.current.stream.getTracks().forEach(track => track.stop());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
}
|
| 102 |
+
console.log('Sensor scan stopped – Neural processing halted.');
|
| 103 |
};
|
| 104 |
|
| 105 |
return (
|
| 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 ? (
|
| 113 |
+
<button onClick={startScanning}>Activate Neural Sensor (Front Camera)</button>
|
| 114 |
+
) : (
|
| 115 |
+
<div>
|
| 116 |
+
<div style={{ position: 'relative', display: 'inline-block' }}>
|
| 117 |
+
<Webcam
|
| 118 |
+
audio={false}
|
| 119 |
+
ref={webcamRef}
|
| 120 |
+
videoConstraints={videoConstraints}
|
| 121 |
+
onUser Media={() => console.log('Front camera feed ready for neural analysis.')}
|
| 122 |
+
mirrored={true}
|
| 123 |
+
style={{ width: '100%', maxWidth: '640px' }}
|
| 124 |
+
/>
|
| 125 |
+
<canvas
|
| 126 |
+
ref={canvasRef}
|
| 127 |
+
style={{
|
| 128 |
+
position: 'absolute',
|
| 129 |
+
top: 0,
|
| 130 |
+
left: 0,
|
| 131 |
+
width: '640px',
|
| 132 |
+
height: '480px',
|
| 133 |
+
pointerEvents: 'none',
|
| 134 |
+
border: '2px solid #00ff00'
|
| 135 |
+
}}
|
| 136 |
+
/>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
</div>
|
| 138 |
+
<br />
|
| 139 |
+
<button onClick={stopScanning}>Deactivate Sensor</button>
|
| 140 |
+
<p>Sensor active: Detecting objects in real-time. Predictions: {predictions.length}</p>
|
| 141 |
+
<ul>
|
| 142 |
+
{predictions.map((pred, index) => (
|
| 143 |
+
<li key={index}>{pred.class} - Confidence: {Math.round(pred.score * 100)}%</li>
|
| 144 |
+
))}
|
| 145 |
+
</ul>
|
|
|
|
|
|
|
|
|
|
| 146 |
</div>
|
| 147 |
)}
|
| 148 |
+
</header>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
</div>
|
| 150 |
);
|
| 151 |
};
|