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 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))) # Get gesture for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SCREEN_TAP: screen_tap = Leap.ScreenTapGesture(gesture) # 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 main(): controller = Leap.Controller() while (not (controller.is_connected)): continue print "connected" enable_swipe(controller) enable_screentap(controller) lastFrameID = -1 previousTapID = -1 previousTapreached = False while (True): # frame = controller.frame() # if(frame.is_valid): # print frame.id # for gesture in frame.gestures(): # if(gesture.type is Leap.Gesture.TYPE_SWIPE): # swipe = Leap.SwipeGesture(gesture) # print " SWIPE " thisFrame = controller.frame() 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 " if (gesture.is_valid and (gesture.type is Leap.Gesture.TYPE_SCREEN_TAP)): if (gesture.id != previousTapID): screenTap = Leap.ScreenTapGesture(gesture) tapper = screenTap.pointable if (tapper.is_finger): finger = Leap.Finger(tapper) if (finger.is_valid and finger.type == 1): previousTapID = gesture.id print "SCREEN_TAP" selectTile(screenTap) else: previousTapreached = True if (previousTapreached): break lastFrameID = thisFrame.id return None
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 on_frame(self, controller): frame = controller.frame() for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SCREEN_TAP: screen_tap = Leap.ScreenTapGesture(gesture) if not frame.hands.is_empty: hand = frame.hands[0] wp = hand.arm.wrist_position # print("wp",wp[0],wp[1],wp[2]) s6, s5, s4, alfa, beta = self.CovToServoAngel(wp[0], wp[1], wp[2]) self.alfa = 180 * alfa / math.pi self.beta = 180 * beta / math.pi a = [] for finger in hand.fingers: for b in range(0, 4): bone = finger.bone(b) a.append(finger) index_point = (a[1].bone(0).next_joint[0], a[1].bone(0).next_joint[1], a[1].bone(0).next_joint[2]) pinky_point = (a[4].bone(0).next_joint[0], a[4].bone(0).next_joint[1], a[4].bone(0).next_joint[2]) index_pinky = (index_point[0] - pinky_point[0], index_point[1] - pinky_point[1], index_point[2] - pinky_point[2]) vector = (index_pinky[0] / 57, index_pinky[1] / 57, index_pinky[2] / 57) thumb_tip = (a[0].bone(3).next_joint[0] / 10, a[0].bone(3).next_joint[1] / 10, a[0].bone(3).next_joint[2] / 10) index_tip = (a[1].bone(3).next_joint[0] / 10, a[1].bone(3).next_joint[1] / 10, a[1].bone(3).next_joint[2] / 10) distance = math.pow((thumb_tip[0] - index_tip[0]), 2) + math.pow( (thumb_tip[1] - index_tip[1]), 2) + math.pow( (thumb_tip[1] - index_tip[1]), 2) v_y = vector[1] if vector[2] < 0: #下降 cita3 = 180 - alfa - beta - 90 * vector[1] else: #上升 cita3 = (90 * v_y) - alfa - beta if cita3 < 120 and cita3 > -120: engine_3 = round(500 - 4.15 * cita3) elif (cita3 < -120): engine_3 = 1000 else: engine_3 = 0 if vector[1] > 0: cita2 = 90 * vector[0] else: cita2 = -180 - 90 * vector[0] if cita2 < 120 and cita2 > -120: engine_2 = round(500 + 4.15 * cita2) elif cita2 < -120: engine_2 = 1000 else: engine_2 = 0 if distance < 8: engine_1 = 10 else: engine_1 = 500 #print("cita3,cita2,alfa,beta:",cita3,cita2,alfa,beta) self.putData(engine_1, engine_2, engine_3, s4, s5, s6) else: self.putData(500, 500, 700, 600, 200, 500)
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): frame = controller.frame( ) # atribuindo a variavel frame o retorno de informações dos ultimos qudros captadas pelo leap motion #frame_penultimo_gesto = controller.frame(1) # atribuindo a variavel frame_penultimo_gesto o retorno de informações do penultimo gesto for hand in frame.hands: for gesture in frame.gestures(): #cada gesto tem seu ID criado if gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = CircleGesture(gesture) #instanciando o objeto da classe gesto circulo # aqui o movimento circular é o de rotacionar o dedo na frente do computador if circle.pointable.direction.angle_to( circle.normal) <= Leap.PI / 2: clockwiseness = "clockwise" # indica que o circulo esta em sentido horario else: clockwiseness = "counter-clockwise" #indica que o circulo esta em sentido anti #swept angle vai dizer o qual longe vc foi no circulo desde a ultimo circulo identificado swept_angle = 0 if circle.state != Leap.Gesture.STATE_START: previous = CircleGesture( controller.frame(1).gesture(circle.id) ) #criando um novo frame ao escrever frame(1) swept_angle = (circle.progress - previous.progress) * 2 * Leap.PI # imprime no console o tamanho do circulo que voê gesticula e o sentido horario ou anti-horario caneta.pensize(2) caneta.circle(circle.radius) print "Radius: " + str(circle.radius) + " " + clockwiseness if gesture.type == Leap.Gesture.TYPE_SWIPE: #instanciando o objeto da classe gesto swipe swipe = SwipeGesture(gesture) #instnciando o objet da classe gesto da direção do swipe swipeDir = swipe.direction #criações das condições dos prints de acordo com as cordenadas estabelecidas pelo leap if ( swipeDir.x > 0 and math.fabs(swipeDir.x) > math.fabs(swipeDir.y) ): #se a posição da mão nas cordenas do leap for x>0, o seja mão indo para a direita print 'Swiped right' #(parte do and):também precisa priorisara leitura do movimento em x para facilitar a leitura do leap caneta.seth(0) caneta.ht() caneta.fd(10) elif ( swipeDir.x < 0 and math.fabs(swipeDir.x) > math.fabs(swipeDir.y) ): #se a posiçao da mão nas coodernadas do Leap x<0, ou seja, mão indo para esquerda print 'Swiped left' #(parte do and):também precisa priorisara leitura do movimento em x para facilitar a leitura do leap caneta.seth(180) caneta.ht() caneta.fd(10) elif ( swipeDir.y > 0 and math.fabs(swipeDir.x) < math.fabs(swipeDir.y) ): #se a posiçao da mão nas coodernadas do Leap y>0, ou seja, mão indo para cima print 'Swiped up' #(parte do and):também precisa priorisara leitura do movimento em y para facilitar a leitura do leap caneta.seth(90) caneta.ht() caneta.fd(10) elif ( swipeDir.y < 0 and math.fabs(swipeDir.x) < math.fabs(swipeDir.y) ): #se a posiçao da mão nas coodernadas do Leap x<0, ou seja, mão indo para baixo print 'Swiped down' #(parte do and):também precisa priorisara leitura do movimento em y para facilitar a leitura do leap caneta.seth(270) caneta.ht() caneta.fd(10) if gesture.type == Leap.Gesture.TYPE_KEY_TAP: #gesto de pinça key_tap = Leap.KeyTapGesture(gesture) tap_point = key_tap.position tapper = key_tap.pointable caneta.pd() print('caneta down') if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP: #gesto de touch na tela screen_tap = Leap.ScreenTapGesture(gesture) caneta.pu() print('caneta up')
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: