Spaces:
Running
Running
Update index.html
Browse files- index.html +73 -47
index.html
CHANGED
|
@@ -3,11 +3,13 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>Hand
|
| 7 |
|
|
|
|
| 8 |
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
|
| 9 |
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js" crossorigin="anonymous"></script>
|
| 10 |
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>
|
|
|
|
| 11 |
|
| 12 |
<style>
|
| 13 |
body {
|
|
@@ -23,7 +25,7 @@
|
|
| 23 |
}
|
| 24 |
|
| 25 |
h2 { margin-bottom: 5px; color: #38bdf8; text-shadow: 0 0 10px rgba(56, 189, 248, 0.5); }
|
| 26 |
-
p { color: #94a3b8; margin-top: 0; }
|
| 27 |
|
| 28 |
#startBtn {
|
| 29 |
background: linear-gradient(135deg, #ec4899, #8b5cf6);
|
|
@@ -75,9 +77,11 @@
|
|
| 75 |
height: 480px;
|
| 76 |
max-width: 90vw;
|
| 77 |
object-fit: cover;
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
-
/*
|
| 81 |
.pose1-anim {
|
| 82 |
position: absolute;
|
| 83 |
display: none;
|
|
@@ -87,13 +91,14 @@
|
|
| 87 |
box-shadow: 0 0 15px rgba(234, 179, 8, 0.8);
|
| 88 |
pointer-events: none;
|
| 89 |
transition: transform 0.1s linear;
|
|
|
|
| 90 |
}
|
| 91 |
</style>
|
| 92 |
</head>
|
| 93 |
<body>
|
| 94 |
|
| 95 |
-
<h2>🤖 AI Vision: Hand Gestures
|
| 96 |
-
<p>Deteksi Dua Tangan
|
| 97 |
|
| 98 |
<button id="startBtn">▶ Mulai Sistem Deteksi</button>
|
| 99 |
|
|
@@ -105,9 +110,8 @@
|
|
| 105 |
<video id="videoElement" style="display: none;" autoplay playsinline></video>
|
| 106 |
<canvas id="outputCanvas" width="640" height="480"></canvas>
|
| 107 |
|
| 108 |
-
<
|
| 109 |
-
<img id="
|
| 110 |
-
<img id="pose1Anim2" class="pose1-anim" src="animasi1.gif" alt="Pose 1 Tangan 2">
|
| 111 |
</div>
|
| 112 |
|
| 113 |
<audio id="audio1" src="suara1.mp3"></audio>
|
|
@@ -123,21 +127,15 @@
|
|
| 123 |
const startBtn = document.getElementById('startBtn');
|
| 124 |
const statusPose = document.getElementById('statusPose');
|
| 125 |
|
| 126 |
-
|
| 127 |
-
const anims = [
|
| 128 |
-
document.getElementById('pose1Anim1'),
|
| 129 |
-
document.getElementById('pose1Anim2')
|
| 130 |
-
];
|
| 131 |
-
|
| 132 |
const sounds = [
|
| 133 |
-
document.getElementById('audio1'),
|
| 134 |
-
document.getElementById('
|
| 135 |
-
document.getElementById('audio3'),
|
| 136 |
-
document.getElementById('audio4'),
|
| 137 |
document.getElementById('audio5')
|
| 138 |
];
|
| 139 |
|
| 140 |
-
let currentActivePoses = [];
|
|
|
|
| 141 |
|
| 142 |
function getDistance(p1, p2) {
|
| 143 |
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
|
|
@@ -161,12 +159,36 @@
|
|
| 161 |
return null;
|
| 162 |
}
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
const hands = new Hands({locateFile: (file) => {
|
| 165 |
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
|
| 166 |
}});
|
| 167 |
-
|
| 168 |
hands.setOptions({
|
| 169 |
-
maxNumHands: 2,
|
| 170 |
modelComplexity: 1,
|
| 171 |
minDetectionConfidence: 0.7,
|
| 172 |
minTrackingConfidence: 0.7
|
|
@@ -177,49 +199,55 @@
|
|
| 177 |
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
|
| 178 |
canvasCtx.drawImage(results.image, 0, 0, canvasElement.width, canvasElement.height);
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
let detectedPosesThisFrame = [];
|
| 181 |
-
|
| 182 |
-
// Sembunyikan semua GIF terlebih dahulu setiap frame
|
| 183 |
anims.forEach(a => a.style.display = 'none');
|
| 184 |
|
| 185 |
if (results.multiHandLandmarks && results.multiHandLandmarks.length > 0) {
|
| 186 |
-
// Looping untuk setiap tangan yang terdeteksi
|
| 187 |
results.multiHandLandmarks.forEach((landmarks, index) => {
|
| 188 |
-
// Beri warna berbeda: Tangan 1 (Pink), Tangan 2 (Cyan)
|
| 189 |
let boneColor = index === 0 ? '#ec4899' : '#06b6d4';
|
| 190 |
-
|
| 191 |
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, {color: boneColor, lineWidth: 4});
|
| 192 |
drawLandmarks(canvasCtx, landmarks, {color: '#ffffff', lineWidth: 2, radius: 3});
|
| 193 |
|
| 194 |
let pose = classifyPose(landmarks);
|
| 195 |
if (pose) {
|
| 196 |
detectedPosesThisFrame.push({
|
| 197 |
-
...pose,
|
| 198 |
-
handIndex: index,
|
| 199 |
-
wrist: landmarks[0] // Ambil koordinat pergelangan tangan
|
| 200 |
});
|
| 201 |
}
|
| 202 |
});
|
| 203 |
}
|
| 204 |
canvasCtx.restore();
|
| 205 |
|
| 206 |
-
// --- LOGIKA
|
| 207 |
if (detectedPosesThisFrame.length > 0) {
|
| 208 |
-
|
| 209 |
-
// 1. Update Teks Status (Menggabungkan nama pose jika ada 2 tangan)
|
| 210 |
-
// Menggunakan Set agar jika kedua tangan posenya sama, teksnya tidak berulang (contoh: "✌️ Victory")
|
| 211 |
let uniquePoseNames = [...new Set(detectedPosesThisFrame.map(p => p.name))];
|
| 212 |
statusPose.textContent = uniquePoseNames.join(" & ");
|
| 213 |
-
statusPose.style.color = detectedPosesThisFrame[0].color;
|
| 214 |
|
| 215 |
-
//
|
| 216 |
detectedPosesThisFrame.forEach((pose, i) => {
|
| 217 |
if (pose.id === 1 && anims[i]) {
|
| 218 |
const wrist = pose.wrist;
|
| 219 |
const canvasWidth = canvasElement.width;
|
| 220 |
const canvasHeight = canvasElement.height;
|
| 221 |
|
| 222 |
-
|
|
|
|
| 223 |
const topPos = (wrist.y * canvasHeight) + 30;
|
| 224 |
|
| 225 |
anims[i].style.display = 'block';
|
|
@@ -229,12 +257,10 @@
|
|
| 229 |
}
|
| 230 |
});
|
| 231 |
|
| 232 |
-
// 3. Logika Audio Dinamis
|
| 233 |
let currentIds = detectedPosesThisFrame.map(p => p.id);
|
| 234 |
let hasNewPose = false;
|
| 235 |
let newPoseId = null;
|
| 236 |
|
| 237 |
-
// Cek apakah ada tangan yang baru saja mengganti posenya
|
| 238 |
currentIds.forEach(id => {
|
| 239 |
if (!currentActivePoses.includes(id)) {
|
| 240 |
hasNewPose = true;
|
|
@@ -242,17 +268,14 @@
|
|
| 242 |
}
|
| 243 |
});
|
| 244 |
|
| 245 |
-
// Jika ada pose baru, putar suaranya
|
| 246 |
if (hasNewPose && newPoseId !== null) {
|
| 247 |
-
sounds.forEach(s => { s.pause(); s.currentTime = 0; });
|
| 248 |
const soundToPlay = sounds[newPoseId - 1];
|
| 249 |
if (soundToPlay) soundToPlay.play();
|
| 250 |
}
|
| 251 |
|
| 252 |
-
currentActivePoses = currentIds;
|
| 253 |
-
|
| 254 |
} else {
|
| 255 |
-
// Jika tidak ada tangan / tidak ada pose yang dikenali
|
| 256 |
if (currentActivePoses.length > 0) {
|
| 257 |
statusPose.textContent = "Menunggu formasi jari...";
|
| 258 |
statusPose.style.color = "#94a3b8";
|
|
@@ -261,17 +284,20 @@
|
|
| 261 |
}
|
| 262 |
});
|
| 263 |
|
|
|
|
|
|
|
|
|
|
| 264 |
startBtn.addEventListener('click', () => {
|
| 265 |
startBtn.style.display = 'none';
|
| 266 |
statusPose.textContent = "Menyiapkan Kamera...";
|
| 267 |
|
| 268 |
-
sounds.forEach(s => {
|
| 269 |
-
s.play().then(() => { s.pause(); s.currentTime = 0; }).catch(()=>{});
|
| 270 |
-
});
|
| 271 |
|
| 272 |
const camera = new Camera(videoElement, {
|
| 273 |
onFrame: async () => {
|
| 274 |
-
|
|
|
|
|
|
|
| 275 |
},
|
| 276 |
width: 640,
|
| 277 |
height: 480
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Hand & Face AI Vision - v16</title>
|
| 7 |
|
| 8 |
+
<!-- Library MediaPipe Wajib (Tambah Face Mesh) -->
|
| 9 |
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
|
| 10 |
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js" crossorigin="anonymous"></script>
|
| 11 |
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>
|
| 12 |
+
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/face_mesh.js" crossorigin="anonymous"></script>
|
| 13 |
|
| 14 |
<style>
|
| 15 |
body {
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
h2 { margin-bottom: 5px; color: #38bdf8; text-shadow: 0 0 10px rgba(56, 189, 248, 0.5); }
|
| 28 |
+
p { color: #94a3b8; margin-top: 0; text-align: center; }
|
| 29 |
|
| 30 |
#startBtn {
|
| 31 |
background: linear-gradient(135deg, #ec4899, #8b5cf6);
|
|
|
|
| 77 |
height: 480px;
|
| 78 |
max-width: 90vw;
|
| 79 |
object-fit: cover;
|
| 80 |
+
/* Membalik kanvas agar seperti cermin asli */
|
| 81 |
+
transform: scaleX(-1);
|
| 82 |
}
|
| 83 |
|
| 84 |
+
/* GIF Animasi - Juga dibalik agar posisinya cocok dengan cermin */
|
| 85 |
.pose1-anim {
|
| 86 |
position: absolute;
|
| 87 |
display: none;
|
|
|
|
| 91 |
box-shadow: 0 0 15px rgba(234, 179, 8, 0.8);
|
| 92 |
pointer-events: none;
|
| 93 |
transition: transform 0.1s linear;
|
| 94 |
+
transform: scaleX(-1);
|
| 95 |
}
|
| 96 |
</style>
|
| 97 |
</head>
|
| 98 |
<body>
|
| 99 |
|
| 100 |
+
<h2>🤖 AI Vision: Hand Gestures & Face Tracking</h2>
|
| 101 |
+
<p>Deteksi Dua Tangan Multi-Animasi + Face Mesh Presisi Tinggi (Visual Only)</p>
|
| 102 |
|
| 103 |
<button id="startBtn">▶ Mulai Sistem Deteksi</button>
|
| 104 |
|
|
|
|
| 110 |
<video id="videoElement" style="display: none;" autoplay playsinline></video>
|
| 111 |
<canvas id="outputCanvas" width="640" height="480"></canvas>
|
| 112 |
|
| 113 |
+
<img id="pose1Anim1" class="pose1-anim" src="animasi1.gif" alt="Pose 1">
|
| 114 |
+
<img id="pose1Anim2" class="pose1-anim" src="animasi1.gif" alt="Pose 2">
|
|
|
|
| 115 |
</div>
|
| 116 |
|
| 117 |
<audio id="audio1" src="suara1.mp3"></audio>
|
|
|
|
| 127 |
const startBtn = document.getElementById('startBtn');
|
| 128 |
const statusPose = document.getElementById('statusPose');
|
| 129 |
|
| 130 |
+
const anims = [document.getElementById('pose1Anim1'), document.getElementById('pose1Anim2')];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
const sounds = [
|
| 132 |
+
document.getElementById('audio1'), document.getElementById('audio2'),
|
| 133 |
+
document.getElementById('audio3'), document.getElementById('audio4'),
|
|
|
|
|
|
|
| 134 |
document.getElementById('audio5')
|
| 135 |
];
|
| 136 |
|
| 137 |
+
let currentActivePoses = [];
|
| 138 |
+
let currentFaceLandmarks = null; // Menyimpan data wajah terbaru
|
| 139 |
|
| 140 |
function getDistance(p1, p2) {
|
| 141 |
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
|
|
|
|
| 159 |
return null;
|
| 160 |
}
|
| 161 |
|
| 162 |
+
// ==========================================
|
| 163 |
+
// 1. INISIALISASI FACE MESH (AKURASI TINGGI)
|
| 164 |
+
// ==========================================
|
| 165 |
+
const faceMesh = new FaceMesh({locateFile: (file) => {
|
| 166 |
+
return `https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/${file}`;
|
| 167 |
+
}});
|
| 168 |
+
faceMesh.setOptions({
|
| 169 |
+
maxNumFaces: 1, // Fokus 1 wajah agar performa tetap cepat
|
| 170 |
+
refineLandmarks: true, // PENTING: Pelacakan ekstra akurat pada mata/bibir meski bergerak
|
| 171 |
+
minDetectionConfidence: 0.7,
|
| 172 |
+
minTrackingConfidence: 0.7
|
| 173 |
+
});
|
| 174 |
+
|
| 175 |
+
// Simpan titik wajah saat terdeteksi
|
| 176 |
+
faceMesh.onResults((results) => {
|
| 177 |
+
if (results.multiFaceLandmarks && results.multiFaceLandmarks.length > 0) {
|
| 178 |
+
currentFaceLandmarks = results.multiFaceLandmarks[0];
|
| 179 |
+
} else {
|
| 180 |
+
currentFaceLandmarks = null;
|
| 181 |
+
}
|
| 182 |
+
});
|
| 183 |
+
|
| 184 |
+
// ==========================================
|
| 185 |
+
// 2. INISIALISASI HANDS (SEKALIGUS RENDER)
|
| 186 |
+
// ==========================================
|
| 187 |
const hands = new Hands({locateFile: (file) => {
|
| 188 |
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
|
| 189 |
}});
|
|
|
|
| 190 |
hands.setOptions({
|
| 191 |
+
maxNumHands: 2,
|
| 192 |
modelComplexity: 1,
|
| 193 |
minDetectionConfidence: 0.7,
|
| 194 |
minTrackingConfidence: 0.7
|
|
|
|
| 199 |
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
|
| 200 |
canvasCtx.drawImage(results.image, 0, 0, canvasElement.width, canvasElement.height);
|
| 201 |
|
| 202 |
+
// --- A. GAMBAR WAJAH DAHULU (BACKGROUND) ---
|
| 203 |
+
if (currentFaceLandmarks) {
|
| 204 |
+
// Tesselation (Jaring-jaring wajah) - Biru Cyan
|
| 205 |
+
drawConnectors(canvasCtx, currentFaceLandmarks, FACEMESH_TESSELATION, {color: '#06b6d4', lineWidth: 0.5});
|
| 206 |
+
|
| 207 |
+
// Kontur Utama Wajah - Pink Neon tebal
|
| 208 |
+
drawConnectors(canvasCtx, currentFaceLandmarks, FACEMESH_RIGHT_EYE, {color: '#ec4899', lineWidth: 2});
|
| 209 |
+
drawConnectors(canvasCtx, currentFaceLandmarks, FACEMESH_RIGHT_EYEBROW, {color: '#ec4899', lineWidth: 2});
|
| 210 |
+
drawConnectors(canvasCtx, currentFaceLandmarks, FACEMESH_LEFT_EYE, {color: '#ec4899', lineWidth: 2});
|
| 211 |
+
drawConnectors(canvasCtx, currentFaceLandmarks, FACEMESH_LEFT_EYEBROW, {color: '#ec4899', lineWidth: 2});
|
| 212 |
+
drawConnectors(canvasCtx, currentFaceLandmarks, FACEMESH_FACE_OVAL, {color: '#ec4899', lineWidth: 2});
|
| 213 |
+
drawConnectors(canvasCtx, currentFaceLandmarks, FACEMESH_LIPS, {color: '#ec4899', lineWidth: 2});
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// --- B. GAMBAR TANGAN (FOREGROUND) ---
|
| 217 |
let detectedPosesThisFrame = [];
|
|
|
|
|
|
|
| 218 |
anims.forEach(a => a.style.display = 'none');
|
| 219 |
|
| 220 |
if (results.multiHandLandmarks && results.multiHandLandmarks.length > 0) {
|
|
|
|
| 221 |
results.multiHandLandmarks.forEach((landmarks, index) => {
|
|
|
|
| 222 |
let boneColor = index === 0 ? '#ec4899' : '#06b6d4';
|
|
|
|
| 223 |
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, {color: boneColor, lineWidth: 4});
|
| 224 |
drawLandmarks(canvasCtx, landmarks, {color: '#ffffff', lineWidth: 2, radius: 3});
|
| 225 |
|
| 226 |
let pose = classifyPose(landmarks);
|
| 227 |
if (pose) {
|
| 228 |
detectedPosesThisFrame.push({
|
| 229 |
+
...pose, handIndex: index, wrist: landmarks[0]
|
|
|
|
|
|
|
| 230 |
});
|
| 231 |
}
|
| 232 |
});
|
| 233 |
}
|
| 234 |
canvasCtx.restore();
|
| 235 |
|
| 236 |
+
// --- C. LOGIKA UI & AUDIO ---
|
| 237 |
if (detectedPosesThisFrame.length > 0) {
|
|
|
|
|
|
|
|
|
|
| 238 |
let uniquePoseNames = [...new Set(detectedPosesThisFrame.map(p => p.name))];
|
| 239 |
statusPose.textContent = uniquePoseNames.join(" & ");
|
| 240 |
+
statusPose.style.color = detectedPosesThisFrame[0].color;
|
| 241 |
|
| 242 |
+
// Penempatan Dinamis GIF
|
| 243 |
detectedPosesThisFrame.forEach((pose, i) => {
|
| 244 |
if (pose.id === 1 && anims[i]) {
|
| 245 |
const wrist = pose.wrist;
|
| 246 |
const canvasWidth = canvasElement.width;
|
| 247 |
const canvasHeight = canvasElement.height;
|
| 248 |
|
| 249 |
+
// Karena kanvas dibalik (Cermin), perhitungan X ikut dibalik
|
| 250 |
+
const leftPos = ((1 - wrist.x) * canvasWidth) - (150 / 2);
|
| 251 |
const topPos = (wrist.y * canvasHeight) + 30;
|
| 252 |
|
| 253 |
anims[i].style.display = 'block';
|
|
|
|
| 257 |
}
|
| 258 |
});
|
| 259 |
|
|
|
|
| 260 |
let currentIds = detectedPosesThisFrame.map(p => p.id);
|
| 261 |
let hasNewPose = false;
|
| 262 |
let newPoseId = null;
|
| 263 |
|
|
|
|
| 264 |
currentIds.forEach(id => {
|
| 265 |
if (!currentActivePoses.includes(id)) {
|
| 266 |
hasNewPose = true;
|
|
|
|
| 268 |
}
|
| 269 |
});
|
| 270 |
|
|
|
|
| 271 |
if (hasNewPose && newPoseId !== null) {
|
| 272 |
+
sounds.forEach(s => { s.pause(); s.currentTime = 0; });
|
| 273 |
const soundToPlay = sounds[newPoseId - 1];
|
| 274 |
if (soundToPlay) soundToPlay.play();
|
| 275 |
}
|
| 276 |
|
| 277 |
+
currentActivePoses = currentIds;
|
|
|
|
| 278 |
} else {
|
|
|
|
| 279 |
if (currentActivePoses.length > 0) {
|
| 280 |
statusPose.textContent = "Menunggu formasi jari...";
|
| 281 |
statusPose.style.color = "#94a3b8";
|
|
|
|
| 284 |
}
|
| 285 |
});
|
| 286 |
|
| 287 |
+
// ==========================================
|
| 288 |
+
// 3. EVENT MULAI KAMERA (SYNCHRONIZE DUAL AI)
|
| 289 |
+
// ==========================================
|
| 290 |
startBtn.addEventListener('click', () => {
|
| 291 |
startBtn.style.display = 'none';
|
| 292 |
statusPose.textContent = "Menyiapkan Kamera...";
|
| 293 |
|
| 294 |
+
sounds.forEach(s => { s.play().then(() => { s.pause(); s.currentTime = 0; }).catch(()=>{}); });
|
|
|
|
|
|
|
| 295 |
|
| 296 |
const camera = new Camera(videoElement, {
|
| 297 |
onFrame: async () => {
|
| 298 |
+
// Rahasia Sinkronisasi Tanpa Lag:
|
| 299 |
+
await faceMesh.send({image: videoElement}); // Proses wajah di background
|
| 300 |
+
await hands.send({image: videoElement}); // Render tangan dan wajah sekaligus ke layar
|
| 301 |
},
|
| 302 |
width: 640,
|
| 303 |
height: 480
|