Exemple #1
0
def proyManoXY(mano):
    #Vectores
    direccion = mano.direction
    posPalma = mano.palm_position
    normPalma = mano.palm_normal
    dedos = mano.fingers
    pulgar = dedos[0]  #Pulgar
    indice = dedos[1]  #Indice
    medio = dedos[2]  #Medio
    anular = dedos[3]  #Anular
    menique = dedos[4]  #Menique

    #componentes en x (flotantes)
    xDireccion = direccion.dot(x)
    xPosPalma = posPalma.dot(x)
    xNormPalma = normPalma.dot(x)

    #componentes en y
    yDireccion = direccion.dot(y)
    yPosPalma = posPalma.dot(y)
    yNormPalma = normPalma.dot(y)

    proyeccion = {}
    proyeccion["direccion"] = Leap.Vector(xDireccion, yDireccion, 0)
    proyeccion["normal"] = Leap.Vector(xNormPalma, yNormPalma, 0)

    #Impresiones
    #print("Datos Originales");
    #print(direccion.to_tuple())
    #print(normPalma.to_tuple())

    #print("Proyeccion a XY")
    #print(proyeccion["direccion"].to_tuple())
    #print(proyeccion["normal"].to_tuple())
    return proyeccion()
Exemple #2
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:

            # No left hand inputs allowed
            if hand.is_left:
                # if a left hand is detected, then reset
                print("Left hand shown, reset robot arm")
                angles = "0 0 20 0 0 0"
                self.serialConnection.write(angles)
                time.sleep(2.0)
                break

            # We need to return the palm position, or alternatively the wrist position
            hand.palm_position

            # We need to return direction vector of the hand
            hand.direction

            # Return the state of the fist
            self.is_fist(hand)

            # We need to return the direction vector of the arm
            hand.arm.direction
            # the yaw can be deduced from this

            wrist_movement_direction = Leap.Vector(0, 0, 0)

            if self.previous_wrist_position != Leap.Vector(0, 0, 0):
                wrist_movement_direction = hand.arm.wrist_position - self.previous_wrist_position

            # move it forward
            self.theta1 = self.theta1 - wrist_movement_direction.z * VEL_SCALE
            self.theta2 = self.theta2 - wrist_movement_direction.y * VEL_SCALE
            #self.theta3 = self.theta3 - wrist_movement_direction.y*VEL_SCALE
            print(wrist_movement_direction)

            self.previous_wrist_position = hand.arm.wrist_position

            # Call Richard's math function
            #theta_values = end_effector_position(hand.arm.wrist_position, hand.direction)

            #theta1, theta2 = calculateInverseKinematics(hand.arm.wrist_position.y, -hand.arm.wrist_position.z)

            if self.is_fist(hand) == True:
                pincer_value = PINCER_ANGLE
            else:
                pincer_value = 0

            angles = "0 " + str(
                self.theta1) + " " + str(20 + self.theta1) + " " + str(
                    self.theta2) + " " + "0 " + str(pincer_value)

            #angles.strip()
            print(angles)
            self.serialConnection.write(angles)
            time.sleep(FRAME_PAUSE)
Exemple #3
0
    def on_frame(self, controller):

        count = 50

        position = Leap.Vector()
        direction = Leap.Vector()
        velocity = Leap.Vector()

        for i in range(0, count - 1):
            hand = controller.frame(i).hands
            if hand[0].is_valid:
                position = position + hand[0].palm_position
                direction = direction + hand[0].direction
                velocity = velocity + hand[0].palm_velocity

        confidence = controller.frame().hands[0].confidence
        grab = controller.frame().hands[0].grab_strength

        position = position / count
        direction = direction / count
        velocity = velocity / count

        mutex.acquire()
        try:
            leap_context["velocity"] = velocity
            leap_context["position"] = position
            leap_context["direction"] = direction
            leap_context["box"] = controller.frame().interaction_box
            leap_context["confidence"] = confidence
            leap_context["grab"] = grab
        finally:
            mutex.release()
Exemple #4
0
 def __init__(self, scale=1. / 4 / 10., offset=b2Vec2(6.0, 0.)):
     self.__thumbPos = Leap.Vector(0., 0., 0.)
     self.__indexPos = Leap.Vector(0., 0., 0.)
     self.__time = 0.
     self.controller = Leap.Controller()
     self.scale = scale
     self.offset = offset
     super(Leaper, self).__init__()
