Exemple #1
0
    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()
Exemple #2
0
    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
Exemple #3
0
    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"
Exemple #4
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
Exemple #5
0
    def on_frame(self, controller):
        # Get the most recent frame and report some basic information
        frame = controller.frame()

        for gesture in frame.gestures():
            if gesture.type is Leap.Gesture.TYPE_KEY_TAP:
                key_tap = Leap.KeyTapGesture(gesture)
                printedid = []
                for hand in key_tap.hands:
                    handType = "Left hand" if hand.is_left else "Right hand"
                    if (hand.id not in printedid):
                        print "  %s, position: %s" % (handType,
                                                      hand.palm_position[0])
                        self.convertedkey = int(
                            (hand.palm_position[0] + 200) / 400 * 16)
                        if (self.convertedkey > 15):
                            self.convertedkey = 15
                        if (self.convertedkey < 0):
                            self.convertedkey = 0
                        print self.convertedkey

                    printedid.append(hand.id)
Exemple #6
0
    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()
Exemple #7
0
    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):
            #print "Frame available"
            frame = controller.frame()
            if not frame.is_valid:
                self.__ca.invalidatePointer()  # TODO Maybe not?
                return
            #print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
            #  frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))

            if len(frame.hands
                   ) != 1:  # User can only point into the cube with 1 hand
                self.__ca.invalidatePointer()

            # Is image stabilised?
            for hand in frame.hands:
                if hand.time_visible < self.MIN_TIME_VISIBLE:  #TODO: What are the time units? seconds?
                    self.__ca.invalidatePointer()
                    return

            # If we're grabbing, rotate the scene:

            # We're grabbing -> rotate:
            if self.__weReGrabbing(frame):
                # Get overall frame rotation:
                # Get latest valid frame:
                for i in range(self.SEARCH_WINDOW, 0, -1):
                    start_frame = controller.frame(i)
                    if start_frame.is_valid:
                        break

                windowDistance = self.SEARCH_WINDOW - i
                if windowDistance < self.MIN_WINDOW_SIZE:
                    return  # Window too short

                #windowFactor = self.SEARCH_WINDOW / windowDistance

                if not start_frame.is_valid:
                    return

                if not self.__weReGrabbing(start_frame):
                    return  # We have to be grabbing on both frames to rotate

                # If the probability that user wanted to rotate is sufficient:
                #if frame.rotation_probability(start_frame) > 0.1:

                rotX = self.__cropRot(
                    frame.rotation_angle(start_frame, Vector.x_axis) *
                    self.FACTOR_ROTX)
                rotY = self.__cropRot(
                    frame.rotation_angle(start_frame, Vector.y_axis) *
                    self.FACTOR_ROTY)
                rotZ = self.__cropRot(
                    frame.rotation_angle(start_frame, Vector.z_axis) *
                    self.FACTOR_ROTZ)
                #dbg("Adding Rotation: " + str(-rotY) + ", " + str(-rotX) + ", " + str(rotZ))
                # TODO: modify by some factor
                self.__ca.addRotation(-rotY, -rotX, rotZ)
                self.__ca.invalidatePointer()

            else:  # We're not grabbing:
                # If there is only 1 hand

                # Was there a gesture in the frame?
                if len(frame.gestures()) == 1:
                    for g in frame.gestures():
                        if g.is_valid and g.type is Leap.Gesture.TYPE_KEY_TAP:
                            key_tap = Leap.KeyTapGesture(g)
                            if (key_tap.pointable == frame.hands[0].fingers[1]
                                    and key_tap.direction.y < -0.8):
                                self.__ca.announcePlacement(
                                )  # Announce placement request to CA
                                # TODO: delete after debugging:
                                keyTap = Leap.KeyTapGesture(g)
                                tap_point = keyTap.position
                                tapPointStr = "(" + str(
                                    tap_point.x) + ", " + str(
                                        tap_point.y) + ", " + str(
                                            tap_point.z) + ")"
                                tap_direction = keyTap.direction
                                tapDirStr = "(" + str(
                                    tap_direction.x) + ", " + str(
                                        tap_direction.y) + ", " + str(
                                            tap_direction.z) + ")"
                                dbg("Detected key tap gesture at " +
                                    tapPointStr + " with direction " +
                                    tapDirStr)

                # Update pointer
                if (len(frame.hands) == 1):
                    #ptrPos = frame.hands[0].stabilized_palm_position
                    ptrPos = frame.hands[0].palm_position
                    px = ptrPos.x * self.POS_X_SCALE + self.POS_X_LINEAR
                    py = ptrPos.y * self.POS_Y_SCALE + self.POS_Y_LINEAR
                    pz = ptrPos.z * self.POS_Z_SCALE + self.POS_Z_LINEAR

                    #dbg("Palm position update: " + str(px) + ", " + str(pz) + ", " + str(py))
                    self.__ca.updatePointer(
                        px, pz, py
                    )  #TODO: devide by some factor to fit Panda's coordinates
Exemple #9
0
    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"
Exemple #10
0
    def on_frame(self, controller):
        global direccion_media, posicion_media, tutorial_activo_leap, tutorial_iniciado_leap, inicio_tutorial, tiempo_baqueta_tutorial, string_sonidos
        global cambiosonido_iniciado, inicio_cambiosonido, tiempo_cambiosonido, num_instrumento
        global hubo_gesto

        # Tomamos un frame
        frame = controller.frame()

        # Obtenemos la posicion y la direccion de la baqueta(s)
        for tool in frame.tools:
            posicion_media.append(tool.tip_position)
            direccion_media.append(tool.direction)

        if not tutorial_activo_leap:
            # Comprobamos los gestos
            for gesture in frame.gestures():
                # Key tap (gesto similar al de pulsar una tecla)
                if gesture.type == Leap.Gesture.TYPE_KEY_TAP:

                    keytap = Leap.KeyTapGesture(gesture)
                    pos = keytap.position
                    hubo_gesto = True

                    tolerancia = 1.5

                    if self.DEBUG:
                        print "pos = ", pos,

                    # Dependiendo de la región, reproducimos un sonido u otro
                    if -graficos.desplazamiento_bateria <= pos[
                            0] <= -graficos.comienzo_bateria and -graficos.desplazamiento_bateria <= pos[
                                2] <= -graficos.comienzo_bateria:
                        if self.DEBUG: print "Sonido 0"
                        self.sonidos[0].play()
                    if graficos.comienzo_bateria <= pos[
                            0] <= graficos.desplazamiento_bateria and -graficos.desplazamiento_bateria <= pos[
                                2] <= -graficos.comienzo_bateria:
                        if self.DEBUG: print "Sonido 1"
                        self.sonidos[1].play()
                    if -graficos.desplazamiento_bateria <= pos[
                            0] <= -graficos.comienzo_bateria and graficos.comienzo_bateria <= pos[
                                2] <= graficos.desplazamiento_bateria:
                        if self.DEBUG: print "Sonido 2"
                        self.sonidos[2].play()
                    if graficos.comienzo_bateria <= pos[
                            0] <= graficos.desplazamiento_bateria and graficos.comienzo_bateria <= pos[
                                2] <= graficos.desplazamiento_bateria:
                        if self.DEBUG: print "Sonido 3"
                        self.sonidos[3].play()

                    if self.DEBUG:
                        print "  Key Tap id: %d, %s, position: %s, direction: %s" % (
                            gesture.id, self.state_names[gesture.state],
                            keytap.position, keytap.direction)
                if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
                    graficos.menu_activo = not graficos.menu_activo

        # Lógica para hacer el gesto de reconocimiento
        if tutorial_activo_leap and posicion_media:
            if not tutorial_iniciado_leap:
                if graficos.comienzo_bateria <= posicion_media[0][
                        0] <= graficos.desplazamiento_bateria and graficos.comienzo_bateria <= posicion_media[
                            0][2] <= graficos.desplazamiento_bateria:
                    tutorial_iniciado_leap = True
                    inicio_tutorial = time.time()
            else:
                if time.time() - inicio_tutorial < tiempo_baqueta_tutorial:
                    if not (graficos.comienzo_bateria <= posicion_media[0][0]
                            <= graficos.desplazamiento_bateria and
                            graficos.comienzo_bateria <= posicion_media[0][2]
                            <= graficos.desplazamiento_bateria):
                        tutorial_iniciado_leap = False
                else:
                    tutorial_activo_leap = False

        # Lógica para cambiar el sonido de una región
        if not tutorial_activo_leap and posicion_media:
            if not cambiosonido_iniciado:
                if -graficos.desplazamiento_bateria <= posicion_media[0][
                        0] <= -graficos.comienzo_bateria and -graficos.desplazamiento_bateria <= posicion_media[
                            0][2] <= -graficos.comienzo_bateria:
                    num_instrumento = 0
                if graficos.comienzo_bateria <= posicion_media[0][
                        0] <= graficos.desplazamiento_bateria and -graficos.desplazamiento_bateria <= posicion_media[
                            0][2] <= -graficos.comienzo_bateria:
                    num_instrumento = 1
                if -graficos.desplazamiento_bateria <= posicion_media[0][
                        0] <= -graficos.comienzo_bateria and graficos.comienzo_bateria <= posicion_media[
                            0][2] <= graficos.desplazamiento_bateria:
                    num_instrumento = 2
                if graficos.comienzo_bateria <= posicion_media[0][
                        0] <= graficos.desplazamiento_bateria and graficos.comienzo_bateria <= posicion_media[
                            0][2] <= graficos.desplazamiento_bateria:
                    num_instrumento = 3
                if num_instrumento is not None:
                    cambiosonido_iniciado = True
                    inicio_cambiosonido = time.time()
            else:
                if hubo_gesto:
                    cambiosonido_iniciado = False
                elif time.time() - inicio_cambiosonido < tiempo_cambiosonido:
                    if num_instrumento == 0:
                        if not (-graficos.desplazamiento_bateria <=
                                posicion_media[0][0] <=
                                -graficos.comienzo_bateria
                                and -graficos.desplazamiento_bateria <=
                                posicion_media[0][2] <=
                                -graficos.comienzo_bateria):
                            cambiosonido_iniciado = False
                    elif num_instrumento == 1:
                        if not (graficos.comienzo_bateria <= posicion_media[0]
                                [0] <= graficos.desplazamiento_bateria
                                and -graficos.desplazamiento_bateria <=
                                posicion_media[0][2] <=
                                -graficos.comienzo_bateria):
                            cambiosonido_iniciado = False
                    elif num_instrumento == 2:
                        if not (-graficos.desplazamiento_bateria <=
                                posicion_media[0][0] <=
                                -graficos.comienzo_bateria
                                and graficos.comienzo_bateria <=
                                posicion_media[0][2] <=
                                graficos.desplazamiento_bateria):
                            cambiosonido_iniciado = False
                    elif num_instrumento == 3:
                        if not (graficos.comienzo_bateria <= posicion_media[0]
                                [0] <= graficos.desplazamiento_bateria
                                and graficos.comienzo_bateria <=
                                posicion_media[0][2] <=
                                graficos.desplazamiento_bateria):
                            cambiosonido_iniciado = False
                else:
                    string_sonidos[
                        num_instrumento] = logicamenu.cambio_instrumento(
                            string_sonidos)
                    self.sonidos = [
                        pygame.mixer.Sound("sonidos/" + s)
                        for s in string_sonidos
                    ]

                    cambiosonido_iniciado = False

        # Si hay baquetas, redibujar
        if posicion_media:
            graficos.redibujar()

        # Quitar baquetas
        posicion_media = []
        direccion_media = []
        hubo_gesto = False
Exemple #11
0
    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: