From bbc5a89d8703aaa83df0af3e2382817f55c31d83 Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Thu, 3 Oct 2024 20:12:11 +0530 Subject: [PATCH] Updated main-mobile-cam.py Display Code added --- .../Hand Game Controller/main-mobile-cam.py | 79 +++++++++++-------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/Computer Vision/Hand Game Controller/main-mobile-cam.py b/Computer Vision/Hand Game Controller/main-mobile-cam.py index 8f231b9c..5e8cb24a 100644 --- a/Computer Vision/Hand Game Controller/main-mobile-cam.py +++ b/Computer Vision/Hand Game Controller/main-mobile-cam.py @@ -1,25 +1,45 @@ import cv2 import mediapipe as mp - from pynput.keyboard import Controller -mp_hands = mp.solutions.hands.Hands() +# Initialize MediaPipe hands with optimizations +mp_hands = mp.solutions.hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5, min_tracking_confidence=0.5) keyboard = Controller() -url = 'http:///video' -cp = cv2.VideoCapture(url) -x1, x2, y1, y2 =0, 0, 0, 0 +# Open the camera +cp = cv2.VideoCapture(0) +x1, x2, y1, y2 = 0, 0, 0, 0 +pressed_key = "" +frame_skip = 1 # Reduce frame skipping to 1 for smoother processing + +frame_count = 0 + +while True: + # Capture frame from camera + ret, image = cp.read() + if not ret: + print("Failed to capture frame. Exiting...") + break + + frame_count += 1 -while(True): + # Skip every nth frame to improve performance (reduced skipping) + if frame_count % frame_skip != 0: + continue - _, image = cp.read() + # Get image dimensions (without resizing) + image_height, image_width, _ = image.shape - image_height, image_width, image_depth = image.shape + # Flip the image horizontally for a selfie-view display image = cv2.flip(image, 1) + + # Convert the image to RGB rgb_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + # Process the image to detect hands output_hands = mp_hands.process(rgb_img) all_hands = output_hands.multi_hand_landmarks + # Detect keypresses based on hand position if all_hands: hand = all_hands[0] one_hand_landmark = hand.landmark @@ -28,58 +48,55 @@ x = int(lm.x * image_width) y = int(lm.y * image_height) - if id == 12: + if id == 12: # Finger tip of middle finger x1 = x y1 = y - if id == 0: + if id == 0: # Wrist point x2 = x y2 = y - distX = 0 distX = x1 - x2 - distY = 0 - distY =y1 - y2 + distY = y1 - y2 - if distY > -140 and distY !=0: - # press S + if distY > -140 and distY != 0: keyboard.release('d') keyboard.release('a') keyboard.release('w') keyboard.press('s') - print("S") - - if distY < -200 and distY != 0: + print("Pressed Key: S") + elif distY < -200 and distY != 0: keyboard.release('s') keyboard.release('d') keyboard.release('a') keyboard.press('w') - print("W") - - if (distX < -100 and distX != 0): + print("Pressed Key: W") + elif distX < -100 and distX != 0: keyboard.release('s') keyboard.release('d') keyboard.press('w') keyboard.press('a') - print('A') - - if (distX > 55 and distX != 0): + print("Pressed Key: A") + elif distX > 55 and distX != 0: keyboard.release('a') keyboard.release('s') keyboard.press('w') keyboard.press('d') - print('D') - + print("Pressed Key: D") else: - print('none') keyboard.release('d') keyboard.release('a') keyboard.release('w') keyboard.release('s') - # if image is not None: - # cv2.imshow("Frame", image) + # Display the camera feed + cv2.imshow("Camera Feed", image) + + # Check if 'q' is pressed to quit q = cv2.waitKey(1) - if q==ord("q"): + if q == ord("q"): break -cv2.destroyAllWindows() \ No newline at end of file + +# Release resources +cv2.destroyAllWindows() +cp.release()