Exemple #5
0
    def onFrame(self, controller):
        # Get the most recent frame and report some basic information
        frame = controller.frame()
        hands = frame.hands()
        numHands = len(hands)
        print "Frame id: %d, timestamp: %d, hands: %d" % (
            frame.id(), frame.timestamp(), numHands)

        if numHands >= 1:
            # Get the first hand
            hand = hands[0]

            # Check if the hand has any fingers
            fingers = hand.fingers()
            numFingers = len(fingers)
            if numFingers >= 1:
                # Calculate the hand's average finger tip position
                pos = Leap.Vector(0, 0, 0)
                for finger in fingers:
                    tip = finger.tip()
                    pos.x += tip.position.x
                    pos.y += tip.position.y
                    pos.z += tip.position.z
                pos = Leap.Vector(pos.x / numFingers, pos.y / numFingers,
                                  pos.z / numFingers)
                print "Hand has %d fingers with average tip position (%f, %f, %f)" % (
                    numFingers, pos.x, pos.y, pos.z)

            # Check if the hand has a palm
            palmRay = hand.palm()
            if palmRay is not None:
                # Get the palm position and wrist direction
                palm = palmRay.position
                wrist = palmRay.direction
                print "Palm position (%f, %f, %f)" % (palm.x, palm.y, palm.z)

                # Check if the hand has a normal vector
                normal = hand.normal()
                if normal is not None:
                    # Calculate the hand's pitch, roll, and yaw angles
                    pitchAngle = math.atan2(normal.z,
                                            normal.y) * 180 / math.pi + 180
                    rollAngle = math.atan2(normal.x,
                                           normal.y) * 180 / math.pi + 180
                    yawAngle = math.atan2(wrist.z,
                                          wrist.x) * 180 / math.pi - 90
                    # Ensure the angles are between -180 and +180 degrees
                    if pitchAngle > 180: pitchAngle -= 360
                    if rollAngle > 180: rollAngle -= 360
                    if yawAngle > 180: yawAngle -= 360
                    print "Pitch: %f degrees,  roll: %f degrees,  yaw: %f degrees" % (
                        pitchAngle, rollAngle, yawAngle)

            # Check if the hand has a ball
            ball = hand.ball()
            if ball is not None:
                print "Hand curvature radius: %f mm" % ball.radius
def take_input():
    print "Taking input."

    controller = Leap.Controller()
    count = 0
    last_frame = 0

    while last_frame < 1:
        string = ''
        frame = controller.frame()
        if frame.id != last_frame:
            last_frame = frame.id
            fingers_1 = []
            fingers_2 = []
            for index_type in range(0,
                                    1):  #Sort the fingers from thumb to pinky.
                fingers_1.append(frame.hands.leftmost)
                fingers_2.append(frame.hands.rightmost)
                fingers_1.append(frame.hands.leftmost.palm_position)
                fingers_2.append(frame.hands.rightmost.palm_position)
            hand_right = fingers_2[0]
            hand_left = fingers_1[0]

            for finger_right in hand_right.fingers:
                bone_right = finger_right.bone(3)
                distance_right = fingers_2[1].distance_to(
                    bone_right.next_joint)
                string += (str(distance_right) + ', ')
                sudut = Leap.Vector(finger_right.tip_position - fingers_2[1])
                b = str(sudut).replace('(', '')
                c = b.replace(')', '')
                string += (str(c) + ', ')

            for finger_left in hand_left.fingers:
                bone_left = finger_left.bone(3)
                distance_left = fingers_1[1].distance_to(bone_left.next_joint)
                string += (str(distance_left) + ', ')
                sudut = Leap.Vector(finger_left.tip_position - fingers_1[1])
                b = str(sudut).replace('(', '')
                c = b.replace(')', '')
                if count < 4:
                    string += (c + ', ')
                else:
                    string += (c)
                count += 1
            print(string)
            string_list = string.split(',')
            print(string_list)
            last_frame += 1
            results = map(float, string_list)
            dicari = np.array(results)
            baru = np.reshape(dicari, (-1, 1))
            baru_lagi = np.reshape(dicari, (1, -1))
            hasil_knn = knn.predict(baru_lagi)
            print(hasil_knn)
