Spaces:
Running
Running
start detection button work and provide report
Browse files- body-detection.html +107 -9
body-detection.html
CHANGED
|
@@ -36,7 +36,11 @@
|
|
| 36 |
Stop Detection
|
| 37 |
</button>
|
| 38 |
</div>
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
<div class="bg-white rounded-lg shadow-md p-6">
|
| 42 |
<h2 class="text-xl font-semibold mb-4">Detection Results</h2>
|
|
@@ -52,7 +56,8 @@
|
|
| 52 |
</div>
|
| 53 |
</div>
|
| 54 |
</main>
|
| 55 |
-
|
|
|
|
| 56 |
<script>
|
| 57 |
// Camera and canvas setup
|
| 58 |
const video = document.getElementById('video');
|
|
@@ -62,6 +67,11 @@
|
|
| 62 |
const stopBtn = document.getElementById('stopBtn');
|
| 63 |
const keypointsEl = document.getElementById('keypoints');
|
| 64 |
const postureEl = document.getElementById('posture');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
// Camera access
|
| 67 |
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
|
@@ -71,18 +81,106 @@
|
|
| 71 |
});
|
| 72 |
}
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
});
|
| 80 |
|
| 81 |
stopBtn.addEventListener('click', () => {
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
});
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
feather.replace();
|
| 87 |
</script>
|
| 88 |
</body>
|
|
|
|
| 36 |
Stop Detection
|
| 37 |
</button>
|
| 38 |
</div>
|
| 39 |
+
<div class="mt-4 p-4 bg-gray-50 rounded-lg">
|
| 40 |
+
<h3 class="font-medium mb-2">Detection Report</h3>
|
| 41 |
+
<p class="text-sm text-gray-600">Click "Generate Report" after detection to save your results.</p>
|
| 42 |
+
</div>
|
| 43 |
+
</div>
|
| 44 |
|
| 45 |
<div class="bg-white rounded-lg shadow-md p-6">
|
| 46 |
<h2 class="text-xl font-semibold mb-4">Detection Results</h2>
|
|
|
|
| 56 |
</div>
|
| 57 |
</div>
|
| 58 |
</main>
|
| 59 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
|
| 60 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/pose-detection"></script>
|
| 61 |
<script>
|
| 62 |
// Camera and canvas setup
|
| 63 |
const video = document.getElementById('video');
|
|
|
|
| 67 |
const stopBtn = document.getElementById('stopBtn');
|
| 68 |
const keypointsEl = document.getElementById('keypoints');
|
| 69 |
const postureEl = document.getElementById('posture');
|
| 70 |
+
|
| 71 |
+
let detector;
|
| 72 |
+
let isDetecting = false;
|
| 73 |
+
let detectionInterval;
|
| 74 |
+
let lastReport = {};
|
| 75 |
|
| 76 |
// Camera access
|
| 77 |
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
|
|
|
| 81 |
});
|
| 82 |
}
|
| 83 |
|
| 84 |
+
async function setupDetector() {
|
| 85 |
+
await tf.ready();
|
| 86 |
+
const model = poseDetection.SupportedModels.MoveNet;
|
| 87 |
+
detector = await poseDetection.createDetector(model);
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
function drawKeypoints(keypoints) {
|
| 91 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 92 |
+
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 93 |
+
|
| 94 |
+
keypoints.forEach(keypoint => {
|
| 95 |
+
if (keypoint.score > 0.3) {
|
| 96 |
+
ctx.beginPath();
|
| 97 |
+
ctx.arc(keypoint.x, keypoint.y, 5, 0, 2 * Math.PI);
|
| 98 |
+
ctx.fillStyle = 'red';
|
| 99 |
+
ctx.fill();
|
| 100 |
+
}
|
| 101 |
+
});
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
function analyzePosture(keypoints) {
|
| 105 |
+
// Simple posture analysis
|
| 106 |
+
const nose = keypoints.find(k => k.name === 'nose');
|
| 107 |
+
const leftShoulder = keypoints.find(k => k.name === 'left_shoulder');
|
| 108 |
+
const rightShoulder = keypoints.find(k => k.name === 'right_shoulder');
|
| 109 |
+
|
| 110 |
+
if (!nose || !leftShoulder || !rightShoulder) return "Incomplete data";
|
| 111 |
+
|
| 112 |
+
const shoulderSlope = (rightShoulder.y - leftShoulder.y) /
|
| 113 |
+
(rightShoulder.x - leftShoulder.x);
|
| 114 |
+
|
| 115 |
+
if (Math.abs(shoulderSlope) > 0.2) {
|
| 116 |
+
return shoulderSlope > 0 ? "Leaning to the left" : "Leaning to the right";
|
| 117 |
+
}
|
| 118 |
+
return "Good posture";
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
async function detectPose() {
|
| 122 |
+
if (!detector) return;
|
| 123 |
+
|
| 124 |
+
const poses = await detector.estimatePoses(video);
|
| 125 |
+
if (poses.length > 0) {
|
| 126 |
+
const keypoints = poses[0].keypoints;
|
| 127 |
+
drawKeypoints(keypoints);
|
| 128 |
+
|
| 129 |
+
// Update report
|
| 130 |
+
lastReport = {
|
| 131 |
+
keypoints: keypoints.map(k => `${k.name} (${k.score.toFixed(2)})`).join(', '),
|
| 132 |
+
posture: analyzePosture(keypoints),
|
| 133 |
+
timestamp: new Date().toLocaleTimeString()
|
| 134 |
+
};
|
| 135 |
+
|
| 136 |
+
keypointsEl.textContent = `Detected: ${lastReport.keypoints}`;
|
| 137 |
+
postureEl.textContent = `Posture: ${lastReport.posture}`;
|
| 138 |
+
}
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
startBtn.addEventListener('click', async () => {
|
| 142 |
+
if (!detector) await setupDetector();
|
| 143 |
+
isDetecting = true;
|
| 144 |
+
detectionInterval = setInterval(detectPose, 100);
|
| 145 |
+
keypointsEl.textContent = "Detection started - processing...";
|
| 146 |
});
|
| 147 |
|
| 148 |
stopBtn.addEventListener('click', () => {
|
| 149 |
+
isDetecting = false;
|
| 150 |
+
clearInterval(detectionInterval);
|
| 151 |
+
keypointsEl.textContent = lastReport.keypoints || "Detection stopped";
|
| 152 |
+
postureEl.textContent = lastReport.posture ? `Posture: ${lastReport.posture}` : "No data available";
|
| 153 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 154 |
});
|
| 155 |
|
| 156 |
+
// Generate report button
|
| 157 |
+
const reportBtn = document.createElement('button');
|
| 158 |
+
reportBtn.textContent = 'Generate Report';
|
| 159 |
+
reportBtn.className = 'bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded';
|
| 160 |
+
reportBtn.addEventListener('click', () => {
|
| 161 |
+
if (!lastReport.keypoints) {
|
| 162 |
+
alert('No detection data available. Start detection first.');
|
| 163 |
+
return;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
const reportWindow = window.open('', '_blank');
|
| 167 |
+
reportWindow.document.write(`
|
| 168 |
+
<html><head><title>Body Detection Report</title></head>
|
| 169 |
+
<body style="font-family: Arial, sans-serif; padding: 20px;">
|
| 170 |
+
<h1>Body Detection Report</h1>
|
| 171 |
+
<p><strong>Timestamp:</strong> ${lastReport.timestamp}</p>
|
| 172 |
+
<h2>Keypoints Detected</h2>
|
| 173 |
+
<p>${lastReport.keypoints.replace(/, /g, '<br>')}</p>
|
| 174 |
+
<h2>Posture Analysis</h2>
|
| 175 |
+
<p>${lastReport.posture}</p>
|
| 176 |
+
<h2>Visualization</h2>
|
| 177 |
+
<img src="${canvas.toDataURL()}" width="640" style="border: 1px solid #ddd;">
|
| 178 |
+
</body></html>
|
| 179 |
+
`);
|
| 180 |
+
reportWindow.document.close();
|
| 181 |
+
});
|
| 182 |
+
|
| 183 |
+
document.querySelector('.flex.space-x-4').appendChild(reportBtn);
|
| 184 |
feather.replace();
|
| 185 |
</script>
|
| 186 |
</body>
|