Note - Double Click to Copy Code Contact Us!

Control Volume with Hand using Python Opencv

Tech Doubility
Control Volume with Hand using Python Opencv


  1. import cv2
  2. import mediapipe as mp
  3. import numpy as np
  4. import math
  5. import pyautogui
  6. import ctypes
  7.  
  8. # Set up volume control parameters
  9. min_distance = 20 # Minimum distance between thumb and index finger
  10. max_distance = 200 # Maximum distance between thumb and index finger
  11. min_volume = 0 # Minimum volume level
  12. max_volume = 100 # Maximum volume level
  13.  
  14. # Set up screen resolution
  15. user32 = ctypes.windll.user32
  16. screen_width, screen_height = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
  17.  
  18. # Initialize Mediapipe
  19. mp_drawing = mp.solutions.drawing_utils
  20. mp_hands = mp.solutions.hands
  21.  
  22. # Initialize PyAutoGUI
  23. pyautogui.FAILSAFE = False
  24.  
  25. # Initialize hand tracking
  26. hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.7)
  27.  
  28. # Main loop
  29. cap = cv2.VideoCapture(0)
  30. while cap.isOpened():
  31. success, frame = cap.read()
  32. if not success:
  33. print("Failed to read video frame")
  34. break
  35.  
  36. # Flip the frame horizontally for a mirror effect
  37. frame = cv2.flip(frame, 1)
  38.  
  39. # Convert the frame from BGR to RGB
  40. frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  41.  
  42. # Process the frame with hand tracking
  43. results = hands.process(frame_rgb)
  44.  
  45. # Check if hands are detected
  46. if results.multi_hand_landmarks:
  47. for hand_landmarks in results.multi_hand_landmarks:
  48. # Get the landmarks for the thumb and index finger
  49. thumb = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP]
  50. index = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
  51.  
  52. # Convert the landmark positions to screen coordinates
  53. thumb_x, thumb_y = int(thumb.x * screen_width), int(thumb.y * screen_height)
  54. index_x, index_y = int(index.x * screen_width), int(index.y * screen_height)
  55.  
  56. # Calculate the Euclidean distance between thumb and index finger
  57. distance = math.sqrt((thumb_x - index_x) ** 2 + (thumb_y - index_y) ** 2)
  58.  
  59. # Map the distance to the volume range
  60. volume = np.interp(distance, [min_distance, max_distance], [min_volume, max_volume])
  61.  
  62. # Set the system volume using PyAutoGUI
  63. pyautogui.press("volumedown") # Decrease volume (optional)
  64. pyautogui.press("volumemute") # Mute volume (optional)
  65. pyautogui.press("volumeup") # Increase volume (optional)
  66. pyautogui.press("volumeup") # Increase volume (optional)
  67. pyautogui.press("volumeup") # Increase volume (optional)
  68. pyautogui.press("volumeup") # Increase volume (optional)
  69. pyautogui.press("volumeup") # Increase volume (optional)
  70.  
  71. # Uncomment the following line to set the volume directly
  72. # pyautogui.press(f"volumedown {volume}")
  73.  
  74. # Draw the thumb and index finger landmarks
  75. cv2.circle(frame, (thumb_x, thumb_y), 5, (0, 255, 0), -1)
  76. cv2.circle(frame, (index_x, index_y), 5, (0, 255, 0), -1)
  77.  
  78. # Draw a line between thumb and index finger
  79. cv2.line(frame, (thumb_x, thumb_y), (index_x, index_y), (0, 255, 0), 2)
  80.  
  81. # Display the frame
  82. cv2.imshow('Hand Volume Control', frame)
  83.  
  84. # Exit if 'q' is pressed
  85. if cv2.waitKey(1) & 0xFF == ord('q'):
  86. break
  87.  
  88. # Release the video capture and destroy windows
  89. cap.release()
  90. cv2.destroyAllWindows()
  91.  

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.