Exemple #7
0
def mom_grandma_dad_grandpa(frame):
    global lastWord
    for hand in frame.hands:
        if len(frame.hands) == 1:
            if hand.sphere_radius > 80:
                handChirality = 1 if hand.is_right else -1
                if hand.palm_normal.angle_to(Leap.Vector(-1,0,0)) < Leap.PI/6 if handChirality==1 else hand.palm_normal.angle_to(Leap.Vector(1,0,0)) < Leap.PI/6:
                    if Leap.PI/6 < hand.direction.angle_to(Leap.Vector(0,0,-1)) < 3*Leap.PI/8:
                        """
                        for gesture in frame.gestures():
                            if gesture.type == Leap.Gesture.TYPE_CIRCLE:
                                circle = CircleGesture(gesture)
                                if (circle.pointable.direction.angle_to(
                                        circle.normal) <= Leap.PI / 2) if handChirality == -1 else not (
                                    circle.pointable.direction.angle_to(circle.normal) <= Leap.PI / 2):  # clockwise
                                    if circle.state != Leap.Gesture.STATE_START:
                                        if circle.progress >= 1.25:
                                            if lastWord != "grandma":
                                                time.sleep(0.5)
                                                print "grandma"
                                                player('grandma')
                                                lastWord = "grandma"
                                                return True
                        """
                        if lastWord != "mom":
                            time.sleep(0.5)
                            print "mom"
                            player('mom')
                            lastWord = "mom"
                            time.sleep(0.25)
                            return True
                    if 5*Leap.PI/12 < hand.direction.angle_to(Leap.Vector(0,0,-1)) < 2*Leap.PI/3:
                        """
                        for gesture in frame.gestures():
                            if gesture.type == Leap.Gesture.TYPE_CIRCLE:
                                circle = CircleGesture(gesture)
                                if (circle.pointable.direction.angle_to(
                                        circle.normal) <= Leap.PI / 2) if handChirality == -1 else not (
                                    circle.pointable.direction.angle_to(circle.normal) <= Leap.PI / 2):  # clockwise
                                    if circle.state != Leap.Gesture.STATE_START: 
                                        if circle.progress >= 1.25:
                                            if lastWord != "grandpa":
                                                print "grandpa"
                                                player('grandpa')
                                                lastWord = "grandpa"
                                                return True
                        """
                        if lastWord != "dad":
                            print "dad"
                            player('dad')
                            lastWord = "dad"
                            time.sleep(1)
                            return True
        return False
Exemple #8
0
 def get_all_fingers(self, frame):
     if not frame.hands.is_empty:
         hands_fingers = []
         for hand in frame.hands:
             this_hand = []
             for finger in hand.fingers:
                 finger_pos = Leap.Vector()
                 finger_pos += finger.tip_position
                 this_hand.append((int(finger_pos[0]), int(finger_pos[1]),
                                   int(finger_pos[3])))
                 finger_vel = Leap.Vector()
                 finger_vel += finger.tip_velocity
             hands_fingers.append(this_hand)
     return hands_fingers
Exemple #9
0
 def leap_to_world(self, leap_point, iBox):
     leap_point.z *= -1.0
     #right-hand to left-hand rule
     normalized = iBox.normalize_point(leap_point, False)
     normalized = normalized + Leap.Vector(0.5, 0, 0.5)
     #recenter origin
     return normalized * 100.0
Exemple #10
0
    def on_frame(self, controller):
        # Get the most recent frame
        frame = controller.frame()

        # Need exactly one hand for this gesture
        if len(frame.hands) is not 1:
            del self.history[:]
            return

        hand = frame.hands[0]

        # About five fingers must be visible
        if len(hand.fingers) < 4:
            del self.history[:]
            return

        self.history.append(hand.stabilized_palm_position)

        # Limit history size
        if len(self.history) > self.history_size:
            self.history = self.history[:-self.history_size]

        # Activate the gesture if there's enough change
        variation = Leap.Vector()
        for i in range(1, len(self.history)):
            variation += self.history[i] - self.history[i - 1]
        if variation.magnitude >= self.threshold:
            self.change_color(hand.stabilized_palm_position)
