Download index.html from mulyani22/pose: direct link, hf CLI and curl.
- Browser
- Download file 5.73 kB
-
https://huggingface.co/spaces/mulyani22/pose/resolve/main/index.html
- Command line
-
hf download hf://spaces/mulyani22/pose/index.html
-
curl -L -o index.html https://huggingface.co/spaces/mulyani22/pose/resolve/main/index.html
5.73 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Teachable Machine Pose Recognition with Audio</title> | |
| <style> | |
| body { | |
| font-family: sans-serif; | |
| display: flex; | |
| flex-direction: column; | |
| align-items: center; | |
| justify-content: center; | |
| background-color: #f0f2f5; | |
| padding: 20px; | |
| } | |
| canvas { | |
| border: 4px solid #333; | |
| border-radius: 10px; | |
| box-shadow: 0 4px 10px rgba(0,0,0,0.2); | |
| background-color: #fff; | |
| } | |
| button { | |
| padding: 12px 24px; | |
| font-size: 18px; | |
| background-color: #4CAF50; | |
| color: white; | |
| border: none; | |
| border-radius: 5px; | |
| cursor: pointer; | |
| margin-bottom: 20px; | |
| } | |
| button:hover { | |
| background-color: #45a049; | |
| } | |
| #label-container { | |
| margin-top: 20px; | |
| font-size: 20px; | |
| font-weight: bold; | |
| text-align: center; | |
| } | |
| #label-container div { | |
| margin-bottom: 5px; | |
| padding: 5px 15px; | |
| background: #fff; | |
| border-radius: 20px; | |
| box-shadow: 0 2px 5px rgba(0,0,0,0.1); | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <h2>Teachable Machine Pose Model</h2> | |
| <button type="button" onclick="init()">Start Camera</button> | |
| <div><canvas id="canvas"></canvas></div> | |
| <div id="label-container"></div> | |
| <!-- Library TensorFlow & Teachable Machine --> | |
| <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/@teachablemachine/pose@0.8/dist/teachablemachine-pose.min.js"></script> | |
| <script type="text/javascript"> | |
| // Link model kamu | |
| const URL = "https://teachablemachine.withgoogle.com/models/Go3b8dlG1P/"; | |
| let model, webcam, ctx, labelContainer, maxPredictions; | |
| // Inisialisasi Audio | |
| const audioFiles = { | |
| "ai": new Audio('ai.mp3'), | |
| "love": new Audio('love.mp3'), | |
| "bby": new Audio('bby.mp3') | |
| }; | |
| let lastPlayedPose = ""; // Mencegah audio yang sama diputar berulang-ulang dengan cepat | |
| async function init() { | |
| const modelURL = URL + "model.json"; | |
| const metadataURL = URL + "metadata.json"; | |
| // Load model dan metadata | |
| model = await tmPose.load(modelURL, metadataURL); | |
| maxPredictions = model.getTotalClasses(); | |
| // Persiapan Webcam | |
| const size = 400; // Ukuran diperbesar agar lebih jelas | |
| const flip = true; | |
| webcam = new tmPose.Webcam(size, size, flip); | |
| await webcam.setup(); | |
| await webcam.play(); | |
| window.requestAnimationFrame(loop); | |
| // Setup DOM | |
| const canvas = document.getElementById("canvas"); | |
| canvas.width = size; canvas.height = size; | |
| ctx = canvas.getContext("2d"); | |
| labelContainer = document.getElementById("label-container"); | |
| labelContainer.innerHTML = ""; // Bersihkan kontainer | |
| for (let i = 0; i < maxPredictions; i++) { | |
| labelContainer.appendChild(document.createElement("div")); | |
| } | |
| } | |
| async function loop(timestamp) { | |
| webcam.update(); | |
| await predict(); | |
| window.requestAnimationFrame(loop); | |
| } | |
| async function predict() { | |
| // Prediksi Pose | |
| const { pose, posenetOutput } = await model.estimatePose(webcam.canvas); | |
| const prediction = await model.predict(posenetOutput); | |
| for (let i = 0; i < maxPredictions; i++) { | |
| const className = prediction[i].className; | |
| const probability = prediction[i].probability; | |
| labelContainer.childNodes[i].innerHTML = `<strong>${className}</strong>: ${Math.round(probability * 100)}%`; | |
| // LOGIKA SUARA | |
| // Jika tingkat keyakinan model di atas 95% | |
| if (probability > 0.95) { | |
| playAudio(className); | |
| } | |
| } | |
| drawPose(pose); | |
| } | |
| function playAudio(poseName) { | |
| // Hanya putar jika pose yang terdeteksi berbeda dari sebelumnya | |
| // ATAU jika audio yang sebelumnya sudah selesai diputar | |
| let audio = audioFiles[poseName]; | |
| if (audio && lastPlayedPose !== poseName) { | |
| // Hentikan semua audio lain yang mungkin sedang jalan agar tidak tumpang tindih | |
| Object.values(audioFiles).forEach(track => { | |
| track.pause(); | |
| track.currentTime = 0; | |
| }); | |
| audio.play().catch(e => console.log("Audio play blocked by browser. Click 'Start' first.")); | |
| lastPlayedPose = poseName; | |
| // Reset status 'lastPlayedPose' setelah 3 detik agar suara bisa bunyi lagi nanti | |
| audio.onended = () => { lastPlayedPose = ""; }; | |
| } | |
| } | |
| function drawPose(pose) { | |
| if (webcam.canvas) { | |
| ctx.drawImage(webcam.canvas, 0, 0); | |
| if (pose) { | |
| const minPartConfidence = 0.5; | |
| tmPose.drawKeypoints(pose.keypoints, minPartConfidence, ctx); | |
| tmPose.drawSkeleton(pose.keypoints, minPartConfidence, ctx); | |
| } | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> |