Skip to content

Commit

Permalink
issue nishtha981#3 :crossing hands stops the code
Browse files Browse the repository at this point in the history
  • Loading branch information
srijan-nodes committed Oct 13, 2023
1 parent be120e8 commit 2b5b65a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
Binary file added __pycache__/hand_position.cpython-311.pyc
Binary file not shown.
21 changes: 20 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@

cap = cv2.VideoCapture(0)

def are_hands_crossed(hand_landmarks, width):
if len(hand_landmarks) < 2:
return False

left_wrist_x = hand_landmarks[0].landmark[mp_hands.HandLandmark.WRIST].x * width
right_wrist_x = hand_landmarks[1].landmark[mp_hands.HandLandmark.WRIST].x * width

return left_wrist_x > right_wrist_x

def hand_tracking():
with mp_hands.Hands(
model_complexity=1,
min_detection_confidence=0.4,
min_tracking_confidence=0.4) as hands:

hands_crossed = False

while cap.isOpened():
success, image = cap.read()
h, w, c = image.shape
Expand All @@ -39,6 +50,14 @@ def hand_tracking():
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())

if are_hands_crossed(results.multi_hand_landmarks, w):
if not hands_crossed:
hands_crossed = True
print("Hands crossed. Stopping the code.")
break
else:
hands_crossed = False

index_finger_tip = hand_landmarks.landmark[0]

index_finger_tip_x = index_finger_tip.x * w
Expand Down Expand Up @@ -72,4 +91,4 @@ def hand_tracking():

key_stimulation_thread.start()

hand_tracking()
hand_tracking()
94 changes: 94 additions & 0 deletions tempCodeRunnerFile.python
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import numpy as np
import mediapipe as mp
import cv2
import time
import threading
import hand_position

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

cap = cv2.VideoCapture(0)

def are_hands_crossed(hand_landmarks, width):
if len(hand_landmarks) < 2:
return False

left_wrist_x = hand_landmarks[0].landmark[mp_hands.HandLandmark.WRIST].x * width
right_wrist_x = hand_landmarks[1].landmark[mp_hands.HandLandmark.WRIST].x * width

return left_wrist_x > right_wrist_x

def hand_tracking():
with mp_hands.Hands(
model_complexity=1,
min_detection_confidence=0.4,
min_tracking_confidence=0.4) as hands:

hands_crossed = False

while cap.isOpened():
success, image = cap.read()
h, w, c = image.shape
start = time.perf_counter()

image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)

image.flags.writeable = False

results = hands.process(image)

image.flags.writeable = True

image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
if hand_landmarks.landmark:
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())

if are_hands_crossed(results.multi_hand_landmarks, w):
if not hands_crossed:
hands_crossed = True
print("Hands crossed. Stopping the code.")
break
else:
hands_crossed = False

index_finger_tip = hand_landmarks.landmark[0]

index_finger_tip_x = index_finger_tip.x * w
index_finger_tip_y = index_finger_tip.y * h

if index_finger_tip_x > w/2:
cv2.putText(image, "Right", (500, 70),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 250))
elif index_finger_tip_x < w/2:
cv2.putText(image, "Left", (500, 70),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, (250, 0))

cv2.line(image, (int(w/2), 0), (int(w/2), h), (0, 255, 0), 2)

end = time.perf_counter()
totalTime = end - start

fps = 1/totalTime

cv2.putText(image, f'FPS: {int(fps)}', (20, 70),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2)
cv2.imshow('MediaPipe Hands', image)

if cv2.waitKey(5) & 0xFF == 27:
break

cap.release()
cv2.destroyAllWindows()

key_stimulation_thread = threading.Thread(target=hand_position.stimulate_keys)

key_stimulation_thread.start()

hand_tracking()

0 comments on commit 2b5b65a

Please sign in to comment.