Exemple #11
0
 def leap_notes(self, frame, last_leap_notes):
     leap_notes = {}
     if not frame.hands.is_empty:
         for finger in frame.fingers:
             finger_pos = Leap.Vector()
             finger_pos += finger.tip_position
             finger_vel = Leap.Vector()
             finger_vel += finger.tip_velocity
             leap_notes = self.get_cur_leap_notes(finger, last_leap_notes,
                                                  finger_pos, finger_vel,
                                                  leap_notes)
     missing_notes = self.get_missing_notes(last_leap_notes, leap_notes)
     if len(missing_notes) != 0:
         for note in missing_notes:
             leap_notes[note] = ["off", self.last_y_pos]
     return leap_notes
Exemple #12
0
    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, tools: %d" % (
            frame.id, frame.timestamp, len(frame.hands), len(
                frame.fingers), len(frame.tools))

        if not frame.hands.empty:
            # Get the first hand
            hand = frame.hands[0]

            # Check if the hand has any fingers
            fingers = hand.fingers
            if not fingers.empty:
                # Calculate the hand's average finger tip position
                avg_pos = Leap.Vector()
                for finger in fingers:
                    avg_pos += finger.tip_position
                avg_pos /= len(fingers)
                print "Hand has %d fingers, average finger tip position: %s" % (
                    len(fingers), avg_pos)

            # Get the hand's sphere radius and palm position
            print "Hand sphere radius: %f mm, palm position: %s" % (
                hand.sphere_radius, 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 "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees\n" % (
                direction.pitch * Leap.RAD_TO_DEG,
                normal.roll * Leap.RAD_TO_DEG, direction.yaw * Leap.RAD_TO_DEG)
Exemple #13
0
    def on_frame(self, controller):
        # Get the most recent frame and report some basic information
        frame = controller.frame()
        if self.state == 1 or self.state == 2:  #recording state
            if not frame.hands.is_empty:
                self.noin = 0
                # Get the first hand
                hand = frame.hands[0]

                # Check if the hand has any fingers
                fingers = hand.fingers
                if not fingers.is_empty:
                    # Calculate the hand's average finger tip position
                    if len(fingers) in (1, 2):
                        avg_pos = Leap.Vector()
                        for finger in fingers:
                            avg_pos += finger.tip_position
                        avg_pos /= len(fingers)
                        coords = avg_pos.to_tuple()
                        if coords[2] < 40:
                            self.xcor.append(coords[0])
                            self.ycor.append(coords[1])
                            print "Recorded X: %d, Y: %d" % (coords[0],
                                                             coords[1])

            else:
                self.noin += 1
                if self.noin >= 100:
                    if self.state == 1:
                        self.xsav = self.xcor
                        self.ysav = self.ycor
                        self.xcor = []
                        self.ycor = []
                        print "Recording complete"
                        self.state = 3
                        self.wait = 0
                        self.noin = 0
                    elif self.state == 2:
                        print "Signature complete"
                        diff = self.sigcomp()
                        print int(diff)
                        if diff > 50 or diff == -1:
                            print "You signature was not accepted, try again"
                            self.state = 3
                            self.wait = 0
                            self.noin = 0
                            self.xcor = []
                            self.ycor = []
                        else:
                            print "Signature was accepted"
                            raw_input()
        if self.state == 3:
            self.wait += 1
            w = 5 - int(self.wait / 40)
            if w != self.lastwait:
                print "Signing in %d" % w
                self.lastwait = w
            if self.wait > 200:
                self.state = 2
                self.wait = 0
 def __prototype_thumb_angle(self):
     thumb = self.hand.fingers[Leap.Finger.TYPE_THUMB]
     point_a = thumb.bone(Leap.Bone.TYPE_PROXIMAL).prev_joint
     point_b = thumb.bone(Leap.Bone.TYPE_INTERMEDIATE).prev_joint
     point_c = thumb.bone(Leap.Bone.TYPE_INTERMEDIATE).next_joint
     point_d = thumb.bone(Leap.Bone.TYPE_DISTAL).next_joint
     ab = Leap.Vector(point_b.x - point_a.x, point_b.y - point_a.y,
                      point_b.z - point_a.z)
     cd = Leap.Vector(point_d.x - point_c.x, point_d.y - point_c.y,
                      point_d.z - point_c.z)
     dot_product = ab.x * cd.x + ab.y * cd.y + ab.z * cd.z
     magnitude_ab = math.sqrt(pow(ab.x, 2) + pow(ab.y, 2) + pow(ab.z, 2))
     magnitude_cd = math.sqrt(pow(cd.x, 2) + pow(cd.y, 2) + pow(cd.z, 2))
     cos_angle = dot_product / (magnitude_ab * magnitude_cd)
     angle = math.acos(cos_angle)
     return angle
Exemple #15
0
 def getMsg(self):
     #frameId
     self.LeapFrameMsg.frameID = self.frame.id
     #device vector:x,y,z,yaw,pitch,roll 
     vector = Leap.Vector()
     #cartesian coordinates
     self.LeapFrameMsg.vector.cartesian = vector.to_float_array()
     #angular coordinates
     self.LeapFrameMsg.vector.angular = [vector.pitch, vector.yaw, vector.roll]
     #time stamp
     self.LeapFrameMsg.timeStamp = self.frame.timestamp
     
     #hands
     for hand in self.frame.hands:
         self.LeapFrameMsg.hands.append(self.getHand(hand))
     
     #fingers
     for finger in self.frame.fingers:
         self.LeapFrameMsg.fingers.append(self.getFinger(finger))
             
     #pointables
     for pointable in self.frame.pointables:
         self.LeapFrameMsg.pointables.append(self.getPointable(pointable))
                 
     return self.LeapFrameMsg
Exemple #16
0
 def get_fingertip_list(self, frame):
     finger_list = []
     for finger in frame.fingers:
         finger_pos = Leap.Vector()
         finger_pos += finger.tip_position
         finger_list.append((int(finger_pos[0]), int(finger_pos[1])))
     return sorted(finger_list)
Exemple #17
0
def generateFingerData(hand, handCnt, numHands):

    fingers = hand.fingers()
    numFingers = len(fingers)
    fingerCnt = 0

    if numFingers >= 1:
        fingerData = '"fingers":[ '
        pos = Leap.Vector(0, 0, 0)
        dir = Leap.Vector(0, 0, 0)

        for finger in fingers:
            tip = finger.tip()
            id = finger.id()
            width = finger.width()
            pos.x = tip.position.x
            pos.y = tip.position.y
            pos.z = tip.position.z
            velocity = finger.velocity()

            fingerData += '{ "id":' + str(id) + ','
            fingerData += '"isTool": "' + str(finger.isTool()) + '",'
            fingerData += '"length":' + str(finger.length()) + ','
            fingerData += '"tip": {'
            fingerData += '"direction":{'
            fingerData += '"x":' + str(tip.direction.x) + ','
            fingerData += '"y":' + str(tip.direction.y) + ','
            fingerData += '"z":' + str(tip.direction.z)
            fingerData += '}, "position":{'
            fingerData += '"x":' + str(tip.position.x) + ','
            fingerData += '"y":' + str(tip.position.y) + ','
            fingerData += '"z":' + str(tip.position.z) + '} '
            fingerData += '}, "velocity":{'
            fingerData += '"x":' + str(velocity.x) + ','
            fingerData += '"y":' + str(velocity.y) + ','
            fingerData += '"z":' + str(velocity.z) + '},'
            fingerData += '"width":' + str(width) + '}'

            fingerCnt += 1
            if (fingerCnt != numFingers):
                fingerData += ','
        fingerData += '],'

    else:
        fingerData = '"fingers": null,'

    return fingerData
    def modal(self, context, event):
        if event.type == 'ESC':
            return self.cancel(context)

        if event.type == 'TIMER':
            controller = self.controller
            frame = controller.frame()

            if self.handID is not None:
                # Trying to track hand.
                hand = frame.hand(self.handID)
                if hand.is_valid:
                    # Hand tracking successfull. Checking fingers.
                    if self.isGesture(hand):
                        # Finger tracking succeeded. Update object.
                        self.updateObject(frame, context)
                    else:
                        # Finger tracking failed. Assuming end of gesture.
                        # Reseting state
                        self.handID = None
                        self.startPosition = None
                        self.startOrientierung = None
                        self.ob = None
                        self.startLocation = None
                        self.startRotation = None
                else:
                    # Hand tracking failed. Searching for gesture.
                    for hand in frame.hands:
                        if self.isGesture(hand):
                            # Replacement hand found. Updating state and object.
                            self.handID = hand.id

                            # Update object
                            self.updateObject(frame, context)

                            break
                    # No replacement hand/gesture found. Assuming hand moved out of sight. Resetting state and object.
                    # TODO
            else:
                # No hand is being tracked. Searching for gesture.
                for hand in frame.hands:
                    if self.isGesture(hand):
                        # Gesture found. Memorizing starting position and orientation.
                        print("New gesture found.")
                        self.handID = hand.id
                        self.startPosition = hand.palm_position
                        orientation = Leap.Vector()
                        orientation.pitch = hand.direction.pitch
                        orientation.yaw = hand.direction.yaw
                        orientation.roll = hand.palm_normal.roll
                        self.startOrientierung = orientation
                        self.ob = bpy.context.object
                        self.ob.rotation_mode = 'QUATERNION'
                        self.startLocation = self.ob.location.copy()
                        self.startRotation = self.ob.rotation_quaternion.copy()

                        break

        return {'PASS_THROUGH'}
Exemple #19
0
    def __init__(self, threshold=35, history_size=10):
        Leap.Listener.__init__(self)

        self.hand_origin = Leap.Vector()
        self.history = []

        self.history_size = history_size
        self.threshold = threshold
 def get_fingers_avg_pos(self, hand):
     fingers = hand.fingers
     if not fingers.is_empty and len(fingers) != -1:
         avg_pos = Leap.Vector()
         for finger in fingers:
             avg_pos += finger.stabilized_tip_position
         avg_pos /= len(fingers)
         return avg_pos
Exemple #21
0
def hand_rotator():
    previous_frame = None
    while True:
        hand = (yield)
        if previous_frame and hand and hand.rotation_probability(
                previous_frame) > 0.1:
            view = list(cmd.get_view())
            matrix = hand.rotation_matrix(previous_frame)
            matrix *= Leap.Matrix(Leap.Vector(*view[0:3]),
                                  Leap.Vector(*view[3:6]),
                                  Leap.Vector(*view[6:9]))
            view[:9] = matrix.to_array_3x3()
            cmd.set_view(view)
        if hand:
            previous_frame = hand.frame
        else:
            previous_frame = None
Exemple #22
0
 def on_init(self, controller):
     print "Initialized"
     self.serialConnection = None
     self.previous_angles = []
     self.previous_wrist_position = Leap.Vector(0, 0, 0)
     self.theta1 = 0
     self.theta2 = 0
     self.theta3 = 0
Exemple #23
0
    def onFrame(self, controller):
        # Get the most recent frame and report some basic information
        frame = controller.frame()

        hands = frame.hands()
        numHands = len(hands)
        print "Frame id: %d, timestamp: %d, hands: %d" % (
            frame.id(), frame.timestamp(), numHands)

        if numHands >= 1:
            # Get the first hand
            hand = hands[0]

            # Check if the hand has any fingers
            fingers = hand.fingers()
            numFingers = len(fingers)
            if numFingers >= 1:
                # Calculate the hand's average finger tip position
                pos = Leap.Vector(0, 0, 0)
                for finger in fingers:
                    tip = finger.tip()
                    pos.x += tip.position.x
                    pos.y += tip.position.y
                    pos.z += tip.position.z
                pos = Leap.Vector(pos.x / numFingers, pos.y / numFingers,
                                  pos.z / numFingers)
                msg = "Hand has %d fingers with average tip position (%f, %f, %f)" % (
                    numFingers, pos.x, pos.y, pos.z)
                print msg

                self.zmqSocket.send_json({"message": msg})

            # Check if the hand has a palm
            palmRay = hand.palm()
            if palmRay is not None:
                # Get the palm position and wrist direction
                palm = palmRay.position
                wrist = palmRay.direction
                direction = ""
                if wrist.x > 0:
                    direction = "left"
                else:
                    direction = "right"
                print "Hand is pointing to the %s with palm position (%f, %f, %f)" % (
                    direction, palm.x, palm.y, palm.z)
Exemple #24
0
 def get_current(self, frame):
     hand = frame.hands[0]
     fingers = hand.fingers
     fingerList = []
     for finger in fingers:
         finger_pos = Leap.Vector()
         finger_pos += finger.tip_position
         fingerList.append((int(finger_pos[0]), int(finger_pos[1])))
     return fingerList[0][1]
Exemple #25
0
def main():
    controller = Leap.Controller()
    controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP)
    mainMonitor = CGDisplayBounds(CGMainDisplayID())
    #	err, ids, count = CGGetOnlineDisplayList(10,None,None)
    #	WIDTH = 0
    #	HEIGHT = 0
    #	for id in ids:
    #		monitor = CGDisplayBounds(id)
    #		WIDTH += monitor.size.width
    #		HEIGHT += monitor.size.height

    FPS = 50
    FARLEFT = -300
    FARRIGHT = 300
    TOP = 500
    BOTTOM = 100
    BACKGROUND = -25
    SCREENWIDTH = mainMonitor.size.width
    SCREENHEIGHT = mainMonitor.size.height

    last_time = time.time()
    touched = False

    while True:
        new_time = time.time()
        sleep_time = ((1000.0 / FPS) - (new_time - last_time)) / 1000.0
        if sleep_time > 0:
            time.sleep(sleep_time)
        last_time = new_time
        frame = controller.frame()
        if not frame.hands.is_empty:
            hand = frame.hands[0]
            fingers = hand.fingers
            if not fingers.is_empty:
                num_fingers = 0
                avg_pos = Leap.Vector()
                for finger in fingers:
                    num_fingers += 1
                    avg_pos += finger.tip_position
                avg_pos /= len(fingers)
                posx = (SCREENWIDTH) / (FARRIGHT - FARLEFT) * (avg_pos[0] -
                                                               FARLEFT)
                posy = (SCREENHEIGHT) / (BOTTOM - TOP) * (avg_pos[1] - TOP)
                # Limit to screen
                posx = 0 if posx < 0 else posx
                posx = SCREENWIDTH if posx > SCREENWIDTH else posx
                posy = 0 if posy < 0 else posy
                posy = SCREENHEIGHT if posy > SCREENHEIGHT else posy
                if num_fingers == 1:
                    mousemove(posx, posy)
                    if avg_pos[2] < BACKGROUND:
                        if touched == False:
                            mouseclick(posx, posy)
                            touched = True
                    else:
                        touched = False
Exemple #26
0
def take_input(label):
    print "Taking input."
    file_dataset = open('angka_sudut_panjang.txt','a')
    file_temp = open('temp_dataset.txt','a')

    file_temp.write(str(label) + " ")
    controller = Leap.Controller()
    count = 0
    last_frame = 0
    while  count < 20:
        frame = controller.frame()
        if frame.id != last_frame:
            print ".",
            last_frame = frame.id
            fingers_1 = []
            fingers_2 = []
            for index_type in range(0,1): #Sort the fingers from thumb to pinky.
                fingers_1.append(frame.hands.leftmost)
                fingers_2.append(frame.hands.rightmost)
                fingers_1.append(frame.hands.leftmost.palm_position)
                fingers_2.append(frame.hands.rightmost.palm_position)
            hand_right = fingers_2[0]
            hand_left = fingers_1[0]
            file_dataset.write(str(label))
            for finger_right in hand_right.fingers:
                bone_right = finger_right.bone(3)
                distance_right = fingers_2[1].distance_to(bone_right.next_joint)
                file_dataset.write(', '+ str(distance_right))
                sudut = Leap.Vector(finger_right.tip_position - fingers_2[1])
                b = str(sudut).replace('(', '')
                c = b.replace(')', '')
                file_dataset.write(', ' + c)

            for finger_left in hand_left.fingers:
                bone_left = finger_left.bone(3)
                distance_left = fingers_1[1].distance_to(bone_left.next_joint)
                file_dataset.write(', '+ str(distance_left))
                sudut = Leap.Vector(finger_left.tip_position - fingers_1[1])
                b = str(sudut).replace('(', '')
                c = b.replace(')', '')
                file_dataset.write(', ' + c)

            file_dataset.write("\n")
            count +=1
