File size: 5,669 Bytes
07eae2b
 
 
 
 
e5b2d3c
07eae2b
 
 
 
 
 
 
 
595ba02
07eae2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
595ba02
07eae2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
595ba02
07eae2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
595ba02
07eae2b
595ba02
 
 
07eae2b
 
 
 
 
 
 
595ba02
07eae2b
595ba02
 
 
 
07eae2b
 
 
 
 
595ba02
07eae2b
 
 
 
 
 
13c5500
 
 
 
 
 
 
 
 
07eae2b
 
 
 
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
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hand Detector - By Rizki Ardianasyah</title>
    
    <script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>

    <style>
        /* Desain simpel dark mode ala progamer */
        body {
            background-color: #0f172a; 
            color: white;
            font-family: sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 20px;
        }
        .monitor-container {
            position: relative;
            margin-top: 20px;
        }
        /* Efek neon biru dan pink pada kanvas */
        canvas {
            border: 2px solid #3b82f6; 
            box-shadow: 0 0 15px #ec4899; 
            border-radius: 8px;
            background-color: #000;
            max-width: 100%;
        }
        /* GIF diletakkan mengambang di tengah kanvas */
        #responGif {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            display: none; 
            border-radius: 10px;
            z-index: 10;
            width: 250px;
        }
        button {
            background-color: #ec4899;
            color: white;
            border: none;
            padding: 12px 24px;
            font-size: 16px;
            border-radius: 5px;
            cursor: pointer;
            margin-bottom: 10px;
            font-weight: bold;
        }
        button:hover { background-color: #db2777; }
    </style>
</head>
<body>

    <h2>✋ Real-Time Hand Detector</h2>
    <p>Data Pengguna Diproses Langsung di Browser Anda.</p>

    <button id="startBtn">Mulai Kamera & Izinkan Suara</button>

    <div class="monitor-container">
        <video id="videoElement" style="display: none;" autoplay playsinline></video>
        <canvas id="outputCanvas" width="640" height="480"></canvas>
        <img id="responGif" src="animasi.gif" alt="Animasi AI">
    </div>

    <audio id="responAudio" src="suara.mp3"></audio>

    <script>
        const videoElement = document.getElementById('videoElement');
        const canvasElement = document.getElementById('outputCanvas');
        const canvasCtx = canvasElement.getContext('2d');
        const responGif = document.getElementById('responGif');
        const responAudio = document.getElementById('responAudio');
        const startBtn = document.getElementById('startBtn');
        
        let lastTriggerTime = 0;

        // Inisialisasi MediaPipe Hands
        const hands = new Hands({locateFile: (file) => {
            return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
        }});

        hands.setOptions({
            maxNumHands: 1,
            modelComplexity: 0, 
            minDetectionConfidence: 0.5,
            minTrackingConfidence: 0.5
        });

        // Logika saat AI mendapatkan frame video
        hands.onResults((results) => {
            canvasCtx.save();
            canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
            canvasCtx.drawImage(results.image, 0, 0, canvasElement.width, canvasElement.height);

            let handDetected = false;
            
            if (results.multiHandLandmarks && results.multiHandLandmarks.length > 0) {
                handDetected = true;
                for (const landmarks of results.multiHandLandmarks) {
                    drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, {color: '#ec4899', lineWidth: 4});
                    drawLandmarks(canvasCtx, landmarks, {color: '#3b82f6', lineWidth: 2});
                }
            }
            canvasCtx.restore();

            // Logika Trigger Baru
            const currentTime = Date.now() / 1000;
            
            // Jeda diubah menjadi 14 detik agar tidak bentrok
            if (handDetected && (currentTime - lastTriggerTime > 14)) {
                
                // Tampilkan GIF dan putar suara
                responGif.style.display = 'block';
                responAudio.currentTime = 0;
                responAudio.play();
                lastTriggerTime = currentTime;

                // Set timer persis 14 detik (14000 ms)
                setTimeout(() => {
                    responGif.style.display = 'none'; // Sembunyikan GIF
                    responAudio.pause();              // Matikan paksa suara (jaga-jaga jika file asli > 14 detik)
                    responAudio.currentTime = 0;      // Kembalikan suara ke detik 0
                }, 14000);
            }
        });

        // Event saat tombol ditekan
        startBtn.addEventListener('click', () => {
            startBtn.style.display = 'none'; 
            
            responAudio.play().then(() => { 
                responAudio.pause(); 
                responAudio.currentTime = 0; 
            }).catch(()=>console.log("Audio siap"));

           // Nyalakan Kamera (Versi Ringan)
        const camera = new Camera(videoElement, {
            onFrame: async () => {
                await hands.send({image: videoElement});
            },
            width: 320,  // Ubah dari 640 menjadi 320
            height: 240  // Ubah dari 480 menjadi 240
        });
        camera.start();
        });
    </script>
</body>
</html>