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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="id">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>AI Pose Recognition with Audio</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 10 |
+
text-align: center;
|
| 11 |
+
background-color: #1a1a1a;
|
| 12 |
+
color: white;
|
| 13 |
+
padding: 20px;
|
| 14 |
+
}
|
| 15 |
+
h2 { color: #00d4ff; }
|
| 16 |
+
#canvas-container {
|
| 17 |
+
position: relative;
|
| 18 |
+
display: inline-block;
|
| 19 |
+
border: 5px solid #333;
|
| 20 |
+
border-radius: 15px;
|
| 21 |
+
overflow: hidden;
|
| 22 |
+
background: #000;
|
| 23 |
+
}
|
| 24 |
+
#label-container {
|
| 25 |
+
margin-top: 20px;
|
| 26 |
+
display: flex;
|
| 27 |
+
justify-content: center;
|
| 28 |
+
gap: 15px;
|
| 29 |
+
}
|
| 30 |
+
#label-container div {
|
| 31 |
+
background: #333;
|
| 32 |
+
padding: 10px 20px;
|
| 33 |
+
border-radius: 8px;
|
| 34 |
+
font-size: 18px;
|
| 35 |
+
min-width: 120px;
|
| 36 |
+
}
|
| 37 |
+
button {
|
| 38 |
+
padding: 15px 35px;
|
| 39 |
+
font-size: 18px;
|
| 40 |
+
font-weight: bold;
|
| 41 |
+
cursor: pointer;
|
| 42 |
+
background: linear-gradient(45deg, #00d4ff, #0056b3);
|
| 43 |
+
color: white;
|
| 44 |
+
border: none;
|
| 45 |
+
border-radius: 50px;
|
| 46 |
+
transition: 0.3s;
|
| 47 |
+
margin-bottom: 20px;
|
| 48 |
+
box-shadow: 0 4px 15px rgba(0, 212, 255, 0.3);
|
| 49 |
+
}
|
| 50 |
+
button:hover {
|
| 51 |
+
transform: scale(1.05);
|
| 52 |
+
box-shadow: 0 6px 20px rgba(0, 212, 255, 0.5);
|
| 53 |
+
}
|
| 54 |
+
.active-pose {
|
| 55 |
+
border: 2px solid #00d4ff !important;
|
| 56 |
+
background: #0056b3 !important;
|
| 57 |
+
}
|
| 58 |
+
</style>
|
| 59 |
+
</head>
|
| 60 |
+
<body>
|
| 61 |
+
|
| 62 |
+
<h2>AI Pose Detector: Peace, Mantap, Metal</h2>
|
| 63 |
+
<p>Pastikan kamera aktif dan tekan tombol di bawah:</p>
|
| 64 |
+
|
| 65 |
+
<button type="button" onclick="init()">MULAI KAMERA</button>
|
| 66 |
+
|
| 67 |
+
<div id="status"></div>
|
| 68 |
+
|
| 69 |
+
<div id="canvas-container">
|
| 70 |
+
<canvas id="canvas"></canvas>
|
| 71 |
+
</div>
|
| 72 |
+
|
| 73 |
+
<div id="label-container"></div>
|
| 74 |
+
|
| 75 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script>
|
| 76 |
+
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/pose@0.8/dist/teachablemachine-pose.min.js"></script>
|
| 77 |
+
|
| 78 |
+
<script type="text/javascript">
|
| 79 |
+
// Link model dari Teachable Machine kamu
|
| 80 |
+
const URL = "https://teachablemachine.withgoogle.com/models/0Y2ifsgF4/";
|
| 81 |
+
let model, webcam, ctx, labelContainer, maxPredictions;
|
| 82 |
+
|
| 83 |
+
// Persiapan Audio
|
| 84 |
+
const sounds = {
|
| 85 |
+
"peace": new Audio("mp3peace.mp3"),
|
| 86 |
+
"mantap": new Audio("mp3mantap.mp3"),
|
| 87 |
+
"metal": new Audio("mp3metal.mp3")
|
| 88 |
+
};
|
| 89 |
+
|
| 90 |
+
let lastPlayedPose = ""; // Mencegah suara terulang terus menerus
|
| 91 |
+
let silenceTimer; // Timer untuk meriset status suara
|
| 92 |
+
|
| 93 |
+
async function init() {
|
| 94 |
+
const modelURL = URL + "model.json";
|
| 95 |
+
const metadataURL = URL + "metadata.json";
|
| 96 |
+
|
| 97 |
+
// Load model
|
| 98 |
+
model = await tmPose.load(modelURL, metadataURL);
|
| 99 |
+
maxPredictions = model.getTotalClasses();
|
| 100 |
+
|
| 101 |
+
// Setup Webcam
|
| 102 |
+
const size = 400;
|
| 103 |
+
const flip = true;
|
| 104 |
+
webcam = new tmPose.Webcam(size, size, flip);
|
| 105 |
+
await webcam.setup();
|
| 106 |
+
await webcam.play();
|
| 107 |
+
window.requestAnimationFrame(loop);
|
| 108 |
+
|
| 109 |
+
// Setup UI
|
| 110 |
+
const canvas = document.getElementById("canvas");
|
| 111 |
+
canvas.width = size; canvas.height = size;
|
| 112 |
+
ctx = canvas.getContext("2d");
|
| 113 |
+
labelContainer = document.getElementById("label-container");
|
| 114 |
+
labelContainer.innerHTML = ""; // Bersihkan container
|
| 115 |
+
for (let i = 0; i < maxPredictions; i++) {
|
| 116 |
+
labelContainer.appendChild(document.createElement("div"));
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
async function loop(timestamp) {
|
| 121 |
+
webcam.update();
|
| 122 |
+
await predict();
|
| 123 |
+
window.requestAnimationFrame(loop);
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
async function predict() {
|
| 127 |
+
const { pose, posenetOutput } = await model.estimatePose(webcam.canvas);
|
| 128 |
+
const prediction = await model.predict(posenetOutput);
|
| 129 |
+
|
| 130 |
+
for (let i = 0; i < maxPredictions; i++) {
|
| 131 |
+
const className = prediction[i].className; // Nama class asli: peace, mantap, metal
|
| 132 |
+
const probability = prediction[i].probability;
|
| 133 |
+
|
| 134 |
+
const element = labelContainer.childNodes[i];
|
| 135 |
+
element.innerHTML = `${className}: ${(probability * 100).toFixed(0)}%`;
|
| 136 |
+
|
| 137 |
+
// Logika Suara dan Highlight UI
|
| 138 |
+
if (probability > 0.90) { // Ambang batas 90% yakin
|
| 139 |
+
element.classList.add("active-pose");
|
| 140 |
+
|
| 141 |
+
// Mainkan suara jika pose berubah
|
| 142 |
+
if (lastPlayedPose !== className) {
|
| 143 |
+
playAudio(className);
|
| 144 |
+
}
|
| 145 |
+
} else {
|
| 146 |
+
element.classList.remove("active-pose");
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
drawPose(pose);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
function playAudio(poseName) {
|
| 153 |
+
const soundKey = poseName.toLowerCase();
|
| 154 |
+
if (sounds[soundKey]) {
|
| 155 |
+
// Stop suara lain yang sedang main (opsional)
|
| 156 |
+
Object.values(sounds).forEach(s => {
|
| 157 |
+
s.pause();
|
| 158 |
+
s.currentTime = 0;
|
| 159 |
+
});
|
| 160 |
+
|
| 161 |
+
// Putar suara baru
|
| 162 |
+
sounds[soundKey].play().catch(e => console.log("Izin audio diperlukan"));
|
| 163 |
+
lastPlayedPose = poseName;
|
| 164 |
+
|
| 165 |
+
// Reset 'lastPlayedPose' setelah 3 detik diam agar bisa bunyi lagi
|
| 166 |
+
clearTimeout(silenceTimer);
|
| 167 |
+
silenceTimer = setTimeout(() => {
|
| 168 |
+
lastPlayedPose = "";
|
| 169 |
+
}, 3000);
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
function drawPose(pose) {
|
| 174 |
+
if (webcam.canvas) {
|
| 175 |
+
ctx.drawImage(webcam.canvas, 0, 0);
|
| 176 |
+
if (pose) {
|
| 177 |
+
const minPartConfidence = 0.5;
|
| 178 |
+
tmPose.drawKeypoints(pose.keypoints, minPartConfidence, ctx);
|
| 179 |
+
tmPose.drawSkeleton(pose.keypoints, minPartConfidence, ctx);
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
</script>
|
| 184 |
+
</body>
|
| 185 |
+
</html>
|