Exemple #27
0
 def get_pressed_keys(self, frame):
     indexes_and_heights = []
     if not frame.hands.is_empty:
         for finger in frame.fingers:
             finger_pos = Leap.Vector()
             finger_pos += finger.tip_position
             index = self.get_note_index(finger_pos[0])
             y_pos = finger_pos[1]
             indexes_and_heights.append((index, y_pos))
     return indexes_and_heights
Exemple #28
0
    def on_frame(self, controller):
        if (self.count == self.AVG):
            valid_fingers = 0
            average = Leap.Vector()

            for i in range(0, self.AVG - 1):
                index_finger = controller.frame(i).fingers.finger_type(
                    Finger.TYPE_INDEX)[0]
                #                index_finger = indexFingerList[0]

                pinky_finger = controller.frame(i).fingers.finger_type(
                    Finger.TYPE_PINKY)[0]
                #pinky_finger = pinky_fingerList[0]

                if (pinky_finger.is_valid and pinky_finger.direction.z < 0):
                    if (time.time() - self.last_click > 1):
                        print "last %d", self.last_click
                        print "this %d", time.time()
                        self.action.click(index_finger)
                        self.clicked = True
                        self.last_click = time.time()

                if (not pinky_finger.is_valid
                        or not pinky_finger.direction.z < 0):
                    if (self.clicked == True):
                        self.clicked = False
                        self.action.unclick(index_finger)

                if (index_finger.is_valid and index_finger.direction.z < 0):
                    average += index_finger.tip_position
                    valid_fingers += 1

                if (self.use_vertscroll):
                    prev_finger = controller.frame(i + 1).fingers.finger_type(
                        Finger.TYPE_INDEX)[0]
                    if (prev_finger.is_valid and index_finger.is_valid):
                        diff = prev_finger.tip_position.y - index_finger.tip_position.y
                        # print diff

                        if (math.fabs(diff) > self.SCROLL_THRESHOLD):
                            self.action.scroll(math.copysign(1, -diff))

                elif index_finger.is_valid and index_finger.type == Leap.Finger.TYPE_INDEX:
                    roll = finger.direction.roll + self.roll_offset
                    if math.fabs(roll) > self.vert_scroll_limit:
                        self.action.scroll(self.vert_scroll_limit - roll)
                        print roll

            average /= valid_fingers
            if not valid_fingers == 0:
                self.action.mouse(average, index_finger)
            count = 0

        else:
            self.count += 1
