def Leap_Motion_part(): global x_pos, y_pos, C_Color frame = controller.frame() for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP: print "Tapping" C_Color = Red elif gesture.type == Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) if abs(swipe.direction[0]) > abs(swipe.direction[1]): if swipe.direction[0] < 0: print "Left Swipe" x_pos -= 10 elif swipe.direction[0] > 0: print "Right Swipe" x_pos += 10 else: print "No swipe" elif gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) if (circle.pointable.direction.angle_to(circle.normal) <= Leap.PI / 2): print "clockwise_circle" y_pos -= 4 else: print "anticlockwise_circle" y_pos += 4
def on_frame(self, controller): #self.paintCanvas.delete("all") frame = controller.frame() for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SWIPE: global i swipe = Leap.SwipeGesture(gesture) self.paintCanvas.delete("all") sleep(2) interactionBox = frame.interaction_box for hand in frame.hands: for finger in hand.fingers: if finger.type == 1: normalizedPosition = interactionBox.normalize_point(finger.tip_position) if hand.is_right: if(finger.touch_distance > 0 and finger.touch_zone != Leap.Pointable.ZONE_NONE): color = self.rgb_to_hex((0, 255 - 255 * finger.touch_distance, 0)) elif(finger.touch_distance <= 0): color = self.rgb_to_hex((-255 * finger.touch_distance, 0, 0)) else: color = self.rgb_to_hex((0,0,200)) self.draw(normalizedPosition.x * 800, 600 - normalizedPosition.y * 600, 15, 15, color) else: self.draw(normalizedPosition.x * 800, 600 - normalizedPosition.y * 600, 15, 15, "white")
def on_frame(self, controller): global writerFile, onSave, counter counter = counter + 1 if counter > 360: counter = 0 self.limpiar() if onSave: frame = controller.frame() for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) self.limpiar() time.sleep(2) interactionBox = frame.interaction_box for hand in frame.hands: for finger in hand.fingers: if finger.type == 1: normalizedPosition = interactionBox.normalize_point(finger.tip_position) if hand.is_right: if finger.touch_distance <= 0.55 and finger.touch_zone != Leap.Pointable.ZONE_NONE and finger.type != 2: self.draw(normalizedPosition.x * 500, 500 - normalizedPosition.y * 500, 15, 15, "black") else: self.draw(normalizedPosition.x * 500, 500 - normalizedPosition.y * 500, 15, 15, "white") writerFile.write(str(time.time()) + "," + str(finger.touch_distance) + "," + str(finger.touch_zone) + "," + str(finger.tip_velocity) + "," + str(finger.tip_position) + "\n")
def on_frame(self, controller): # print ("Frame available") frame = controller.frame() if (frame.hands.is_empty == False): hand = frame.hands[0] for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) pointable = swipe.pointable if (pointable.is_finger == False): continue finger = Leap.Finger(pointable) hand = HandWrapper(finger.hand) if (finger.type != Leap.Finger.TYPE_INDEX and finger.type != Leap.Finger.TYPE_MIDDLE): continue if (hand.specific_fingers_extended( Leap.Finger.TYPE_MIDDLE, Leap.Finger.TYPE_INDEX) == False): continue current_gesture = GestureInfo(swipe) self.is_new_gesture(current_gesture) break
def on_frame(self, controller): # Get the most recent frame and report some basic information frame = controller.frame() global connection try: # x = str(len(frame.hands)) + "\n" # connection.sendall(x.encode('ascii')) for hand in frame.hands: handType = "Left Hand" if hand.is_left else "Right Hand" normal = hand.palm_normal direction = hand.direction pitch = direction.pitch * Leap.RAD_TO_DEG # Rotation around x-axis roll = normal.roll * Leap.RAD_TO_DEG # Rotation around z-axis yaw = direction.yaw * Leap.RAD_TO_DEG # Rotation around y-axis(Perpendicular to the plane) if pitch > 35: connection.sendall((handType + ",Back\n").encode('ascii')) elif pitch < -35: connection.sendall((handType + ",Front\n").encode('ascii')) for tool in frame.tools: connection.sendall(("Tool\n").encode('ascii')) for gesture in frame.gestures(): # swipe gesture if gesture.type == Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) swipe_id = swipe.id swipe_state = self.state_names[gesture.state] swipe_position = swipe.position swipe_direction = swipe.direction swipe_speed = swipe.speed if swipe_direction.x > 0: connection.sendall(("Swipe Right\n").encode('ascii')) else: connection.sendall(("Swipe Left\n").encode('ascii')) # screen tab gesture elif gesture.type == Leap.Gesture.TYPE_SCREEN_TAP: screentap = Leap.ScreenTapGesture(gesture) screentap_id = gesture.id screentap_state = self.state_names[gesture.state] screentap_position = screentap.position screentap_direction = screentap.direction connection.sendall(("Screentab\n").encode('ascii')) # keytab Gesture elif gesture.type == Leap.Gesture.TYPE_KEY_TAP: keytap = Leap.KeyTapGesture(gesture) keytap_id = gesture.id keytap_state = self.state_names[gesture.state] keytap_position = keytap.position keytap_direction = keytap.direction connection.sendall(("Keytab\n").encode('ascii')) except socket.error: create_connection()
def __process_gesture(self, gesture): current_time = time.time() if current_time < self.__last_gesture_time + self.__gesture_cooldown_duration: return # Swipes (including ones that are only in-progress) take precedence over taps. if gesture.type is Leap.Gesture.TYPE_SWIPE: if gesture.state is Leap.Gesture.STATE_STOP: swipe = Leap.SwipeGesture(gesture) for pointable in swipe.pointables: if not pointable.is_finger: continue finger = Leap.Finger(pointable) if finger.type is not Leap.Finger.TYPE_INDEX: continue swipe_offset = swipe.position - swipe.start_position planar_swipe_offset_length_squared = swipe_offset.x**2 + swipe_offset.y**2 if planar_swipe_offset_length_squared < self.__min_swipe_length**2: continue assert len(gesture.hands) == 1 gesturing_hand = gesture.hands[0] self.__gesturing_hand_id = gesturing_hand.id swipe_angle = math.atan2(swipe.direction.y, swipe.direction.x) tau = 2.0 * math.pi if (swipe_angle < 0.0): swipe_angle += tau assert 0.0 <= swipe_angle and swipe_angle <= tau # lerp the real number range [0,tau] onto the integer range [0,8] menu_index = int(round(8.0 * swipe_angle / tau)) if menu_index == 8: # wrap 8 to 0. menu_index = 0 handedness = "lefthand" if gesturing_hand.is_left else "righthand" activated_compass_direction_name = self.__compass_direction_name[ menu_index] gesture_name = handedness + ":" + activated_compass_direction_name #Log.record('swiped {0}\n{1}\n'.format(gesture_name, self.__compass_direction_image[menu_index])) if self.__call_upon_gesture != None: self.__call_upon_gesture(gesture_name) self.__last_gesture_time = current_time elif gesture.type is Leap.Gesture.TYPE_KEY_TAP: key_tap = Leap.KeyTapGesture(gesture) if key_tap.pointable.is_finger: assert len(gesture.hands) == 1 gesturing_hand = gesture.hands[0] self.__gesturing_hand_id = gesturing_hand.id finger = Leap.Finger(key_tap.pointable) handedness = "lefthand" if gesturing_hand.is_left else "righthand" tap_name = "T-" + self.__finger_names[finger.type] gesture_name = handedness + ":" + tap_name #Log.record('tapped {0}\n'.format(gesture_name)) if self.__call_upon_gesture != None: self.__call_upon_gesture(gesture_name) self.__last_gesture_time = current_time
def on_frame(self, controller): frame = controller.frame() #print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d" % (frame.id, frame.timestamp, len(frame.hands), len(frame.fingers)) hand = frame.hands.rightmost #position = hand.palm_velocity #velocity = hand.palm_velocity direction = hand.direction print "no. of hands : {} hands".format(len(frame.hands)), flag = 0 #this is used to issue one command per frame pinch = hand.pinch_strength #print " pinch strenght : {} ".format(pinch) """Pause and play""" if pinch > 0.5 and flag == 0: vlcCommand('play/pause') flag = 1 #print "finger {} ".format() #print "position : {} , velocity : {} , direction : {}".format(position,velocity,direction) #print getDistance(self.position1,self.position2) """ Next and previous gesture """ for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) #print "start position ",swipe.start_position #print "current position " , swipe.position distance = getDistance(swipe.start_position[0], swipe.position[0], 10) if flag == 0: vlcCommand(distance) flag = 1 #rotation_around_y_axis = hand.rotation_angle(start_frame, Vector.y_axis) """Volume up and Down""" pitch = int(direction.pitch * Leap.RAD_TO_DEG) if pitch <= 80: if self.prev_angle == None: self.prev_angle = int(pitch) else: angle = getDistance(int(pitch), self.prev_angle, 20) if angle != None and flag == 0: if hand.is_left: vlcCommand('volume down') else: vlcCommand('volume up') flag = 1 os.system('cls') """
def on_frame(self, controller): music = Music_Control() frame = controller.frame() for gesture in frame.gestures(): # ----------------------------------------------------------------------- if gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = CircleGesture(gesture) if self.isClockwise(circle.pointable.direction.angle_to(circle.normal)): music.increaseVolume() else: music.decreaseVolume() # ----------------------------------------------------------------------- if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture)
def on_frame(self, controller): # Get the most recent frame and report some basic information frame = controller.frame() # print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d" % ( #frame.id, frame.timestamp, len(frame.hands), len(frame.fingers)) gestures = frame.gestures() righthand = frame.hands.rightmost pinky_position = righthand.fingers[4].bone(Leap.Bone.TYPE_DISTAL).center ring_position = righthand.fingers[3].bone(Leap.Bone.TYPE_DISTAL).center finger_position = righthand.fingers[1].bone(Leap.Bone.TYPE_DISTAL).center hand_position = righthand.palm_position is_fisting = (0.0 < point_distance(ring_position, hand_position) < 45.0 and 0.0 < point_distance(pinky_position, hand_position) < 45.0) for gesture in gestures: if not self.is_mouse_controlled: if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) swipe_direction = swipe.direction swipe_pointable = swipe.pointable swipe_speed = swipe.speed if swipe_direction.x > 0 and abs(swipe_direction.y) < self.swipe_min_delta_y: self.flag["direction"] = 0 self.flag["count"] += 1 if self.flag["swipe_starttime"] == 0: self.flag["swipe_starttime"] = frame.timestamp elif swipe_direction.x < 0 and abs(swipe_direction.y) < self.swipe_min_delta_y: self.flag["direction"] = 1 self.flag["count"] += 1 if self.flag["swipe_starttime"] == 0: self.flag["swipe_starttime"] = frame.timestamp if len(gestures) == 0: if self.flag["direction"] == 1 and (self.flag["swipe_starttime"] - self.flag["swipe_lastendtime"] > self.min_during_time or (self.flag["last_direction"] == 1 and self.flag["swipe_starttime"] - self.flag["swipe_lastendtime"] > self.min_same_direction_time)): send_cmd("c0_01") self.flag["swipe_lastendtime"] = frame.timestamp self.flag["last_direction"] = 1 elif self.flag["direction"] == 0 and (self.flag["swipe_starttime"] - self.flag["swipe_lastendtime"] > self.min_during_time or (self.flag["last_direction"] == 0 and self.flag["swipe_starttime"] - self.flag["swipe_lastendtime"] > self.min_same_direction_time)): send_cmd("c0_02") self.flag["swipe_lastendtime"] = frame.timestamp self.flag["last_direction"] = 0 self.flag["count"] = 0 self.flag["direction"] = -1 self.flag["swipe_starttime"] = 0
def on_frame(self, controller): # Get the most recent frame and report some basic information frame = controller.frame() # Get hands for hand in frame.hands: global x_val global y_val global left_pressed global right_pressed handType = "Left hand" if hand.is_left else "Right hand" #print " position: %s" % (hand.palm_position) print "x - %s" % (hand.palm_position[0]) print "y - %s" % (-hand.palm_position[2]) win32api.SetCursorPos( (map(int(hand.palm_position[0]), -400, 400, 0, screen_width), map(int(hand.palm_position[2]), -400, 400, 0, screen_height))) x_val = map(int(hand.palm_position[0]), 0, 400, 0, screen_width) y_val = map(int(hand.palm_position[2]), 0, 400, 0, screen_height) if (hand.pinch_strength > pinch_power_threshold and left_pressed == 0): leftClick() if (hand.pinch_strength <= pinch_power_threshold and left_pressed == 1): leftRelease() if (hand.grab_strength > grab_power_threshold and right_pressed == 0): rightCLick() if (hand.grab_strength <= grab_power_threshold and right_pressed == 1): rightRelease() if not frame.hands.is_empty: print "" for gesture in frame.gestures(): print "gesture dir: %d, gesture pos: %d, gesture speed: %d" % ( gesture.direction, gesture.position, gesture.speed) if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) print "hello"
def on_frame(self, controller): frame = controller.frame() hands = frame.hands if DEBUG: print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d" % \ (frame.id, frame.timestamp, len(frame.hands), len(frame.fingers)) hand = hands[0] # first hand print(hand.palm_position) if hands.is_empty: if DEBUG: print "no hands. Raspimouse should be stopped." self.stop() else: self.move() for gesture in frame.gestures(): # start to move by <Key Taps> # https://developer-archive.leapmotion.com/documentation/v2/python/api/Leap.KeyTapGesture.html if gesture.type is Leap.Gesture.TYPE_KEY_TAP: key_tap = Leap.KeyTapGesture(gesture) if DEBUG: print "keytap detected. Raspimouse will start to move." self.goForward() # Turn Left/Right <Swipe> # https://developer-archive.leapmotion.com/documentation/v2/python/api/Leap.SwipeGesture.html if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) direction = swipe.direction if abs(direction[0]) > abs( direction[1]): # horizontal movement if direction[0] > 0: swipeDirection = "right" self.turnRight() else: swipeDirection = "left" self.turnLeft() else: # vertical movement, nothing to do swipeDirection = "ignore" if DEBUG: print direction print swipeDirection print "Swipe gesture detected"
def on_frame(self, controller): global InitXPos global FinalXPos frame = controller.frame() for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) if circle.state == 3: ShapePos = 1 SHAPE.ShapeClassMenu(shapecircle, ShapePos) for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) if swipe.state == 1: InitXPos = swipe.direction if swipe.state == 3: FinalXPos = swipe.direction DeltaX = float(FinalXPos[0]) - float(InitXPos[0]) if DeltaX < 0: if self.CurrentPos >= 1: self.CurrentPos = self.CurrentPos - 1 else: self.CurrentPos = 0 else: if self.CurrentPos <= 10: self.CurrentPos = self.CurrentPos + 1 else: self.CurrentPos = 11 state.LeapControl(self.CurrentPos) for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_KEY_TAP: tap = Leap.KeyTapGesture(gesture) state.Reset() self.CurrentPos = 0
def on_frame(self, controller): frame = controller.frame() hand = frame.hands.rightmost direction = hand.direction print "no. of hands : {} hands".format(len(frame.hands)), print "\n" flag = 0 pinch = hand.pinch_strength """Pause and play""" if pinch > 0.5 and flag == 0: vlcCommand('play/pause') flag = 1 """ Next and previous gesture """ for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) distance = getDistance( swipe.start_position[0], swipe.position[0], 10) if flag == 0: vlcCommand(distance) flag = 1 """Volume up and Down""" pitch = int(direction.pitch * Leap.RAD_TO_DEG) if pitch <= 80: if self.prev_angle == None: self.prev_angle = int(pitch) else: angle = getDistance(int(pitch), self.prev_angle, 20) if angle != None and flag == 0: if hand.is_left: vlcCommand('volume down') else: vlcCommand('volume up') flag = 1
def on_frame(self, controller): # Get the most recent frame and report some basic information frame = controller.frame() print "Frame ID:"+ str(frame.id) \ + "Timestamp: " + str(frame.timestamp) \ + "# of Hands: " + str(len(frame.hands)) \ + "# of Fingers: " + str(len(frame.fingers)) \ + "# of Tools: " + str(len(frame.tools)) \ + "# of Gestures: " + str(len(frame.gestures())) for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) start = swipe.start_position current = swipe.position direction = swipe.direction velocity = swipe.speed swipper = swipe.pointable print "swipe: " print "\t start:", start print "\t current:", current print "\t current:", current print "\t direction:", direction print "\t velocity:", velocity
def on_frame(self, controller): global swipeLeft global swipeRight global fistTimer global turnDir global swipeDir global swipeFinger global tapFinger global screenTapFinger global pinch global grab global fingerCount # Get the most recent frame and report some basic information frame = controller.frame() turns = 0 #Configurating Gesture Settings controller.config.set("Gesture.Swipe.MinLength", 220) controller.config.set("Gesture.Swipe.MinVelocity", 100) controller.config.save() controller.config.set("Gesture.KeyTap.MinDownVelocity", 40.0) controller.config.set("Gesture.KeyTap.HistorySeconds", .3) controller.config.set("Gesture.KeyTap.MinDistance", 1.0) controller.config.save() controller.config.set("Gesture.ScreenTap.MinForwardVelocity", 20.0) controller.config.set("Gesture.ScreenTap.HistorySeconds", .5) controller.config.set("Gesture.ScreenTap.MinDistance", 1.0) controller.config.save() for gesture in frame.gestures(): #Circle Gesture if gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = CircleGesture(gesture) if (circle.pointable.direction.angle_to(circle.normal) <= Leap.PI / 2): turnDir = "CW" turns += circle.progress if (turns > 2.5): pyautogui.hotkey('volumeup') #sleep(.1) else: turnDir = "CCW" turns -= circle.progress if (turns < -2.5): pyautogui.hotkey('volumedown') #sleep(.1) print(turnDir + " " + str(turns)) ######SWAP WITH FUCNTION #Swipe Gesture if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) swipeDir = swipe.direction swipeFinger = swipe.pointable if (swipeDir[0] < 0): pyautogui.moveTo(980, 540, 0) pyautogui.click() pyautogui.hotkey('left') pyautogui.hotkey('alt', 'tab') sleep(1.2) elif (swipeDir[0] > 0): pyautogui.moveTo(980, 540, 0) pyautogui.click() pyautogui.hotkey('right') pyautogui.hotkey('alt', 'tab') sleep(1.2) print("Swipe " + str(swipeFinger) + " " + str(swipeDir[0]) ) ######SWAP WITH FUCNTION #Key-Tap Gesture if gesture.type is Leap.Gesture.TYPE_KEY_TAP: tap = Leap.KeyTapGesture(gesture) tapFinger = tap.pointable tapDir = tap.direction print("Tap " + str(tapDir)) ######SWAP WITH FUCNTION #Screen-Tap Gesture if gesture.type is Leap.Gesture.TYPE_SCREEN_TAP: screenTap = Leap.ScreenTapGesture(gesture) screenTapFinger = screenTap.pointable print("ScreenTap " + str(screenTapFinger) ) ######SWAP WITH FUCNTION for hand in frame.hands: #Identifies the hand handType = "Left" if hand.is_left else "Right" grab = hand.grab_strength pinch = hand.pinch_strength #print(handType + " p:" + str(pinch) + " g:"+ str(grab)) ######SWAP WITH FUCNTION if grab == 1: if (fistTimer > 2): pyautogui.hotkey('volumemute') print("g" + str(grab)) sleep(1.2) fistTimer = 0 else: fistTimer += 1 '''elif pinch > 0.99:
def main(): global Song_Number, Action_Wait, Song_change, Music_volume, Music_paused, Music_file, Onging_gesture #pygame.display.set_icon() listener = Leap.Listener() controller = Leap.Controller() controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE) controller.enable_gesture(Leap.Gesture.TYPE_SWIPE) ## controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP) controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP) controller.add_listener(listener) ## controller.config.set("Gesture.KeyTap.MinDownVelocity", 1.0) ## controller.config.set("Gesture.KeyTap.HistorySeconds", .1) ## controller.config.set("Gesture.KeyTap.MinDistance", 1.0) ## controller.config.save() pygame.mixer.set_num_channels(1) Music_file = pygame.mixer.Sound('M1.ogg') pygame.mixer.music.set_volume(0.5) pygame.mixer.music.load('M1.ogg') pygame.mixer.music.play() pygame.mixer.music.pause() while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() Display_surface.fill(White) Progress_bar() Volume_bar() Image_cover() Pause_Start_icon() if Song_change: pygame.mixer.Sound.stop(Music_file) Music_file = pygame.mixer.Sound('M%s.ogg' % (Song_Number)) pygame.mixer.music.load('M%s.ogg' % (Song_Number)) pygame.mixer.music.play() Music_paused = False Song_change = False frame = controller.frame() if Action_Wait > 60: for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_KEY_TAP: print "Tapping" Action_Wait = 0 if Music_paused: Music_paused = False pygame.mixer.music.unpause() else: Music_paused = True pygame.mixer.music.pause() elif gesture.type == Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) if abs(swipe.direction[0]) > abs(swipe.direction[1]): if swipe.direction[0] < 0: print "Left Swipe" Onging_gesture = "Left Swipe" Action_Wait = 0 if Song_Number < 5: Song_Number += 1 Song_change = True elif swipe.direction[0] > 0: print "Right Swipe" Onging_gesture = "Right Swipe" Action_Wait = 0 if Song_Number > 1: Song_Number -= 1 Song_change = True else: print "No swipe" elif gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) if (circle.pointable.direction.angle_to(circle.normal) <= Leap.PI / 2): print "clockwise_circle" if Music_volume < 1: Music_volume += 0.01 pygame.mixer.music.set_volume(Music_volume) print pygame.mixer.music.get_volume() else: print "anticlockwise_circle" if Music_volume > 0: Music_volume -= 0.01 pygame.mixer.music.set_volume(Music_volume) print pygame.mixer.music.get_volume() else: Action_Wait += 1 pygame.display.flip() FPS_Clock.tick(FPS)
def on_frame(self, controller): """ Get the most recent frame and report some basic information """ finger_angles = [ 'ThumbIndexAngle', 'IndexMidAngle', 'MidRingAngle', 'RingPinkyAngle' ] bone_angles = ['MetaProxAngle', 'ProxInterAngle', 'InterDistAngle'] bone_direc = [ 'MetaDirection', 'ProxDirection', 'InterDirection', 'DistDirection' ] bone_len = ['MetaLength', 'ProxLength', 'InterLength', 'DistLength'] bone_prevJoint = [ 'MetaPrevJoint', 'ProxPrevJoint', 'InterPrevJoint', 'DistPrevJoint' ] bone_nextJoint = [ 'MetaNextJoint', 'ProxNextJoint', 'InterNextJoint', 'DistNextJoint' ] frame = controller.frame() mydict = defaultdict(list) hand_num = len(frame.hands) for hand in frame.hands: arm = hand.arm mydict['ArmDirection'].append(str(arm.direction)[1:-1]) mydict['ElbowPosition'].append(str(arm.elbow_position)[1:-1]) mydict['ArmWidth'].append(str(arm.width)) mydict['WristPosition'].append(str(arm.wrist_position)[1:-1]) displacement = arm.wrist_position - arm.elbow_position mydict['ArmLength'].append(str(displacement.magnitude)) mydict['HandID'].append(str(hand.id)) mydict['HandConfid'].append(str(hand.confidence)) mydict['GrabStrength'].append(str(hand.grab_strength)) mydict['PalmNormal'].append(str(hand.palm_normal)[1:-1]) mydict['PalmYaw'].append(str(hand.direction.yaw)) mydict['PalmRoll'].append(str(hand.direction.roll)) mydict['PalmPitch'].append(str(hand.direction.pitch)) mydict['PalmPosition'].append(str(hand.palm_position)[1:-1]) mydict['HandDirection'].append(str(hand.direction)[1:-1]) mydict['PalmVelocity'].append(str(hand.palm_velocity)[1:-1]) mydict['PinchStrength'].append(str(hand.pinch_strength)) mydict['SphereCenter'].append(str(hand.sphere_center)[1:-1]) mydict['SphereRadius'].append(str(hand.sphere_radius)) mydict['PalmWidth'].append(str(hand.palm_width)) if hand.is_left: mydict['HandType'].append('Left hand') else: mydict['HandType'].append('Right hand') # Get fingers # finger_num = 5 * hand_num prox_direc = [] for finger in hand.fingers: prox_direc.append(finger.bone(1).direction) mydict['FingerType'].append(self.finger_names[finger.type]) mydict['FingerLength'].append(str(finger.length)) mydict['FingerWidth'].append(str(finger.width)) mydict['TipDirection'].append(str(finger.direction)[1:-1]) mydict['TipPosition'].append(str(finger.tip_position)[1:-1]) for ix, (d, l, p, n) in enumerate( zip(bone_direc, bone_len, bone_prevJoint, bone_nextJoint)): mydict[d].append(str(finger.bone(ix).direction)[1:-1]) mydict[l].append(str(finger.bone(ix).length)) mydict[p].append(str(finger.bone(ix).prev_joint)[1:-1]) mydict[n].append(str(finger.bone(ix).next_joint)[1:-1]) for ix, name in enumerate(bone_angles): angle = finger.bone(ix).direction.angle_to( finger.bone(ix + 1).direction) mydict[name].append(str(angle * 180 / Leap.PI)) for index, angle in enumerate(finger_angles): mydict[angle].append( str(prox_direc[index].angle_to(prox_direc[index + 1]) * 180 / Leap.PI)) # Get tools tool_num = len(frame.tools) for tool in frame.tools: mydict['ToolPosition'].append(str(tool.tip_position)[1:-1]) mydict['ToolDirection'].append(str(tool.direction)[1:-1]) # Get gestures gesture_num = len(frame.gestures()) for gesture in frame.gestures(): mydict['GestureState'].append(str(self.state_names[gesture.state])) mydict['GestureDuration'].append(str(gesture.duration)) if gesture.type == Leap.Gesture.TYPE_CIRCLE: #circle = Leap.CircleGesture(gesture) mydict['GestureType'].append('circle') #circle has no position attribute mydict['GesturePosition'].append(str([-1.0, -1.0, -1.0])[1:-1]) if gesture.type == Leap.Gesture.TYPE_SWIPE: mydict['GestureType'].append('swip') swipe = Leap.SwipeGesture(gesture) mydict['GesturePosition'].append(str(swipe.position)[1:-1]) if gesture.type == Leap.Gesture.TYPE_KEY_TAP: mydict['GestureType'].append('key tap') keytap = Leap.KeyTapGesture(gesture) mydict['GesturePosition'].append(str(keytap.position)[1:-1]) if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP: mydict['GestureType'].append('screen tap') screentap = Leap.ScreenTapGesture(gesture) mydict['GesturePosition'].append(str(screentap.position)[1:-1]) # insert field length for multi-field values for _, value in mydict.iteritems(): value.insert(0, str(len(value))) # add sing field values mydict['GestureNum'].append(str(gesture_num)) mydict["LeapTime"].append(str(int(time.time() * 1000))) mydict['FrameID'].append(str(frame.id)) mydict['HandNum'].append(str(hand_num)) mydict['ToolNum'].append(str(tool_num)) self.vsocket.send_message(mydict) print 'frame rate: ', frame.current_frames_per_second mydict.clear()
def startNewGame(controller): global currentState global tileList im = Image.open("flanders.gif") #im = blacken15(im) # print "here haha" generateTileList(im) # print "here" imO = makeImage() sendImage(imO) time.sleep(5) randIndex = random.randint(0, len(sLists)-1) (currentState, blankInd) = sLists[randIndex] #get the start game thing #print (currentState, blankInd) scIm = makeImage() #scIm.show() #scrambled image scIm.save("scIm.gif") ### HOPEFULLY IM HAS NOT CHANGED NOW sendImage(scIm) #get gestures lastFrameID = -1 previousTapID = -1 previousTapreached = False while(True): #Get new frame thisFrame = controller.frame() #print 'f' maxHistory = 60 for i in range (maxHistory): frame = controller.frame(i) if (frame.id == lastFrameID): break if (not frame.is_valid): break if (frame.is_valid): for gesture in frame.gestures(): if((i == 0) and (gesture.type is Leap.Gesture.TYPE_SWIPE)): swipe = Leap.SwipeGesture(gesture) #print " SWIPE " resetGame(controller) break return None ###RESET if(gesture.is_valid and (gesture.type is Leap.Gesture.TYPE_SCREEN_TAP)): if (gesture.id != previousTapID): screenTap = Leap.ScreenTapGesture(gesture) previousTapID = gesture.id print "SCREEN_TAP" #handleTap(screenTap, blankInd) break else: previousTapreached = True if (previousTapreached): break lastFrameID = thisFrame.id return None
def on_frame(self, controller): global frame, Pause # Get the most recent frame captured by the Leap Motion and report some basic information frame = controller.frame() #Get gestures for gesture in frame.gestures(): #Return True when fingers are tapping the controller if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP: print "Tapping" if Pause == False: pause() Pause = True else: restart() Pause = False #Detect whether a hand is swiping to the left or the right elif gesture.type == Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) if abs(swipe.direction[0]) > abs(swipe.direction[1]): if swipe.direction[0] < 0: print "Left Swipe" previous_music() elif swipe.direction[0] > 0: print "Right Swipe" next_music() else: print "No swipe" #Detect whether a finger is drawing circle clockwise or anticlockwise elif gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) if (circle.pointable.direction.angle_to(circle.normal) <= Leap.PI / 2): print "clockwise_circle" volume_up() else: print "anticlockwise_circle" volume_down() # Get hands for hand in frame.hands: handType = "Left hand" if hand.is_left else "Right hand" print " %s, id %d, position: %s" % (handType, hand.id, hand.palm_position) # Get the hand's normal vector and direction normal = hand.palm_normal direction = hand.direction # Calculate the hand's pitch, roll, and yaw angles print " pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % ( direction.pitch * Leap.RAD_TO_DEG, normal.roll * Leap.RAD_TO_DEG, direction.yaw * Leap.RAD_TO_DEG) # Get arm bone arm = hand.arm print " Arm direction: %s, wrist position: %s, elbow position: %s" % ( arm.direction, arm.wrist_position, arm.elbow_position) # Get fingers for finger in hand.fingers: print " %s finger, id: %d, length: %fmm, width: %fmm" % ( self.finger_names[finger.type], finger.id, finger.length, finger.width) # Get bones for b in range(0, 4): bone = finger.bone(b) print " Bone: %s, start: %s, end: %s, direction: %s" % ( self.bone_names[bone.type], bone.prev_joint, bone.next_joint, bone.direction) if not frame.hands.is_empty: print ""
else: righthand = all_hands.rightmost lefthand = all_hands.leftmost twohand = True print '2 hands' #update the gesture motion everytime, as the interface will analyse #different frames to anticipate movements made by a person for gesture in righthand.frame.gestures(oldframe): if gesture.state is Leap.Gesture.STATE_START: pass elif gesture.state is Leap.Gesture.STATE_UPDATE: pass elif gesture.state is Leap.Gesture.STATE_STOP: if (gesture.type == Leap.Gesture.TYPE_SWIPE): swipe = Leap.SwipeGesture(gesture) print swipe.speed else: print 'nothing much...' print '\n\n' #this part checks for the utilisation of the index and thumb if (twohand == True): print 'there is a left hand!' if not (lefthand.fingers.is_empty): for finger in lefthand.fingers: if (finger.is_extended == True): if (finger.type == Leap.Finger.TYPE_INDEX): print 'index is up' elif (finger.type == Leap.Finger.TYPE_THUMB):
def get_direction(swipe): """ Returns true if the swipe is horizontal. """ swipe = Leap.SwipeGesture(swipe) return swipe.direction
def on_frame(self, controller): # Get the most recent frame and report some basic information frame = controller.frame() self.prev_frames.pop(0) self.prev_frames.append(self.frames[0]) self.frames.pop(0) self.frames.append(controller.frame()) if not frame.hands.is_empty: if len(frame.hands) == 2: def task_switcher_callback(obj): print "Activating task switcher" obj.active_modes['task_switcher'] = True obj.backend.start_task_switcher() obj.lock_gestures() if not self.active_modes['task_switcher']: self.timer.factory('task_switcher', task_switcher_callback, self) if self.check_gestures_timeout(): for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) self.backend.switch_task(frame, swipe) self.lock_gestures() if len(frame.hands) <= 1 and self.active_modes['task_switcher']: def deactivate_task_switcher_callback(obj): print "Deactivating task switcher" obj.active_modes['task_switcher'] = False obj.backend.release_task_switcher() obj.lock_gestures() self.timer.factory('deactivate_task_switcher', deactivate_task_switcher_callback, self) if len(frame.hands) == 1: # Get the first hand hand = frame.hands[0] # Check if the hand has any fingers fingers = hand.fingers pitch = hand.direction.pitch * RAD_TO_DEG if len(fingers) == 1: pos = self.get_average_palm_position() self.backend.process_pointer(pos) if len(fingers) == 0 and (pitch > 45 or self.active_modes['scroll']): # Checks for scroll timer if 'scroll' not in self.timer.timers: self.timer.start_timer('scroll') elif self.timer.check_timer('scroll'): del self.timer.timers['scroll'] # Toggles scroll mode self.active_modes['scroll'] = not self.active_modes['scroll'] print "Toggle scroll mode to %s" % self.active_modes['scroll'] elif 'scroll' in self.timer.timers: del self.timer.timers['scroll'] if self.active_modes['scroll'] and len(fingers) in (4, 5): if self.timer.check_timer('scroll_sleep'): self.backend.scroll(pitch) del self.timer.timers['scroll_sleep'] if 'scroll_sleep' not in self.timer.timers: self.timer.start_timer('scroll_sleep') # Gestures gesture_found = False if self.check_gestures_timeout(): for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_SWIPE and len(fingers) in (4, 5): swipe = Leap.SwipeGesture(gesture) workspaces = self.backend.generate_workspace_matrix(WORKSPACE_TOTAL, WORKSPACE_COLS) current_position = self.backend.get_position(workspaces, self.backend.get_current_workspace()) new_position = self.backend.find_new_position(workspaces, current_position, swipe.direction) new_workspace = self.backend.get_workspace_by_position(workspaces, new_position) self.backend.move_to_workspace(new_workspace) gesture_found = True if gesture.type == Leap.Gesture.TYPE_CIRCLE and len(fingers) == 4: self.backend.lock_screen() gesture_found = True if gesture.type == Leap.Gesture.TYPE_KEY_TAP and len(fingers) == 1: self.backend.click() self.flush_buffer() if gesture_found: self.lock_gestures() if not (frame.hands.is_empty and frame.gestures().is_empty): pass
def on_frame(self, controller): frame = controller.frame() for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SWIPE and gesture.state is Leap.Gesture.STATE_START: swipe = Leap.SwipeGesture(gesture) swiper = swipe.pointable direction = swipe.direction if (swiper.is_finger and Leap.Finger(swiper).type == Leap.Finger.TYPE_INDEX and direction.z < -0.1): if (direction.x < -0.2 and direction.y > 0.1): print "Red light on" s.sendall(bytearray([0, 1])) elif (direction.x < -0.2 and direction.y < -0.1): print "Red light off" s.sendall(bytearray([0, 0])) if (abs(direction.x) <= 0.2 and direction.y > 0.1): print "Green light on" s.sendall(bytearray([1, 1])) elif (abs(direction.x) <= 0.2 and direction.y < -0.1): print "Green light off" s.sendall(bytearray([1, 0])) if (direction.x > 0.2 and direction.y > 0.1): print "Blue light on" s.sendall(bytearray([2, 1])) elif (direction.x > 0.2 and direction.y < -0.1): print "Blue light off" s.sendall(bytearray([2, 0])) if (direction.y < -0.80 and len(frame.hands) == 2): #Universal turn off all lights function print "Lights out!" s.sendall(bytearray([0, 0])) s.sendall(bytearray([1, 0])) s.sendall(bytearray([2, 0])) if gesture.type is Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) clockwise = circle.pointable.direction.angle_to( circle.normal) <= Leap.PI / 2 global circling completedTurns = (math.floor(circle.progress) >= 2) if not completedTurns: continue if (circling and gesture.state is Leap.Gesture.STATE_STOP and not clockwise): print "Counterclockwise / Volume decreased / STOP" s.sendall(bytearray([255, 0])) circling = False elif (circling and gesture.state is Leap.Gesture.STATE_STOP and clockwise): print "Clockwise / Volume increased / STOP" s.sendall(bytearray([255, 1])) circling = False elif (not circling and not clockwise): print "Counterclockwise / Volume decreased / START" s.sendall(bytearray([255, 2])) circling = True elif (not circling and clockwise): print "Clockwise / Volume increased / START" s.sendall(bytearray([255, 3])) circling = True
def on_frame(self, controller): # Current frame of the leap controller frame = controller.frame() if not frame.hands.is_empty: circle_id = None currentDir = "" for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) # Calculate the angle swept since the last frame if circle.radius > 20 and np.size(frame.pointables) == 1 and frame.pointables[0].touch_distance < 0: # Determine clock direction using the angle between the pointable and the circle normal if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4: clockwiseness = 1 else: clockwiseness = -1 if np.sign(circle.progress * clockwiseness) != np.sign(self.previous_progress) and self.previous_progress != 0: self.slide = self.current_slide print "changement de sens" current_slice = self.getslice(self.slide + 2 * np.round(circle.progress) * clockwiseness) if self.current_slide != current_slice: self.current_slide = current_slice print "changement de slide" self.previous_progress = circle.progress * clockwiseness circle_id = circle.id print "%d %f %f %f %f" % (circle_id, self.slide, self.current_slide, circle.progress, 2 * np.round(circle.progress) * clockwiseness) if circle.state == Leap.Gesture.STATE_STOP and circle_id == circle.id : self.slide = self.current_slide if np.size(frame.hands) == 1: hand = frame.hands[0] fingers = hand.fingers if np.size(fingers) >= 4 and gesture.type == Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) if gesture.id != self.swipeid and self.state_string(gesture.state) == "STATE_START" and abs(swipe.direction.x) > 0.8: # Get direction: -1 (Left) | +1 (Right) if swipe.direction.x <= 0: swipeDirection = -1 else: swipeDirection = 1 # Little hack to only get one swipe instead to swipes from several fingers if currentDir != swipeDirection and abs(swipe.speed) > 500: print "Swipe id: %d, direction: %d, speed: %f, directionv: %s" % ( gesture.id, swipeDirection, swipe.speed, swipe.direction) self.cur_max = np.minimum(self.cur_max + swipeDirection * swipe.speed/5, self.prev_max) self.cur_max = np.maximum(self.cur_max, 0) print self.cur_max if self.cur_max != self.prev_max: self.dcmimage.normalize(maximum=self.cur_max) self.swipeid = gesture.id currentDir = swipeDirection # Define the slice that will be displayed self.set_image() if not frame.hands.is_empty: # Get the interaction_box class interactionBox = frame.interaction_box # Iterate over the fingers detected and display them for i, pointable in enumerate(frame.pointables): normalizedPosition = interactionBox.normalize_point(pointable.tip_position) if(pointable.touch_distance > 0 and pointable.touch_zone != Leap.Pointable.ZONE_NONE): color = (0, 255 - 255 * pointable.touch_distance, 0) elif(pointable.touch_distance <= 0): color = (-255 * pointable.touch_distance, 0, 0) else: color = (0, 0, 200) self.draw(normalizedPosition.x * self.dimX, self.dimY - normalizedPosition.y * self.dimY, 10, color) # Conditions to translate the image cond_translation = [frame.hands[0].translation_probability(self.previous_frame) > 0.9, 3 <= np.size(frame.pointables) <4, frame.pointables[0].touch_distance < 0, frame.pointables[1].touch_distance < 0] # Conditions to scale the image cond_scaling = [frame.hands[0].scale_probability(self.previous_frame) > 0.9, 2 <= np.size(frame.pointables) < 3, frame.pointables[0].touch_distance < 0, frame.pointables[1].touch_distance < 0] # Conditions to rotate the image compteur = 0 condition_rotation = False for pointable in frame.pointables: if pointable.touch_distance < 0: compteur += 1 if compteur == 3: condition_rotation = True compteur = 0 cond_rotation = [frame.hands[0].rotation_probability(self.previous_frame) > 0.9, 4 <= np.size(frame.pointables) <= 5, condition_rotation] # Translate the image if all(cond_translation): translationX = frame.hands[0].translation(self.previous_frame)[0] translationY = frame.hands[0].translation(self.previous_frame)[1] if abs(translationX) > 10: print "translation x %s %s" % (translationX, frame.hands[0].translation_probability(self.previous_frame)) self.dcmimage.transX += translationX self.dcmimage.transform() self.previous_frame = frame if abs(translationY) > 10: print "translation y %s %s" % (translationY, frame.hands[0].translation_probability(self.previous_frame)) self.dcmimage.transY += -translationY self.dcmimage.transform() self.previous_frame = frame # Scale the image elif all(cond_scaling): scalingXY = frame.hands[0].scale_factor(self.previous_frame) if abs(scalingXY - 1) > 0.1: print "scaling %s %s" % (scalingXY, frame.hands[0].scale_probability(self.previous_frame)) if scalingXY > 0: self.dcmimage.scaling += scalingXY - 1 elif scalingXY < 0: self.dcmimage.scaling += 1 - scalingXY self.dcmimage.transform() self.previous_frame = frame elif all(cond_rotation): step = np.rad2deg(frame.hands[0].rotation_axis(self.previous_frame).roll) if step>0: step -= 90 if abs(abs(self.prev_step) - abs(step)) > 5 and abs(step) > 5 and abs(step/3)<30: self.prev_angle += step/3 print "rotation: %f %f" % (step/3, self.prev_angle) self.dcmimage.theta = np.int(-self.prev_angle) self.dcmimage.transform() self.prev_step = step self.previous_frame = frame condition_rotation = False self.prev_step = step else: self.previous_frame = frame # Show final image cv2.imshow(self.name, self.opencv_img)