diff --git a/camera.py b/camera.py index 1e522fd..e8787a4 100644 --- a/camera.py +++ b/camera.py @@ -10,22 +10,22 @@ mp_hands = mp.solutions.hands joint_list =[[8,5,0]] -def draw_finger_angles(image, results, joint_list): - # Loop through hands - for hand in results.multi_hand_landmarks: - # Loop through joint sets - for joint in joint_list: - a = np.array([hand.landmark[joint[0]].x, hand.landmark[joint[0]].y]) # First coord - b = np.array([hand.landmark[joint[1]].x, hand.landmark[joint[1]].y]) # Second coord - c = np.array([hand.landmark[joint[2]].x, hand.landmark[joint[2]].y]) # Third coord +# def draw_finger_angles(image, results, joint_list): +# # Loop through hands +# for hand in results.multi_hand_landmarks: +# # Loop through joint sets +# for joint in joint_list: +# a = np.array([hand.landmark[joint[0]].x, hand.landmark[joint[0]].y]) # First coord +# b = np.array([hand.landmark[joint[1]].x, hand.landmark[joint[1]].y]) # Second coord +# c = np.array([hand.landmark[joint[2]].x, hand.landmark[joint[2]].y]) # Third coord - radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0]) - angle = np.abs(radians * 180.0 / np.pi) +# radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0]) +# angle = np.abs(radians * 180.0 / np.pi) - cv2.putText(image, str(round(angle, 2)), tuple(np.multiply(b, [640, 480]).astype(int)), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA) - return image, angle +# cv2.putText(image, str(round(angle, 2)), tuple(np.multiply(b, [640, 480]).astype(int)), +# cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA) +# return image, angle def get_label(index, hand, results): output = None @@ -40,12 +40,12 @@ def get_label(index, hand, results): coords = tuple(np.multiply( np.array((hand.landmark[mp_hands.HandLandmark.WRIST].x, hand.landmark[mp_hands.HandLandmark.WRIST].y)), [640, 480]).astype(int)) - - output = text, coords + + output = label, coords return output - +keyboard = Controller() cap = cv2.VideoCapture(0) with mp_hands.Hands(min_detection_confidence=0.8, min_tracking_confidence=0.5) as hands: @@ -70,11 +70,12 @@ def get_label(index, hand, results): # RGB 2 BGR image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) - # Detections - print(results) + # # Detections + # print(results) # Rendering results if results.multi_hand_landmarks: + display = "" for num, hand in enumerate(results.multi_hand_landmarks): mp_drawing.draw_landmarks(image, hand, mp_hands.HAND_CONNECTIONS, mp_drawing.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4), @@ -85,13 +86,16 @@ def get_label(index, hand, results): if get_label(num, hand, results): text, coord = get_label(num, hand, results) cv2.putText(image, text, coord, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) - + display = text + print(text) # Draw angles to image from joint list - image, angle = draw_finger_angles(image, results, joint_list) - keyboard = Controller() - if angle<=180: + # image, angle = draw_finger_angles(image, results, joint_list) + + # Changed it to detect left hand and right hand + if display == "Left": keyboard.press(Key.right) keyboard.release(Key.right) + #used else here because mp doesn't detect right hand sometimes (idk why) else: keyboard.press(Key.left) keyboard.release(Key.left) @@ -101,7 +105,7 @@ def get_label(index, hand, results): cv2.rectangle(image, (0, 0), (355, 73), (214, 44, 53)) cv2.putText(image, 'Direction', (15, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA) - cv2.putText(image, "Left" if angle >180 else "Right", + cv2.putText(image, "Left" if display == "Left" else "Right", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2, cv2.LINE_AA) cv2.imshow('Hand Tracking', image)