Skip to content

VolumeControlwithhands #11661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions computer_vision/wave_tune.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import math
import time
import pyautogui
import mediapipe as mp
import cv2

CAMERA_ID = 0

Check failure on line 7 in computer_vision/wave_tune.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

computer_vision/wave_tune.py:1:1: I001 Import block is un-sorted or un-formatted
FLIP_IMAGE = True
VOLUME_CHANGE_COOLDOWN = 0.5
VOLUME_CHANGE_THRESHOLD = 50

def main() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file computer_vision/wave_tune.py, please provide doctest for the function main

"""Main function to control hand gestures for volume control."""
cap = cv2.VideoCapture(CAMERA_ID)
if not cap.isOpened():
print(f"Error: Could not open camera with ID {CAMERA_ID}")
return
hands = mp.solutions.hands.Hands(
static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
)
mp_draw = mp.solutions.drawing_utils
last_volume_change_time = 0
distance = 0
while True:
success, image = cap.read()
if not success:
print("Failed to capture frame")
break
if FLIP_IMAGE:
image = cv2.flip(image, 1)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_draw.draw_landmarks(
image, hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS
)
thumb_tip = hand_landmarks.landmark[4]
index_tip = hand_landmarks.landmark[8]
x1, y1 = (
int(thumb_tip.x * image.shape[1]),
int(thumb_tip.y * image.shape[0]),
)
x2, y2 = (
int(index_tip.x * image.shape[1]),
int(index_tip.y * image.shape[0]),
)
cv2.circle(image, (x1, y1), 8, (0, 0, 255), -1)
cv2.circle(image, (x2, y2), 8, (0, 255, 255), -1)
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
distance = math.hypot(x2 - x1, y2 - y1)
current_time = time.time()
if current_time - last_volume_change_time > VOLUME_CHANGE_COOLDOWN:
if distance > VOLUME_CHANGE_THRESHOLD:
pyautogui.press("volumeup")
print("Volume Up")
else:
pyautogui.press("volumedown")
print("Volume Down")
last_volume_change_time = current_time
cv2.putText(
image,
f"Distance: {int(distance)}",
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 0, 0),
2,
)
cv2.imshow("Hand Volume Control", image)
key = cv2.waitKey(1) & 0xFF
if key == 27:
print("Esc key pressed. Exiting...")
break
elif key == ord("q"):
print("Q key pressed. Exiting...")
break
cap.release()
cv2.destroyAllWindows()
print("Program ended")

if __name__ == "__main__":
main()
Loading