import cv2 import mediapipe as mp import numpy as np import math import pyautogui import ctypes # Set up volume control parameters min_distance = 20 # Minimum distance between thumb and index finger max_distance = 200 # Maximum distance between thumb and index finger min_volume = 0 # Minimum volume level max_volume = 100 # Maximum volume level # Set up screen resolution user32 = ctypes.windll.user32 screen_width, screen_height = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1) # Initialize Mediapipe mp_drawing = mp.solutions.drawing_utils mp_hands = mp.solutions.hands # Initialize PyAutoGUI pyautogui.FAILSAFE = False # Initialize hand tracking hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.7) # Main loop cap = cv2.VideoCapture(0) while cap.isOpened(): success, frame = cap.read() if not success: print("Failed to read video frame") break # Flip the frame horizontally for a mirror effect frame = cv2.flip(frame, 1) # Convert the frame from BGR to RGB frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Process the frame with hand tracking results = hands.process(frame_rgb) # Check if hands are detected if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: # Get the landmarks for the thumb and index finger thumb = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP] index = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP] # Convert the landmark positions to screen coordinates thumb_x, thumb_y = int(thumb.x * screen_width), int(thumb.y * screen_height) index_x, index_y = int(index.x * screen_width), int(index.y * screen_height) # Calculate the Euclidean distance between thumb and index finger distance = math.sqrt((thumb_x - index_x) ** 2 + (thumb_y - index_y) ** 2) # Map the distance to the volume range volume = np.interp(distance, [min_distance, max_distance], [min_volume, max_volume]) # Set the system volume using PyAutoGUI pyautogui.press("volumedown") # Decrease volume (optional) pyautogui.press("volumemute") # Mute volume (optional) pyautogui.press("volumeup") # Increase volume (optional) pyautogui.press("volumeup") # Increase volume (optional) pyautogui.press("volumeup") # Increase volume (optional) pyautogui.press("volumeup") # Increase volume (optional) pyautogui.press("volumeup") # Increase volume (optional) # Uncomment the following line to set the volume directly # pyautogui.press(f"volumedown {volume}") # Draw the thumb and index finger landmarks cv2.circle(frame, (thumb_x, thumb_y), 5, (0, 255, 0), -1) cv2.circle(frame, (index_x, index_y), 5, (0, 255, 0), -1) # Draw a line between thumb and index finger cv2.line(frame, (thumb_x, thumb_y), (index_x, index_y), (0, 255, 0), 2) # Display the frame cv2.imshow('Hand Volume Control', frame) # Exit if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture and destroy windows cap.release() cv2.destroyAllWindows()
Control Volume with Hand using Python Opencv
Control Volume with Hand using Python Opencv