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