Repository navigation
Expand file tree
/
Copy pathgesture.py
More file actions
132 lines (95 loc) · 3.56 KB
/
Copy pathgesture.py
File metadata and controls
132 lines (95 loc) · 3.56 KB
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
import cv2
import mediapipe as mp
from gtts import gTTS
import pygame
import threading
import time
import os
import warnings
warnings.filterwarnings("ignore")
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
# --- Fungsi untuk memainkan suara---
def play_audio(text):
filename = f"voice_{text.replace(' ', '_').lower()}.mp3"
tts = gTTS(text=text, lang='id')
tts.save(filename)
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
# Tunggu sampai suara selesai
while pygame.mixer.music.get_busy():
time.sleep(0.1)
pygame.mixer.quit()
os.remove(filename)
# --- Fungsi deteksi gesture sederhana ---
def detect_gesture(landmarks):
thumb_tip = landmarks.landmark[4].y
index_tip = landmarks.landmark[8].y
middle_tip = landmarks.landmark[12].y
ring_tip = landmarks.landmark[16].y
pinky_tip = landmarks.landmark[20].y
thumb_base = landmarks.landmark[2].y
index_base = landmarks.landmark[5].y
# jari telunjuk tegak
if (index_tip < index_base and middle_tip > index_base and
ring_tip > index_base and pinky_tip > index_base):
return "Salam"
# tangan terbuka
if (thumb_tip < thumb_base and index_tip < index_base and
middle_tip < index_base and ring_tip < index_base and pinky_tip < index_base):
return "Halo"
# tangan mengepal
if (index_tip > index_base and middle_tip > index_base and
ring_tip > index_base and pinky_tip > index_base):
return "Selamat"
# tanda love (telunjuk + kelingking)
if (index_tip < index_base and pinky_tip < index_base and
middle_tip > index_base and ring_tip > index_base):
return "Sukses"
return None
# --- Program utama ---
cap = cv2.VideoCapture(0)
last_gesture = None
last_time = 0
# Warna teks untuk tiap gesture
gesture_colors = {
"Salam": (0, 0, 225),
"Halo": (0, 165, 255),
"Selamat": (255, 0, 0),
"Sukses": (0, 0, 225)
}
with mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7) as hands:
while True:
ret, frame = cap.read()
if not ret:
continue
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb_frame)
gesture = None
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
gesture = detect_gesture(hand_landmarks)
if gesture:
text = gesture
color = gesture_colors.get(text, (255, 255, 255)) # warna default putih
font = cv2.FONT_HERSHEY_SIMPLEX
# Posisi pojok kiri atas
x, y = 50, 80
# Bayangan hitam tipis agar tetap terlihat
cv2.putText(frame, text, (x + 2, y + 2), font, 1.2, (0, 0, 0), 3, cv2.LINE_AA)
# Teks utama dengan warna sesuai gesture
cv2.putText(frame, text, (x, y), font, 1.2, color, 2, cv2.LINE_AA)
# Mainkan suara kalau gesture baru terdeteksi
if gesture != last_gesture and time.time() - last_time > 2:
threading.Thread(target=play_audio, args=(text,)).start()
last_gesture = gesture
last_time = time.time()
cv2.imshow("Hand Gesture Recognition", frame)
# Tekan ESC untuk keluar
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()