-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Closed
VolumeControlwithhands #11661
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1482f46
Volume Control with hands
YashDhirajOza 2b4700f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 88fee26
Update and rename WaveTune.py to wave_tune.py
YashDhirajOza 7c0a780
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0f814e0
Update wave_tune.py
YashDhirajOza 1479c68
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 | ||
FLIP_IMAGE = True | ||
VOLUME_CHANGE_COOLDOWN = 0.5 | ||
VOLUME_CHANGE_THRESHOLD = 50 | ||
|
||
def main() -> None: | ||
"""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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 functionmain