def on_frame(self, controller): # print("Frame available") frame = controller.frame() for gesture in frame.gestures(): if gesture.type is not Leap.Gesture.TYPE_CIRCLE: continue if gesture.state is Leap.Gesture.STATE_STOP: self.previous_circle_progress = 0 continue circle = Leap.CircleGesture(gesture) clockwise = circle.pointable.direction.angle_to( circle.normal) <= Leap.PI / 2 value = self.volumeStep if clockwise else -self.volumeStep coef = circle.progress - self.previous_circle_progress value *= abs(coef) self.previous_circle_progress = circle.progress self.volumePercent += value if self.volumePercent < 0: self.volumePercent = 0 elif self.volumePercent > 100: self.volumePercent = 100 print("Volume = {} %".format(self.volumePercent)) if self.volumePercent != self.previousVolumePercent: self.send_to_client() self.previousVolumePercent = self.volumePercent Leap.Listener.on_frame(self, controller)
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: mouse_movement_status = True controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE) controller.config.set("Gesture.Circle.MinRadius", 10.0) controller.config.set("Gesture.Circle.MinArc", .5) controller.config.save() if len(frame.fingers.extended()) == 5: for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_CIRCLE: mouse_movement_status = False circle = Leap.CircleGesture(gesture) if circle.pointable.direction.angle_to( circle.normal) <= Leap.PI / 2: win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, 10, 0) else: win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -10, 0) else: mouse_movement_status = True self.mouse_move(frame.hands[0], mouse_movement_status)
def get_frame(self, controller): frame = controller.frame() gestures = frame.gestures() types = [i.type for i in gestures] if len(frame.hands) == 0: return None if len(frame.hands) == 2: return "stop" if Leap.Gesture.TYPE_CIRCLE in types: for gesture in gestures: circle = Leap.CircleGesture(gesture) if (circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/2): return "next" else: return "prev" if len(frame.hands) == 1: hand = frame.hands[0] speed = hand.palm_velocity.magnitude num_fingers = len(frame.fingers.extended()) if speed < 200: if num_fingers == 0: return "play/pause" if num_fingers == 2: return "prev playlist" if num_fingers == 3: return "next playlist" if num_fingers == 5: return "mode change" time.sleep(0.1)
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 eval_state(self, state): left_circle = None right_circle = None rc = lc = False if (state.has_key('left') and state.has_key('right')): for g in state['left']['gestures']: if (g.type == Leap.Gesture.TYPE_CIRCLE): left_circle = Leap.CircleGesture(g) lc = True break for g in state['right']['gestures']: if (g.type == Leap.Gesture.TYPE_CIRCLE): right_circle = Leap.CircleGesture(g) rc = True break if (lc and rc): if (left_circle.pointable.direction.angle_to( left_circle.normal) <= Leap.PI / 4.0): left_dir = 'cw' else: left_dir = 'ccw' if (right_circle.pointable.direction.angle_to( right_circle.normal) <= Leap.PI / 4.0): right_dir = 'cw' else: right_dir = 'ccw' if (left_dir != right_dir): if (left_dir == 'cw'): self.operation = 'merge' else: self.operation = 'split' return True return False
def Handler(self,frame): hands = frame.hands if hands: Hand = None for item in hands: if item.is_right: Hand = item if Hand: fingers = Hand.fingers extended_fingers = fingers.extended() self.mode = 0 Ring = fingers.finger_type(3)[0] Pinky = fingers.finger_type(4)[0] if 2<= len(extended_fingers) <=3: if Ring not in extended_fingers and Pinky not in extended_fingers: Middle = fingers.finger_type(2)[0] pinky_to_mid = Pinky.direction.angle_to(Middle.direction) pinky_to_palm = Pinky.direction.angle_to(Hand.direction) if 0.1 <= pinky_to_palm <= 3.5 and 0.1 <= pinky_to_mid <= 2.5: self.mode = 1 if self.clickPoint is None: self.clickPoint = Hand.palm_position.z #self.cursor_level = self.hideCursor() elif Hand.grab_strength > 0.93 and Hand.palm_normal.x < -0.6: self.mode = 2 else: pass if self.mode == 0: #print "Pointer" self.clickPoint = None self.zoomCoord = None self.Pointer(Hand,0) if self.cursor_level is not None: win32api.ShowCursor(self.cursor_level) elif self.mode == 1: #print "Scroller" self.Scroller(Hand) if self.cursor_level is not None: win32api.ShowCursor(self.cursor_level) elif self.mode == 2: self.clickPoint = None self.zoomCoord = None #print "Grabber" self.Pointer(Hand,1) else: pass for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) if circle.radius > 50: self.volumeSetter(circle)
def handle(self, hands): global viz_mode if (not self.eval_state(hands[0])): return for gesture in hands[0][self.hand]['gestures']: if (gesture.type == Leap.Gesture.TYPE_CIRCLE): circle = Leap.CircleGesture(gesture) if circle.pointable.direction.angle_to( circle.normal) <= Leap.PI / 4.0: clockwise = True else: clockwise = False if circle.state != Leap.Gesture.STATE_START: self.progress += circle.progress - self.prev_progress self.prev_progress = circle.progress if (self.progress > 1.0): if (clockwise): viz_mode[self.hand] = viz_mode[self.hand] % 4 + 1 if (viz_mode[self.hand] == 3): viz_mode[self.hand] += 1 else: viz_mode[ self.hand] = (viz_mode[self.hand] - 2) % 4 + 1 if (viz_mode[self.hand] == 3): viz_mode[self.hand] -= 1 sendds(type=5, mode=viz_mode[self.hand], hdir=0, ddir=0, graph=0 if self.hand == 'left' else 1) print('visualization mode changed') self.progress %= 1.0 else: self.prev_progress = circle.progress
def calculateVolume(): if not self.gestured: return self.volume / 100 gesture = self.frame.gestures()[0] circle = Leap.CircleGesture(gesture) if circle.pointables[0].direction.angle_to( circle.normal) <= math.pi / 2: self.volume += 1 else: self.volume -= 1 if self.volume > 100: self.volume = 100 elif self.volume < 0: self.volume = 0
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() gestures = frame.gestures() self.frame = frame if len(gestures) > 0: if gestures[0].type == Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gestures[0]) if circle.center[1] > 250: self.volume += 1 if circle.pointables[0].direction.angle_to(circle.normal) <= math.pi / 2 else -1 if self.volume > 100: self.volume = 100 elif self.volume < 0: self.volume = 0 self.gestured = False self.path.append(frame)
def handle(self, frame): if frame.gestures(): self.back_stack.append(frame) if len(self.back_stack) > self.BACK_STACK_MAX_LEN: self.back_stack.pop(self.BACK_STACK_MAX_LEN / 2) # add stuff to counter self.counter['frames'] += 1 self.counter['hands'] += len(frame.hands) self.counter['fingers_a'] += len(frame.hands[0].fingers) self.counter['fingers_b'] += len(frame.hands[1].fingers) for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_CIRCLE: self.counter['gesture_circles'] += 1 c = Leap.CircleGesture(gesture) if (c.pointable.direction.angle_to(c.normal) <= Leap.PI / 2): self.counter['circle_direction'] += 1 else: self.counter['circle_direction'] -= 1 elif gesture.type == Leap.Gesture.TYPE_SWIPE: self.counter['gesture_swipes'] += 1 elif gesture.type == Leap.Gesture.TYPE_KEY_TAP: self.counter['gesture_key_taps'] += 1 elif gesture.type == Leap.Gesture.TYPE_SCREEN_TAP: self.counter['gesture_screen_taps'] += 1 return None if not frame.gestures() and self.back_stack: if self.no_gesture_counter < self.NO_GESTURE_COUNTER_MAX_LEN: self.no_gesture_counter += 1 return None gestures = {key: value for key, value in self.counter.items() if key.startswith('gesture')} gesture = max(gestures, key=lambda x: gestures[x]) if not len(self.back_stack) >= self.BACK_STACK_MAX_LEN: self.init_data_structures() if gesture == 'gesture_key_taps': return 'TAP' if gesture == 'gesture_screen_taps': return 'SCREEN_TAP' else: print('back_stack too short and not a tap', gesture) return None # calculate back_stack and counter means frames = self.counter['frames'] hands = round(float(self.counter['hands']) / frames) fingers_a = round(float(self.counter['fingers_a']) / frames) fingers_b = round(float(self.counter['fingers_b']) / frames) vec = {'start': {'l': [x.hands[0].palm_position for x in self.back_stack[:(self.BACK_STACK_MAX_LEN / 2)]]}, 'stop': {'l': [x.hands[0].palm_position for x in self.back_stack[(self.BACK_STACK_MAX_LEN / 2):]]} } if gesture == 'gesture_circles': l = ['clockwise', 'counterclockwise'] direction = l[0] if \ self.counter['circle_direction'] >= 0 else l[1] else: for name in ['start', 'stop']: vec_len = len(vec[name]['l']) vec[name]['x'] = sum([x[0] for x in vec[name]['l']]) / vec_len vec[name]['y'] = sum([x[1] for x in vec[name]['l']]) / vec_len vec[name]['z'] = sum([x[2] for x in vec[name]['l']]) / vec_len diff = {axis: (vec['stop'][axis] - vec['start'][axis]) for axis in ['x', 'y', 'z']} max_diff = max(diff, key=lambda x: abs(diff[x])) direction = ('+' if diff[max_diff] > 0 else '-') + max_diff # return solution sol = (hands, fingers_a, fingers_b, gesture, direction) # empty back_stack and counter self.init_data_structures() return sol
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): 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 ""
def on_frame(self, controller): tempa = 0 #@@@@@print "NEW CYCLE___________________________________" frame = controller.frame() """#print "frame-id" + str(frame.id) + " Time_stamp:" \ #+ str(frame.timestamp) + " hands:" + str(len(frame.hands)) \ #+ " fingers" + str(len(frame.fingers)) \ #+ " no: of gestures detected" + str(len(frame.gestures()))""" handType = -1 #==============================================================================================# #==============================================================================================# for hand in frame.hands: handType = 1 if hand.is_left else 0 ### print "NO MOVE" if handType == 1: op = 00000 opt1 = 0 opt2 = 0 opt3 = 0 opt4 = 0 opt5 = 0 for finger in hand.fingers: fingertype = self.finger_names[finger.type] ### print 'finger: ' + str(fingertype) # + 'l= ' + str(finger.length) #for b in range(0,4): bne = finger.bone(2) if fingertype == 1: if (bne.direction[1] * Leap.RAD_TO_DEG) < 0: opt1 = 0 else: opt1 = 0 elif fingertype == 2: if (bne.direction[2] * Leap.RAD_TO_DEG) > 0: opt2 = 1 else: opt2 = 0 elif fingertype == 3: if (bne.direction[2] * Leap.RAD_TO_DEG) > 0: opt3 = 1 else: opt3 = 0 elif fingertype == 4: if (bne.direction[2] * Leap.RAD_TO_DEG) > 0: opt4 = 1 else: opt4 = 0 elif fingertype == 5: if (bne.direction[2] * Leap.RAD_TO_DEG) > 0: opt5 = 1 else: opt5 = 0 else: pass #op = 10000*opt1 + 1000*opt2 + 100*opt3 + 10*opt4 +opt5 op = opt1 + opt2 + opt3 + opt4 + opt5 #bn = self.bone_names[bne.type] ### print 'print :'+str(op) #==============================================================================================# #==============================================================================================# self.prev = self.cur self.cur = op if self.prev == self.cur: self.times = self.times + 1 elif self.prev != self.cur: self.times = 0 if self.times == 7: self.sp = op #####ls = self.sp #==============================================================================================# self.pa = self.ca self.ca = self.sp if self.pa == self.ca: x = 0 else: client.write_register(3603, self.ca) print "AXIS/JOINT TO MOVE :: " + str(self.ca) time.sleep(0.5) #==============================================================================================# #==============================================================================================# ### print "end of 1 hand" #@@@@@clockwiseness = 0 #==============================================================================================# #==============================================================================================# if len(frame.gestures()) == 0: clockwiseness = 0 for gesture in frame.gestures(): """if gesture.type != Leap.Gesture.TYPE_CIRCLE and gesture.type != Leap.Gesture.TYPE_KEY_TAP: clockwiseness == 0 ###print "joint to move: " +str(self.sp) +"dir :" +str(clockwiseness)""" if gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) #print str(circle.pointable) if circle.pointable.direction.angle_to( circle.normal) <= Leap.PI / 2: clockwiseness = 1 else: clockwiseness = 2 #print "Circle detected" + str(clockwiseness)+" no of fingers :" #print str(gesture.handType) #3# if len(circle.pointable) == 1: # tempa = 1 #else: #tempa = 0 # if tempa == 1 : #==============================================================================================# #==============================================================================================# self.prevg = self.curg self.curg = clockwiseness if self.prevg == self.curg: self.timesg = self.timesg + 1 #print "#-----------------------------------------ignoring state change" elif self.prevg != self.curg: self.timesg = 0 if self.timesg == 2: self.spg = clockwiseness self.pb = self.cb self.cb = self.spg if self.pb == self.cb: y = 0 else: print "MOTION DIRECTION :: " + str(self.cb) client.write_register(3604, self.cb)
def on_frame(self, controller): for gesture in controller.frame().gestures(): if gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) if circle.radius > self.RADIUS_THRESHOLD and circle.pointable.tip_velocity > self.VELOCITY_THRESHOLD: self.set_volume(circle)
if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP: print "Tapping" if Pause == False: pause() Pause = True else: restart() Pause = False 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" 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()
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)
def update_view(self, frame, do_rotation, do_translation): if not self.prev_frame: return #check what mode to set, also make directional in future #currently disabled, does not match rest of the program # if len(frame.hands) == 2: # for gest in frame.gestures(): # if gest.type is Leap.Gesture.TYPE_SWIPE: # if Leap.SwipeGesture(gest).direction.y > 0.5 and gest.duration_seconds > 0.15: # time.sleep(0.3) # if self.mode == 'view': # self.mode = 'edit' # cmd.bg_color("white") # else: # self.mode = 'view' # cmd.bg_color("black") # do_rotation = False # do_translation = False # print 'Changing mode to: ' + self.mode # time.sleep(0.6) # break for gest in frame.gestures(): if gest.type is Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gest) if circle.progress >= 1.5: # and len(frame.hands)==1: self.circom = 0 if self.circom == 0 and len(frame.gestures()) == 0: self.circom = 1 if len(frame.hands) == 1: cmd.center("all", 0, 1) elif len(frame.hands) == 2: cmd.orient("all") if frame.hands.rightmost.rotation_probability( self.prev_frame) > 0.1 and do_rotation == True: #print 'rotating' rotation_about_x = frame.hands.rightmost.rotation_angle( self.prev_frame, Vector.x_axis) rotation_about_y = frame.hands.rightmost.rotation_angle( self.prev_frame, Vector.y_axis) rotation_about_z = frame.hands.rightmost.rotation_angle( self.prev_frame, Vector.z_axis) #print rotation_about_x, rotation_about_y, rotation_about_z cmd.rotate('x', rotation_about_x * 100) cmd.rotate('y', rotation_about_y * 100) cmd.rotate('z', rotation_about_z * 100) #m = frame.hands.rightmost.rotation_matrix(self.prev_frame) #print m #m *= Matrix(Vector(*view[0:3]), # Vector(*view[3:6]), # Vector(*view[6:9])) #view[:9] = m.to_array_3x3() elif frame.hands.rightmost.translation_probability( self.prev_frame) > 0.1 and do_translation == True: translation = frame.hands.rightmost.translation(self.prev_frame) #print translation.to_float_array() cmd.translate(translation.to_float_array())
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): left_bound = -50 right_bound = 50 up_bound = 360 down_bound = 220 back_bound = 120 forward_bound = -50 frame = controller.frame() hand = frame.hands.rightmost position = hand.palm_position velocity = hand.palm_velocity direction = hand.direction controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP) controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE) #Key-tap gesture for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_KEY_TAP: key_tap = Leap.KeyTapGesture(gesture) print("Turning drone on") if gesture.type is Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) print("Turning off") 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) # print hand.palm_position #center point (0,200,0) #x-axis if (hand.palm_position[0] < left_bound): print("Moving left") elif (hand.palm_position[0] > right_bound): print("Moving right") #y-axis if (hand.palm_position[1] > up_bound): print("Moving up") elif (hand.palm_position[1] < down_bound): print("Moving down") #z-axis if (hand.palm_position[2] > back_bound): print("Moving back") elif (hand.palm_position[2] < forward_bound): print("Moving forward") if (hand.palm_position[0] > left_bound) and (hand.palm_position[0] < right_bound) and ( hand.palm_position[1] < up_bound) and ( hand.palm_position[1] > down_bound) and ( hand.palm_position[2] < back_bound) and ( hand.palm_position[2] > forward_bound): print "Not moving"