Update index.html
Browse files- index.html +100 -18
index.html
CHANGED
|
@@ -1,19 +1,101 @@
|
|
| 1 |
-
<!
|
| 2 |
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Pose Recognition with Audio</title>
|
| 5 |
+
<style>
|
| 6 |
+
body { font-family: sans-serif; text-align: center; background: #f0f2f5; }
|
| 7 |
+
canvas { border: 4px solid #333; border-radius: 10px; margin-top: 20px; }
|
| 8 |
+
button { padding: 10px 25px; font-size: 18px; cursor: pointer; background: #007bff; color: white; border: none; border-radius: 5px; }
|
| 9 |
+
#label-container { margin-top: 20px; font-weight: bold; font-size: 20px; }
|
| 10 |
+
</style>
|
| 11 |
+
</head>
|
| 12 |
+
<body>
|
| 13 |
+
<h2>Teachable Machine Pose Model + Audio</h2>
|
| 14 |
+
<button type="button" onclick="init()">Mulai Kamera</button>
|
| 15 |
+
<div><canvas id="canvas"></canvas></div>
|
| 16 |
+
<div id="label-container"></div>
|
| 17 |
+
|
| 18 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script>
|
| 19 |
+
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/pose@0.8/dist/teachablemachine-pose.min.js"></script>
|
| 20 |
+
<script type="text/javascript">
|
| 21 |
+
const URL = "https://teachablemachine.withgoogle.com/models/irrvgnfP0/";
|
| 22 |
+
let model, webcam, ctx, labelContainer, maxPredictions;
|
| 23 |
+
|
| 24 |
+
const audioFiles = {
|
| 25 |
+
"kicawkicaw": new Audio('kicawkicaw.mp3'),
|
| 26 |
+
"semangat": new Audio('semangat.mp3'),
|
| 27 |
+
"berjuang": new Audio('berjuang.mp3'),
|
| 28 |
+
"sukses": new Audio('sukses.mp3'),
|
| 29 |
+
"hidup jokowi": new Audio('hidup_jokowi.mp3')
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
let lastPlayedClass = "";
|
| 33 |
+
|
| 34 |
+
async function init() {
|
| 35 |
+
const modelURL = URL + "model.json";
|
| 36 |
+
const metadataURL = URL + "metadata.json";
|
| 37 |
+
model = await tmPose.load(modelURL, metadataURL);
|
| 38 |
+
maxPredictions = model.getTotalClasses();
|
| 39 |
+
|
| 40 |
+
const size = 400;
|
| 41 |
+
const flip = true;
|
| 42 |
+
webcam = new tmPose.Webcam(size, size, flip);
|
| 43 |
+
await webcam.setup();
|
| 44 |
+
await webcam.play();
|
| 45 |
+
window.requestAnimationFrame(loop);
|
| 46 |
+
|
| 47 |
+
const canvas = document.getElementById("canvas");
|
| 48 |
+
canvas.width = size; canvas.height = size;
|
| 49 |
+
ctx = canvas.getContext("2d");
|
| 50 |
+
labelContainer = document.getElementById("label-container");
|
| 51 |
+
for (let i = 0; i < maxPredictions; i++) {
|
| 52 |
+
labelContainer.appendChild(document.createElement("div"));
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
async function loop(timestamp) {
|
| 57 |
+
webcam.update();
|
| 58 |
+
await predict();
|
| 59 |
+
window.requestAnimationFrame(loop);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
async function predict() {
|
| 63 |
+
const { pose, posenetOutput } = await model.estimatePose(webcam.canvas);
|
| 64 |
+
const prediction = await model.predict(posenetOutput);
|
| 65 |
+
|
| 66 |
+
for (let i = 0; i < maxPredictions; i++) {
|
| 67 |
+
const className = prediction[i].className;
|
| 68 |
+
const probability = prediction[i].probability;
|
| 69 |
+
labelContainer.childNodes[i].innerHTML = className + ": " + (probability * 100).toFixed(0) + "%";
|
| 70 |
+
|
| 71 |
+
if (probability > 0.90 && lastPlayedClass !== className) {
|
| 72 |
+
playVoice(className);
|
| 73 |
+
lastPlayedClass = className;
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
drawPose(pose);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
function playVoice(className) {
|
| 80 |
+
Object.values(audioFiles).forEach(audio => {
|
| 81 |
+
audio.pause();
|
| 82 |
+
audio.currentTime = 0;
|
| 83 |
+
});
|
| 84 |
+
if (audioFiles[className]) {
|
| 85 |
+
audioFiles[className].play().catch(e => console.log("Audio play blocked"));
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
function drawPose(pose) {
|
| 90 |
+
if (webcam.canvas) {
|
| 91 |
+
ctx.drawImage(webcam.canvas, 0, 0);
|
| 92 |
+
if (pose) {
|
| 93 |
+
const minPartConfidence = 0.5;
|
| 94 |
+
tmPose.drawKeypoints(pose.keypoints, minPartConfidence, ctx);
|
| 95 |
+
tmPose.drawSkeleton(pose.keypoints, minPartConfidence, ctx);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
</script>
|
| 100 |
+
</body>
|
| 101 |
+
</html>
|