Exemple #29
0
def love(frame):
    global lastWord
    for hand in frame.hands:
        angle_to_index = 0.0
        angle_to_middle = 0.0
        angle_to_ring = 0.0
        angle_to_pinky = 0.0
        projection_on_direction_thumb = 0.0

        for finger in hand.fingers:

            if finger.type == TYPE_THUMB:
                for i in xrange(4):
                    bone_i = finger.bone(i)
                    if bone_i.type == TYPE_PROXIMAL:
                        projection_on_direction_thumb = bone_i.direction.dot(hand.direction) / hand.direction.magnitude

            elif finger.type == TYPE_INDEX:
                for i in xrange(4):
                    bone = finger.bone(i)
                    if bone.type == TYPE_DISTAL:
                        angle_to_index = bone.direction.angle_to(hand.direction)

            elif finger.type == TYPE_MIDDLE:
                for i in xrange(4):
                    bone = finger.bone(i)
                    if bone.type == TYPE_DISTAL:
                        angle_to_middle = bone.direction.angle_to(hand.direction)

            elif finger.type == TYPE_RING:
                for i in xrange(4):
                    bone = finger.bone(i)
                    if bone.type == TYPE_DISTAL:
                        angle_to_ring = bone.direction.angle_to(hand.direction)
            else:
                for i in xrange(4):
                    bone = finger.bone(i)
                    if bone.type == TYPE_DISTAL:
                        angle_to_pinky = bone.direction.angle_to(hand.direction)

        index_up = False if angle_to_index < 1 else True
        middle_up = False if angle_to_middle < 1 else True
        ring_up = False if angle_to_ring < 1 else True
        pinky_up = False if angle_to_pinky < 1 else True
        thumb_up = False if projection_on_direction_thumb < -.75 else True

        if index_up and not middle_up and not ring_up and pinky_up and thumb_up:
            if abs(hand.palm_normal.angle_to(Leap.Vector(0,-1,0))) * Leap.RAD_TO_DEG <= 30:
                if lastWord != 'love':
                    print 'I love you'
                    lastWord = 'love'
                    player('ily')
                    return True
    return False
 def on_frame(self, controller):
     frame = controller.frame()
     interactionBox = frame.interaction_box
     if (frame.pointables.is_empty == True):
         self.position = Leap.Vector(0, 0, 0)
     elif (frame.pointables.is_empty == False):
         for pointable in frame.pointables:
             pointable = frame.pointables.frontmost
             self.position = pointable.tip_position
     print(self.position)
     return self.position