From 1482f46dae8db7f1fb231647edc61b2a46caf81c Mon Sep 17 00:00:00 2001 From: Damm Bro <144452060+YashDhirajOza@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:01:32 +0530 Subject: [PATCH 1/6] Volume Control with hands --- computer_vision/WaveTune.py | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 computer_vision/WaveTune.py diff --git a/computer_vision/WaveTune.py b/computer_vision/WaveTune.py new file mode 100644 index 000000000000..f47f6b69c3dd --- /dev/null +++ b/computer_vision/WaveTune.py @@ -0,0 +1,61 @@ +import cv2 +import mediapipe as mp +import pyautogui +import math +import time +CAMERA_ID = 0 +FLIP_IMAGE = True +VOLUME_CHANGE_COOLDOWN = 0.5 +VOLUME_CHANGE_THRESHOLD = 50 +def main(): + 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) + mpDraw = 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: + mpDraw.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() \ No newline at end of file From 2b4700f01ed2766b36f4daa80e6cb92200cd6a46 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Oct 2024 05:04:55 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/WaveTune.py | 48 ++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/computer_vision/WaveTune.py b/computer_vision/WaveTune.py index f47f6b69c3dd..3856f6fc7ef2 100644 --- a/computer_vision/WaveTune.py +++ b/computer_vision/WaveTune.py @@ -3,19 +3,27 @@ import pyautogui import math import time + CAMERA_ID = 0 FLIP_IMAGE = True VOLUME_CHANGE_COOLDOWN = 0.5 -VOLUME_CHANGE_THRESHOLD = 50 +VOLUME_CHANGE_THRESHOLD = 50 + + def main(): 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) + hands = mp.solutions.hands.Hands( + static_image_mode=False, + max_num_hands=1, + min_detection_confidence=0.5, + min_tracking_confidence=0.5, + ) mpDraw = mp.solutions.drawing_utils last_volume_change_time = 0 - distance = 0 + distance = 0 while True: success, image = cap.read() if not success: @@ -27,11 +35,19 @@ def main(): results = hands.process(image_rgb) if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: - mpDraw.draw_landmarks(image, hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS) + mpDraw.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]) + 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) @@ -41,21 +57,31 @@ def main(): if distance > VOLUME_CHANGE_THRESHOLD: pyautogui.press("volumeup") print("Volume Up") - else: + 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.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: + if key == 27: print("Esc key pressed. Exiting...") break - elif key == ord('q'): + elif key == ord("q"): print("Q key pressed. Exiting...") break cap.release() cv2.destroyAllWindows() print("Program ended") + + if __name__ == "__main__": - main() \ No newline at end of file + main() From 88fee26ff61c569927ee6acb4c42a09057b6c5be Mon Sep 17 00:00:00 2001 From: Damm Bro <144452060+YashDhirajOza@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:07:38 +0530 Subject: [PATCH 3/6] Update and rename WaveTune.py to wave_tune.py --- computer_vision/{WaveTune.py => wave_tune.py} | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) rename computer_vision/{WaveTune.py => wave_tune.py} (94%) diff --git a/computer_vision/WaveTune.py b/computer_vision/wave_tune.py similarity index 94% rename from computer_vision/WaveTune.py rename to computer_vision/wave_tune.py index 3856f6fc7ef2..130c332a6e8c 100644 --- a/computer_vision/WaveTune.py +++ b/computer_vision/wave_tune.py @@ -1,15 +1,14 @@ -import cv2 -import mediapipe as mp -import pyautogui 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(): cap = cv2.VideoCapture(CAMERA_ID) if not cap.isOpened(): @@ -21,7 +20,7 @@ def main(): min_detection_confidence=0.5, min_tracking_confidence=0.5, ) - mpDraw = mp.solutions.drawing_utils + mp_draw = mp.solutions.drawing_utils last_volume_change_time = 0 distance = 0 while True: @@ -35,7 +34,7 @@ def main(): results = hands.process(image_rgb) if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: - mpDraw.draw_landmarks( + mp_draw.draw_landmarks( image, hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS ) thumb_tip = hand_landmarks.landmark[4] @@ -82,6 +81,5 @@ def main(): cv2.destroyAllWindows() print("Program ended") - if __name__ == "__main__": main() From 7c0a780fb90f0067194fa9030004e61d2a56d02f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Oct 2024 06:38:00 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/wave_tune.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/computer_vision/wave_tune.py b/computer_vision/wave_tune.py index 130c332a6e8c..f5e0dd38ce8b 100644 --- a/computer_vision/wave_tune.py +++ b/computer_vision/wave_tune.py @@ -9,6 +9,7 @@ VOLUME_CHANGE_COOLDOWN = 0.5 VOLUME_CHANGE_THRESHOLD = 50 + def main(): cap = cv2.VideoCapture(CAMERA_ID) if not cap.isOpened(): @@ -81,5 +82,6 @@ def main(): cv2.destroyAllWindows() print("Program ended") + if __name__ == "__main__": main() From 0f814e043016f98e0028bd21955d0d55ce13c500 Mon Sep 17 00:00:00 2001 From: Damm Bro <144452060+YashDhirajOza@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:22:31 +0530 Subject: [PATCH 5/6] Update wave_tune.py --- computer_vision/wave_tune.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/computer_vision/wave_tune.py b/computer_vision/wave_tune.py index f5e0dd38ce8b..5fee3384537e 100644 --- a/computer_vision/wave_tune.py +++ b/computer_vision/wave_tune.py @@ -9,8 +9,8 @@ VOLUME_CHANGE_COOLDOWN = 0.5 VOLUME_CHANGE_THRESHOLD = 50 - -def main(): +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}") @@ -82,6 +82,5 @@ def main(): cv2.destroyAllWindows() print("Program ended") - if __name__ == "__main__": main() From 1479c68300c69b16bece424150de87bf768efefd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Oct 2024 06:52:53 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/wave_tune.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/computer_vision/wave_tune.py b/computer_vision/wave_tune.py index 5fee3384537e..e080ebf8b767 100644 --- a/computer_vision/wave_tune.py +++ b/computer_vision/wave_tune.py @@ -9,6 +9,7 @@ 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) @@ -82,5 +83,6 @@ def main() -> None: cv2.destroyAllWindows() print("Program ended") + if __name__ == "__main__": main()