diff --git a/Hand gesture code/main.py b/Hand gesture code/main.py index 613d10204d304a439f7572de4a86524b31bc362a..5791e691315243771bf0474522dcf10bb2c274e4 100644 --- a/Hand gesture code/main.py +++ b/Hand gesture code/main.py @@ -7,21 +7,18 @@ import time mp_hands = mp.solutions.hands hands = mp_hands.Hands() start_time = time.time() - +counter = 0 cap = cv2.VideoCapture(0) printvar = 0 def map_range(finger_norm, finger_low, finger_mid, finger_high, deadzone=0.15): # Maps a linear spectrum to the range [-1,1] - finger_range = finger_high-finger_low - - mapped_val = (finger_norm-finger_mid)/(0.5*finger_range) - mapped_val = mapped_val*mapped_val*mapped_val/np.abs(mapped_val) # square the value to get smaller values for small changes near zero - if mapped_val > 1: # Upper limit - mapped_val = 1 - elif mapped_val < -1: # lower limit - mapped_val = -1 - if mapped_val < deadzone and mapped_val > -deadzone: # Implement a deadzone near zero - mapped_val = 0 + finger_range = finger_high - finger_low + + mapped_val = (finger_norm - finger_mid) / (0.5 * finger_range) + mapped_val = mapped_val ** 3 / np.abs(mapped_val) # cube the value to get smaller values for small changes near zero + mapped_val = max(-1, min(mapped_val, 1)) # limit the value between -1 and 1 + if abs(mapped_val) < deadzone: # Implement a deadzone near zero + mapped_val = 0 return mapped_val while True: @@ -117,11 +114,11 @@ while True: cv2.putText(frame, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA) cv2.imshow('Hand Gesture Recognition', frame) - - if hand_data: + + if hand_data and counter % 50 == 0: with open('hand_data.txt', 'a') as file: file.write(hand_data + '\n') - + counter += 1 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release()