Skip to content

Commit

Permalink
Updated main-mobile-cam.py
Browse files Browse the repository at this point in the history
Display Code added
  • Loading branch information
aviralgarg05 authored Oct 3, 2024
1 parent 499ab84 commit bbc5a89
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions Computer Vision/Hand Game Controller/main-mobile-cam.py
Original file line number Diff line number Diff line change
@@ -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://<YOUR-IP>/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
Expand All @@ -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()

# Release resources
cv2.destroyAllWindows()
cp.release()

0 comments on commit bbc5a89

Please sign in to comment.