def loadData(filename): f = open(filename, 'r') frames = [] for data in f.readlines(): dataDecoded = base64.b64decode( data ) frame = Leap.Frame() leap_byte_array = Leap.byte_array(len(dataDecoded)) address = leap_byte_array.cast().__long__() ctypes.memmove(address, dataDecoded, len(dataDecoded)) frame.deserialize((leap_byte_array, len(dataDecoded))) frames.append( frame ) return frames
def main(calibration_value): # Create a sample listener and controller listener = SampleListener() controller = Leap.Controller() cntrlr = controller # Have the sample listener receive events from the controller cntrlr.add_listener(listener) # if calibration_value == None: # print "Calibrating in 3.." # time.sleep(1) # print "2.." # time.sleep(1) # print "1.." # time.sleep(1) # calibrate(controller.frame()) # return calibration # sys.exit(0) # else: recording = listener.record(cntrlr) cntrlr.remove_listener(listener) return recording sys.exit(0)
def __init__(self): self.numberOfGestures = 1000 self.gestureIndex = 0 self.gestureData = np.zeros((5, 4, 6, self.numberOfGestures), dtype='f') #3D matrix w/ 5 rows for fingers, (0 is thumb) #4 columns for bones, (3 is distal phalange) #6 stacks for x,y,z coords of bone base & tip #(first 3 are base, last 3 are tip) # self.numberOfGesturesSaved = 0 self.prevNumberOfHands = 0 self.currNumberOfHands = 0 self.controller = Leap.Controller() self.lines = [] print "start..." matplotlib.interactive(True) self.fig = plt.figure(figsize=(8, 6)) self.ax = self.fig.add_subplot(111, projection='3d') self.ax.set_xlim(-300, 300) self.ax.set_ylim(-200, 200) self.ax.set_zlim(0, 400) self.ax.view_init(azim=90) plt.draw()
def main(): global sout port_name = raw_input("Enter Arduino port name: ") sout = serial.Serial(port_name, 9600, timeout=0) print("Initializing...") time.sleep(3) # Create a sample listener and controller listener = SampleListener() controller = Leap.Controller() # Have the sample listener receive events from the controller controller.add_listener(listener) # Keep this process running until Enter is pressed print "Press Enter to quit..." sys.stdin.readline() # Remove the sample listener when done controller.remove_listener(listener) swrite("{r:0,g:0,b:0,m:0}")
def not_moving(): controller = Leap.Controller() while not controller.is_connected: pass # Waiting until hand is moving. fingers = [] while not fingers: # Keep waiting for the fingers to be detected. frame = controller.frame() fingers = frame.fingers pass # Fingers detected. last_frame = 0 while controller.is_connected: frame = controller.frame() if frame.id != last_frame: last_frame = frame.id fingers_1 = [] for index_type in range(0, 5): # Sort the fingers from thumb to pinky. fingers_1.append(frame.fingers.finger_type(index_type)[0]) check(fingers_1) for finger in fingers_1: if finger.tip_velocity.magnitude > 200: return
def main(): # Setup a connection between SC and Py wtih OSC c = OSC.OSCClient() c.connect(('127.0.0.1', 57122)) # Create a sample listener and controller listener = SampleListener() listener.add_osc_connect(c) controller = Leap.Controller() # Have the sample listener receive events from the controller controller.add_listener(listener) # Keep this process running until Enter is pressed print "Press Enter to quit..." try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # Remove the sample listener when done controller.remove_listener(listener)
def main(): # Create a sample listener and controller listener = SampleListener() #listener2 = SampleListener() controller = Leap.Controller() #controller2 = Leap.Controller() # Have the sample listener receive events from the controller controller.add_listener(listener) # controller2.add_listener(listener2) # Keep this process running until Enter is pressed print "Press Enter to quit..." try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # Remove the sample listener when done controller.remove_listener(listener) if (captureData): listener.dumpList()
def __init__(self, master): Frame.__init__(self, master) self.parent = master self.parent.protocol('WM_DELETE_WINDOW', self.close_window) self.parent.title("Gesture Recorder") self.init_dict() self.init_ui() self.set_minimum_confidence(0.75) self.set_n_frames(1) self.set_db_name("test") self.set_collection_name("test1") self.queue = Queue.Queue() self.active_thread = None self.controller = Leap.Controller() self.controller.set_policy(Leap.Controller.POLICY_IMAGES) self.last_oid = None self.last_db_name = None self.last_col_name = None
def main(): global left_hand_fingers global right_hand_fingers global NUM_SAMPLES controller = Leap.Controller() letter = raw_input("Enter label: ") verbose = False filename = 'res/mlData.csv' if verbose: filename = 'res/mlDataVerbose.csv' print "Show letter: " + letter time.sleep(4) #start = raw_input("Wait for Enter to be pressed") with open(filename, 'a') as csvfile: fieldnames = ['L0', 'L1', 'L2', 'L3', 'L4', 'R0', 'R1', 'R2', 'R3', 'R4', 'label'] if verbose: fieldnames = verbose_fieldnames writer = csv.DictWriter(csvfile, fieldnames=fieldnames) data_counter = 0 while data_counter < NUM_SAMPLES: frame = controller.frame() if verbose: dataPoint, status = get_data_all(frame, letter, data_counter) else: dataPoint, status = get_data_angles(frame, letter, data_counter) if(status): writer.writerow(dataPoint) data_counter += 1 print "Completed sampling letter: " + letter
def take_input(label): print "Taking input." file_dataset = open('angka_coba.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) + ',') 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) + ',') file_dataset.write("\n") count += 1
def getFingerTipAttributes(self,origHand,fingerName): ### wuold need abstipPos and relativeTipDirection hand_x_basis = origHand.basis.x_basis hand_y_basis = origHand.basis.y_basis hand_z_basis = origHand.basis.z_basis hand_origin = origHand.palm_position hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis, hand_origin) hand_transform = hand_transform.rigid_inverse() absTipPosition,relativeTipDirection = None,None for finger in origHand.fingers: if fingerName == self.finger_names[finger.type]: fingtip_transformed_position = hand_transform.transform_point(finger.stabilized_tip_position) fingtip_transformed_direction = hand_transform.transform_direction(finger.direction) absTipPosition = finger.stabilized_tip_position #[x,y,z] absTipDirection = finger.direction #[pitch,yaw,roll] relativeTipPosition = fingtip_transformed_position relativeTipDirection = fingtip_transformed_direction relativeTipDirection = np.array(relativeTipDirection)*Leap.RAD_TO_DEG return absTipPosition,relativeTipDirection
def on_frame(self, controller): frame = controller.frame() # Code for transformed data points from Leap Motion developer guide for hand in frame.hands: hand_x_basis = hand.basis.x_basis hand_y_basis = hand.basis.y_basis hand_z_basis = hand.basis.z_basis hand_origin = hand.palm_position hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis, hand_origin) hand_transform = hand_transform.rigid_inverse() fingertips = {} for finger in hand.fingers: fingertipPos = finger.bone(3).next_joint fingertips[self.finger_names[finger.type]] = (fingertipPos.x, fingertipPos.y, fingertipPos.z) transformed_position = hand_transform.transform_point( finger.tip_position) transformed_direction = hand_transform.transform_direction( finger.direction) signData[self.finger_names[finger.type] + " position"] = (transformed_position.x, transformed_position.y, transformed_position.z) signData[self.finger_names[finger.type] + " direction"] = (transformed_direction.x, transformed_direction.y, transformed_direction.z) for i in range(len(fingertips) - 1): signData[self.finger_names[i][0] + self.finger_names[i + 1][0] + " distance"] = [ distance3D(fingertips[self.finger_names[i]], fingertips[self.finger_names[i + 1]]) ]
def main(options, hostname, port): # Now we append Mixin classes to our LeapListener's inheritance # chain based on the options the user specified # def runtime_mixin(class_, mixin): class_.__bases__ = (mixin,) + class_.__bases__ if options.multi_arg: runtime_mixin(RuntimeLeapListener, VectorAsArgsMixin) if not options.dumb: runtime_mixin(RuntimeLeapListener, RealPartTrackerMixin) if not options.unbundled: runtime_mixin(RuntimeLeapListener, BundledMixin) # ok, that was weird. Now instantiate this listener listener = RuntimeLeapListener(hostname=hostname, port=int(port), verbose=options.verbose) controller = Leap.Controller() controller.add_listener(listener) # Keep this process running until Enter is pressed log("Press Enter to quit...") sys.stdin.readline() controller.remove_listener(listener)
def __init__(self): self.controller = Leap.Controller() # Create the controller class self.lines = [] # This is where the coordinates of the fingers will go self.numberOfGestures = 1000 self.gestureIndex = 0 #5-3 self.gestureData = np.zeros( (5, 4, 6, self.numberOfGestures), dtype="f") # Make a 3d matrix for storing gesture info #5-4 self.previousNumberOfHands = 0 self.currentNumberOfHands = 0 matplotlib.interactive(True) # Initialize a 3d plot self.fig = plt.figure(figsize=(12, 8)) # Make a figure self.ax = self.fig.add_subplot(111, projection="3d") # 3d yo. self.ax.set_xlim(-260, 260) self.ax.set_ylim(0, 500) self.ax.set_zlim(0, 500) self.ax.view_init(azim=90)
def main(): # Create a sample listener and controller listener = SampleListener() controller = Leap.Controller() #Connecting Leap Motion print "Connecting. . ." SampleListener.on_connect(listener, controller) # Have the sample listener receive events from the controller controller.add_listener(listener) SampleListener.on_focus_gained(listener, controller) print "Ready to listen. . ." # Keep this process running until Enter is pressed print "Press Enter to quit..." i = 0 while(True): controller.add_listener(listener) SampleListener.on_focus_gained(listener, controller) time.sleep(1000) try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # Remove the sample listener when done controller.remove_listener(listener)
def invoke(self, context, event): """ Invoke the leap device. """ context.window_manager.modal_handler_add(self) self.controller = Leap.Controller() # Enable and improve KeyTab. self.controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP) if (LeapPython.Config_set_float(self.controller.config, "Gesture.KeyTap.MinDownVelocity", settings.KeyTap_MinDownVelocity) and LeapPython.Config_set_float( self.controller.config, "Gesture.KeyTap.HistorySeconds", settings.KeyTap_HistorySeconds) and LeapPython.Config_set_float(self.controller.config, "Gesture.KeyTap.MindDistance", settings.KeyTap_MinDistance)): self.controller.config.save() # Set timer to run every settings.framerate. context.window_manager.event_timer_add(settings.framerate, context.window) self._updating = False return {'RUNNING_MODAL'}
def calibrate(): print "Calibrating hand. Please hold it steady." moving() controller = Leap.Controller() frame = controller.frame() hands = frame.hands fingers = frame.fingers middle_finger = fingers[2] for finger in fingers: if finger.type == 2: middle_finger = finger break last_frame = 0 palm_center = hands[0].palm_position calib = palm_center.distance_to(middle_finger.tip_position) #Normalizing with respect to the distance of the middle finger tip to the center of the palm print "Calibrating done. Please remove your hand." while controller.is_connected: frame = controller.frame() if frame.id != last_frame: last_frame = frame.id hands = frame.hands if not hands: return calib
def on_frame(self, controller): #self.paintCanvas.delete("all") global i, on_prediction if on_prediction: return frame = controller.frame() for gesture in frame.gestures(): if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) self.paintCanvas.delete("all") sleep(2) interactionBox = frame.interaction_box for hand in frame.hands: for finger in hand.fingers: if finger.type == 1: normalizedPosition = interactionBox.normalize_point( finger.tip_position) if hand.is_right: """if(finger.touch_distance > 0 and finger.touch_zone != Leap.Pointable.ZONE_NONE): color = self.rgb_to_hex((0, 255 - 255 * finger.touch_distance, 0)) elif(finger.touch_distance <= 0): color = self.rgb_to_hex((-255 * finger.touch_distance, 0, 0)) else: color = self.rgb_to_hex((0,0,200))""" if (finger.touch_distance <= 0.55 and finger.touch_zone != Leap.Pointable.ZONE_NONE and finger.type != 2): self.draw(normalizedPosition.x * 500, 500 - normalizedPosition.y * 500, 20, 20, "black") else: self.draw(normalizedPosition.x * 500, 500 - normalizedPosition.y * 500, 20, 20, "white")
def main(): controller = Leap.Controller() while 1: if controller.is_connected: frame = controller.frame() if len(frame.hands) > 1: arm1 = frame.hands.leftmost arm2 = frame.hands.rightmost pitch_rads1 = int(arm1.direction.pitch // 0.018) pitch_rads2 = 90 + int(arm2.direction.pitch // 0.018) if pitch_rads1 > 0 and pitch_rads1 < 180: pitch1 = str(pitch_rads1) arm1serial = pitch1 else: arm1serial = "1" if pitch_rads2 > 0 and pitch_rads2 < 180: pitch2 = str(pitch_rads2) arm2serial = arm1serial + " " + pitch2 + "\n" else: arm2serial = arm1serial + " 1\n" else: arm2serial = "1 1\n" ser.write(arm2serial) print arm2serial
def debug(): print("Fox on the run") controller = Leap.Controller() listener = LeapListener() controller.add_listener(listener) #Keep process running till input try: while True: sys.stdin.readline() frame = listener.frame print("frame id:", frame.id) listener.getExtendedFingers(listener.firstHand()) #print("grab_strength:", listener.debugGrab_Strength()) #print("confidence: ",listener.handConfidence()) print("detect fist ? ", listener.detectFist()) print("detect paper ? ", listener.detectPalm()) print("detect scissors ? ", listener.detectScissors()) except KeyboardInterrupt: pass
def setFingerTipAttributes(self, origHand): hand_x_basis = origHand.basis.x_basis hand_y_basis = origHand.basis.y_basis hand_z_basis = origHand.basis.z_basis hand_origin = origHand.palm_position hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis, hand_origin) hand_transform = hand_transform.rigid_inverse() for finger in origHand.fingers: newFinger = LeapFinger3() newFinger.type = finger.type newFinger.typeName = self.finger_names[finger.type] fingtip_transformed_position = hand_transform.transform_point( finger.stabilized_tip_position) fingtip_transformed_direction = hand_transform.transform_direction( finger.direction) newFinger.absTipPosition = CoOrdinates( finger.stabilized_tip_position, 'pos') newFinger.absTipDirection = CoOrdinates(finger.direction, 'dir') newFinger.relativeTipPosition = CoOrdinates( fingtip_transformed_position, 'pos') newFinger.relativeTipDirection = CoOrdinates( fingtip_transformed_direction, 'dir') self.fingers.append(newFinger) return self
def main(): num = input("File number : ") listener = LeapMotionListener() controller = Leap.Controller() controller.add_listener(listener) print("Press enter to quit") try: sys.stdin.readline() except KeyboardInterrupt: pass finally: controller.remove_listener(listener) # if len(T)==90: # controller.remove_listener(listener) # print(T[:90]) # print(T[0][0]) file = open("Dataset/NO/NO_" + str(num) + ".txt", "w") # file=open("Test_Dataset/MORNING/MORNING_"+str(num)+".txt", "w") for i in range(90): file.write(str(T[50 + i]) + "\n")
def main(): # Creating dataset by asking for the gesture first, and # the number of examples we want to add. controller = Leap.Controller() while not controller.is_connected: pass print "Connected." count_ex = 0 # Used to normalize the finger lengths across people # so that dataset can scale well to real world examples. calib = calibrate() print "The calibrated value is: ", calib print "Start training..." while True: # This function detects that your hand is moving. # We need to start taking input when the hand stops moving. moving() # The function where the input is taken. take_input() # Again, one more function to loop while the user changes # the gesture he's performing. not_moving() count_ex = count_ex + 1
def main(): #Initialize positions L_pos = [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)] R_pos = [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)] print "Mapping both hands, printing out left hand positional data" print "Place either index finger immediately above the leap controller to end" # Create a sample listener and controller print "" print "X is right, Y is up, Z is out. Measurements are in mm" print "Fingers are ordered starting from the thumb" controller = Leap.Controller() #print positions every half second looping = True while looping: time.sleep(0.5) R_pos = update(controller, 0) L_pos = update(controller, 1) print L_pos #End if either index finger is above the controller for positions in [L_pos, R_pos]: if (positions[1][0] < 50 and positions[1][0] > -50) and \ (positions[1][1] < 50 and positions[1][1] > 0) and \ (positions[1][2] < 50 and positions[1][2] > -50): looping = False
def main(): # Create a sample listener and controller global filename global ges_type, si #si = subprocess.STARTUPINFO() #si =subprocess.Popen("C:\Program Files (x86)\Leap Motion\Core Services\Visualizer.exe") listener = SampleListener() controller = Leap.Controller() # Have the sample listener receive events from the controller controller.add_listener(listener) # Keep this process running until Enter is pressed print("Press Enter to quit... ") try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # Remove the sample listener when done controller.remove_listener(listener)
import Leap, cv2 from supportFunctions import * from time import sleep import numpy as np controller = Leap.Controller() #Allow access to raw images: controller.set_policy(Leap.Controller.POLICY_IMAGES) sleep(0.1) #getModelParts(frame) for i in range(100): frame = controller.frame() image = frame.images[0] for hand in frame.hands: #For left camera: horizontal_slope = -1 * (hand.fingers[1].tip_position.x + 20) / hand.fingers[1].tip_position.y vertical_slope = hand.fingers[1].tip_position.z / hand.fingers[ 1].tip_position.y ray = [horizontal_slope*image.ray_scale_x + image.ray_offset_x, \ vertical_slope*image.ray_scale_y + image.ray_offset_y] pixel = [ray[0] * 400, ray[1] * 400] # vertical_slope = hand.fingers[1].tip_position.y/(hand.fingers[1].tip_position.x+20) # horizontal_slope = hand.fingers[1].tip_position.z/(hand.fingers[1].tip_position.x+20)
def __init__(self, parent=None): super(self.__class__, self).__init__(parent) self.controller = Leap.Controller() self.controller.set_policy_flags(Leap.Controller.POLICY_IMAGES) self.iBox = None
super(StartEnd, self).__init__() def run(self, ): while True: if not read_in(): break ctrl.add_listener(listener) read_in() ctrl.remove_listener(listener) print "listener removed" # Init as global listener and controller so that thread class can handle listener = SampleListener() ctrl = Leap.Controller() def read_in(): inputs = sys.stdin.readline() if inputs == '\n': return True else: return False def main(): thread_save = ConvertSave() thread_run = StartEnd() thread_save.start()
Left_Enemy_pos_fix = [] Right_Enemy_pos_fix = [] Top_Enemy_pos_change = [] Bottom_Enemy_pos_change = [] Left_Enemy_pos_change = [] Right_Enemy_pos_change = [] Create_Enemies = True Character_rect = None FPS_Clock = pygame.time.Clock() Display_surface = pygame.display.set_mode((Screen_Width, Screen_Height)) listener = Leap.Listener() controller = Leap.Controller() controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE) controller.enable_gesture(Leap.Gesture.TYPE_SWIPE) controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP) controller.add_listener(listener) def main(): pygame.init() pygame.display.set_caption('Game 2') while True:
def on_frame(self, controller): # Get the most recent frame and report some basic information frame = controller.frame() # ParaView parameters normal1 = [ frame.hands[0].palm_normal[x], frame.hands[0].palm_normal[y], frame.hands[0].palm_normal[z] ] # ParaView slice: if only 1 hand and not a fist if len(frame.hands) == 1 and len(frame.hands[0].fingers) > 0: sl.SliceType.Normal = normal1 # ParaView zoom: if 2 hands with each at least 2 fingers elif len( frame.hands) == 2 and len(frame.hands[0].fingers) >= 2 and len( frame.hands[1].fingers) >= 2: cam.Zoom(frame.scale_factor(controller.frame(2))) # ParaView rotate: only attempt if there are 2 hands (at least one must have less than 2 fingers) if len(frame.hands) == 2: # Check for which hand has at least 2 fingers and use that one if len(frame.hands[0].fingers) < 2 and len( frame.hands[1].fingers) > 2: rotHand = frame.hands[1] rollAngle = rotHand.palm_normal.roll * Leap.RAD_TO_DEG azimAngle = rotHand.palm_normal.yaw * Leap.RAD_TO_DEG cam.SetRoll(rollAngle) elif len(frame.hands[1].fingers) < 2 and len( frame.hands[0].fingers) > 2: rotHand = frame.hands[0] rollAngle = rotHand.palm_normal.roll * Leap.RAD_TO_DEG azimAngle = rotHand.palm_normal.yaw * Leap.RAD_TO_DEG cam.SetRoll(rollAngle) #else: #cam.SetRoll(0) Render() 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 not frame.hands.is_empty: # 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 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" % ( direction.pitch * Leap.RAD_TO_DEG, normal.roll * Leap.RAD_TO_DEG, direction.yaw * Leap.RAD_TO_DEG) # Gestures for gesture in frame.gestures(): if gesture.type == Leap.Gesture.TYPE_CIRCLE: circle = CircleGesture(gesture) # Determine clock direction using the angle between the pointable and the circle normal if circle.pointable.direction.angle_to( circle.normal) <= Leap.PI / 4: clockwiseness = "clockwise" else: clockwiseness = "counterclockwise" # Calculate the angle swept since the last frame swept_angle = 0 if circle.state != Leap.Gesture.STATE_START: previous_update = CircleGesture( controller.frame(1).gesture(circle.id)) swept_angle = (circle.progress - previous_update.progress) * 2 * Leap.PI print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % ( gesture.id, self.state_string( gesture.state), circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness) if gesture.type == Leap.Gesture.TYPE_SWIPE: swipe = SwipeGesture(gesture) print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % ( gesture.id, self.state_string(gesture.state), swipe.position, swipe.direction, swipe.speed) if gesture.type == Leap.Gesture.TYPE_KEY_TAP: keytap = KeyTapGesture(gesture) print "Key Tap id: %d, %s, position: %s, direction: %s" % ( gesture.id, self.state_string( gesture.state), keytap.position, keytap.direction) if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP: screentap = ScreenTapGesture(gesture) print "Screen Tap id: %d, %s, position: %s, direction: %s" % ( gesture.id, self.state_string(gesture.state), screentap.position, screentap.direction) if not (frame.hands.is_empty and frame.gestures().is_empty): print ""
def main(): controller = Leap.Controller() controller.set_policy_flags(Leap.Controller.POLICY_BACKGROUND_FRAMES) # Initial Intrinsic Camaera Parameters # the resolution is 1280x720, cm0 = np.float32([[1.31446976e+03, 0.00000000e+00, 1.03816814e+03], [0.00000000e+00, 1.31845592e+03, 5.53934350e+02], [0.00000000e+00, 0.00000000e+00, 1.00000000e+00]]) dist0 = np.float32( [[-0.10849121, 0.05242051, 0.00223423, -0.01146983, -0.04983505]]) cm = None #test points are 5 * 3 #test_pos=[(0.1+(1.0*i/4)*0.8,0.1+(1.0*j/2)*0.8) for i in range(5) for j in range(3)] #test points are 5 * 5 test_pos = [(0.1 + (1.0 * i / 4) * 0.8, 0.1 + (1.0 * j / 4) * 0.8) for i in range(5) for j in range(5)] result = [] screen_pos = [] # opencv related content cv2.startWindowThread() cap = cv2.VideoCapture(2) #ret = cap.set(3,1280); #ret = cap.set(4,720); #ret = cap.set(3,960); #ret = cap.set(4,720); ret = cap.set(3, 1920) ret = cap.set(4, 1080) idx = 0 while (True): ret, img = cap.read() img = cv2.flip(img, 1) H, W = img.shape[:2] keycode = cv2.waitKey(1) & 0xff frame = controller.frame() tip = None for hand1 in frame.hands: index_finger_list = hand1.fingers.finger_type(Finger.TYPE_INDEX) index_finger = index_finger_list[ 0] #since there is only one per hand if keycode == ord('q'): break elif keycode == ord(' '): if tip: screen_pos.append( (int(test_pos[idx][0] * W), int(test_pos[idx][1] * H))) result.append((tip[0], tip[1], tip[2])) idx += 1 if idx >= len(test_pos): idx = 0 cv2.circle(img, (int(test_pos[idx][0] * W), int(test_pos[idx][1] * H)), 10, (0, 0, 255) if tip else (255, 0, 0), -1) cv2.imshow('frame', img) cv2.destroyWindow('frame') cap.release() controller.clear_policy(Leap.Controller.POLICY_BACKGROUND_FRAMES) screen_pos2 = [(W - 1 - x, y) for (x, y) in screen_pos] retval, cm, dist, rvec, tvec = cv2.calibrateCamera(np.float32([result]), \ np.float32([screen_pos]), (W, H), cm0.copy(), dist0.copy(),\ flags=cv2.CALIB_USE_INTRINSIC_GUESS) np.set_printoptions(threshold='nan') np.set_printoptions(suppress=True) print retval, rvec, tvec, cm, dist controller.clear_policy(Leap.Controller.POLICY_BACKGROUND_FRAMES) # Keep this process running until Enter is pressed print "Press Enter to quit..." try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # Remove the sample listener when done #controller.remove_listener(listener) pass
import Leap, struct, sys if len(sys.argv) != 2: print "usage:", sys.argv[0], "<filename>" exit(1) filename = sys.argv[1] controller = Leap.Controller() frame_list = [] with open(filename, "rb") as file_: frame_size_data = file_.read(4) while frame_size_data: frame_size = struct.unpack('i', frame_size_data)[0] frame_data_ptr = Leap.byte_array(frame_size) for i in xrange(frame_size): frame_data_ptr[i] = struct.unpack('B', file_.read(1))[0] frame = Leap.Frame() frame.deserialize((frame_data_ptr,frame_size)) frame_list.append(frame) frame_size_data = file_.read(4) print "Loaded %d frames." % len(frame_list) for frame in frame_list: print frame