Skip to content

Instantly share code, notes, and snippets.

@Ganesyk12
Created June 21, 2024 13:33
Show Gist options
  • Save Ganesyk12/c26612fc950992c862e9dba7ec66dfc1 to your computer and use it in GitHub Desktop.
Save Ganesyk12/c26612fc950992c862e9dba7ec66dfc1 to your computer and use it in GitHub Desktop.
Cursor mover with hand detector - trial
import cv2
import pyautogui
import mediapipe as mp
# Inisialisasi MediaPipe Hands
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
drawing_utils = mp.solutions.drawing_utils
# Inisialisasi webcam
cap = cv2.VideoCapture(0)
# 0 : default cam
# 1 : virtual cam
# 2 : webcam usb
# Posisi awal kursor
prev_x, prev_y = 0, 0
# Mendapatkan ukuran layar
screen_width, screen_height = pyautogui.size()
while True:
# Membaca frame dari webcam
ret, frame = cap.read()
if not ret:
break
# Membalik frame secara vertikal
frame = cv2.flip(frame, 1)
# Konversi gambar BGR ke RGB untuk MediaPipe
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Proses deteksi tangan dengan MediaPipe Hands
results = hands.process(rgb_frame)
# Periksa apakah ada tangan yang terdeteksi
if results.multi_hand_landmarks:
# Dapatkan landmark tangan pertama
hand_landmarks = results.multi_hand_landmarks[0]
# Temukan landmark ujung jari telunjuk
index_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
# Ubah koordinat landmark ke dalam frame
x = int(index_finger_tip.x * frame.shape[1])
y = int(index_finger_tip.y * frame.shape[0])
# Ubah koordinat frame ke koordinat layar
screen_x = int(index_finger_tip.x * screen_width)
screen_y = int(index_finger_tip.y * screen_height)
# Pindahkan kursor ke posisi baru
pyautogui.moveTo(screen_x, screen_y)
# Gambar landmark tangan di frame
drawing_utils.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Tampilkan frame
cv2.imshow('Deteksi Gerak Tangan', frame)
# Tekan 'q' untuk keluar
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Menutup webcam dan jendela
cap.release()
cv2.destroyAllWindows()
absl-py==2.1.0
attrs==23.2.0
cffi==1.16.0
contourpy==1.2.1
cycler==0.12.1
flatbuffers==24.3.25
fonttools==4.53.0
jax==0.4.30
jaxlib==0.4.30
kiwisolver==1.4.5
matplotlib==3.9.0
mediapipe==0.10.14
ml-dtypes==0.4.0
MouseInfo==0.1.3
numpy==2.0.0
opencv-contrib-python==4.10.0.84
opencv-python==4.10.0.84
opt-einsum==3.3.0
packaging==24.1
pillow==10.3.0
protobuf==4.25.3
PyAutoGUI==0.9.54
pycparser==2.22
PyGetWindow==0.0.9
PyMsgBox==1.0.9
pyparsing==3.1.2
pyperclip==1.9.0
PyRect==0.2.0
PyScreeze==0.1.30
python-dateutil==2.9.0.post0
pytweening==1.2.0
scipy==1.13.1
six==1.16.0
sounddevice==0.4.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment