Skip to content

Commit

Permalink
volume up and down
Browse files Browse the repository at this point in the history
  • Loading branch information
tul53850 committed Mar 26, 2024
1 parent 38878cb commit 3fd4c87
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def exit_app():

# Create the main windows
root = tk.Tk()
root.title("Waveease!")
root.title("WavEase!")
root.geometry("800x600") # initial size

# layout
Expand Down
Binary file added __pycache__/Camera.cpython-38.pyc
Binary file not shown.
68 changes: 68 additions & 0 deletions volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pyautogui simulates mouse movements so like for example turning the volume up or down

import cv2
import mediapipe as mp
import time
import pyautogui

def main():
cap = cv2.VideoCapture(0)

mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
mp_draw = mp.solutions.drawing_utils

while True:
success, img = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue

imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)

if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
# List used to store finger extension status (True means extended)
fingers = []

# Obtain landmark coordinates for finger MCP (Metacarpophalangeal, metacarpophalangeal joint) and TIP (tip of the finger)
for i, finger in enumerate([mp_hands.HandLandmark.INDEX_FINGER_MCP, mp_hands.HandLandmark.MIDDLE_FINGER_MCP,
mp_hands.HandLandmark.RING_FINGER_MCP, mp_hands.HandLandmark.PINKY_MCP]):
finger_mcp = hand_landmarks.landmark[finger]
finger_tip = hand_landmarks.landmark[finger + 3]

# Determine if fingers are extended (tip y-coordinate is less than metacarpophalangeal joint y-coordinate)
fingers.append(finger_tip.y < finger_mcp.y)

# The thumb is a slightly special case, judged here by its x-coordinate #
thumb_tip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP]
thumb_ip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_IP]
fingers.insert(0, thumb_tip.x < thumb_ip.x)

# Detect finger pointing up
if fingers[1] and all(not f for f in fingers[2:]):
pyautogui.press('volumeup')
print("Volume Up")
# Detect finger pointing down
elif not fingers[1] and all(not f for f in fingers[2:]):
pyautogui.press('volumedown')
print("Volume Down")

# Print the corresponding number based on the number of fingers stretched out
if all(not f for f in fingers):
print("6 - Fist")
else:
print(f"{fingers.count(True)} - Fingers extended")

mp_draw.draw_landmarks(img, hand_landmarks, mp_hands.HAND_CONNECTIONS)

cv2.imshow("Hands", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

if __name__ == "__main__":
main()

0 comments on commit 3fd4c87

Please sign in to comment.