Created
November 26, 2025 06:12
-
-
Save datfooldive/8d5aad594b8646506dc99b76b15af925 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import mediapipe as mp | |
| mp_hands = mp.solutions.hands | |
| mp_draw = mp.solutions.drawing_utils | |
| # Inisialisasi kamera | |
| cap = cv2.VideoCapture(0) | |
| # Model MediaPipe Hands | |
| hands = mp_hands.Hands( | |
| max_num_hands=2, | |
| min_detection_confidence=0.5, | |
| min_tracking_confidence=0.5 | |
| ) | |
| while True: | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| frame = cv2.flip(frame, 1) # Mirror like phone selfie | |
| h, w, _ = frame.shape | |
| # Convert ke RGB | |
| rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| results = hands.process(rgb) | |
| if results.multi_hand_landmarks: | |
| for hand_landmarks in results.multi_hand_landmarks: | |
| # Gambar struktur tangan + titik | |
| mp_draw.draw_landmarks( | |
| frame, | |
| hand_landmarks, | |
| mp_hands.HAND_CONNECTIONS | |
| ) | |
| # Contoh: Hitung jari yang terangkat | |
| # (menggunakan z-coordinate sederhana) | |
| finger_tips = [4, 8, 12, 16, 20] | |
| fingers_up = 0 | |
| for tip in finger_tips: | |
| tip_y = hand_landmarks.landmark[tip].y | |
| pip_y = hand_landmarks.landmark[tip - 2].y | |
| if tip_y < pip_y: | |
| fingers_up += 1 | |
| cv2.putText( | |
| frame, | |
| f"Fingers: {fingers_up}", | |
| (10, 50), | |
| cv2.FONT_HERSHEY_SIMPLEX, | |
| 1, | |
| (0, 255, 0), | |
| 2 | |
| ) | |
| cv2.imshow("Hand & Finger Detection", frame) | |
| if cv2.waitKey(1) & 0xFF == ord('q'): | |
| break | |
| cap.release() | |
| cv2.destroyAllWindows() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment