def read_frame(filename): Leap.Controller() new_frame = Leap.Frame() with open(os.path.realpath(filename), 'rb') as data_file: data = data_file.read() leap_byte_array = Leap.byte_array(len(data)) address = leap_byte_array.cast().__long__() ctypes.memmove(address, data, len(data)) new_frame.deserialize((leap_byte_array, len(data))) return new_frame
def leap_frames(filename): """List generator of `Leap.Frame` from raw LeapMotion binary file.""" 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 = Leap.byte_array(frame_size) frame_data_ptr = frame_data.cast().__long__() ctypes.memmove(frame_data_ptr, file_.read(frame_size), frame_size) frame = Leap.Frame() frame.deserialize((frame_data, frame_size)) frame_size_data = file_.read(4) yield frame
def get_frame(filename): frame = Leap.Frame() # filename = 'cali_1495553181022000.data' with open(os.path.realpath(filename), 'rb') as data_file: data = data_file.read() leap_byte_array = Leap.byte_array(len(data)) address = leap_byte_array.cast().__long__() ctypes.memmove(address, data, len(data)) frame.deserialize((leap_byte_array, len(data))) return frame
def read_file(f, l): #hand = [] fd = open(str(f) + "/" + l + ".data", "rb") next_block_size = fd.read(4) size = struct.unpack('i', next_block_size)[0] data = fd.read(size) leap_byte_array = Leap.byte_array(size) address = leap_byte_array.cast().__long__() ctypes.memmove(address, data, size) frame = Leap.Frame() frame.deserialize((leap_byte_array, size)) #hand.append(frame.hands.rightmost) #return hand return frame.hands.rightmost
def load_raw_data(self, file_name): classified_sequence_list = [] with open("../data/raw/" + file_name + ".data", "rb") as data_file: for j in range(0, 16): frame_list = [] for i in range(0, 60): next_block_size = data_file.read(4) size = struct.unpack('i', next_block_size)[0] data = data_file.read(size) leap_byte_array = Leap.byte_array(size) address = leap_byte_array.cast().__long__() ctypes.memmove(address, data, size) frame = Leap.Frame() frame.deserialize((leap_byte_array, size)) frame_list.append(frame) code = struct.unpack('i', data_file.read(4))[0] classified_sequence_list.append( m.ClassifiedSequence(m.Sequence().load_data(frame_list), m.Gesture.gesture_from_code(code))) return classified_sequence_list
def deserialize_frame(self, serialized_stream): frames = [] controller = Leap.Controller() with open(serialized_stream, "rb") as frame_file: next_block_size = frame_file.read(4) while next_block_size: size = struct.unpack("i", next_block_size)[0] data = frame_file.read(size) leap_byte_array = Leap.byte_array(size) address = leap_byte_array.cast().__long__() ctypes.memmove(address, data, size) frame = Leap.Frame() frame.deserialize((leap_byte_array, size)) frames.append(frame) next_block_size = frame_file.read(4) return frames
class LeapController(bpy.types.Operator): bl_idname = "object.leap_blender" bl_label = "Leap Blender" bl_options = {'REGISTER', 'UNDO'} bl_property = "distance" activated = False frame = Leap.Frame() key_tap_timestamp = 0 #GUI Check Box #activated = BoolProperty( # name="Leap Controlled", # description="Enables controlling Object through Leap Motion", # default=False, # options={'ANIMATABLE'}, # subtype='NONE') distance = FloatProperty(name="distance", default=1.0, subtype='DISTANCE', unit='LENGTH', description="distance") @classmethod def poll(cls, context): return True def modal(self, context, event): """ Listen for TIMER event and look for leap frames on every tick. """ # Cancel LeapMotion control on pressing END key. if event.type == 'END': self.cancel(context) return if event.type == 'TIMER' and not self._updating: self._updating = True # Handle all new frames since last tick. last_frame = self.frame self.frame = self.controller.frame() #print (self.frame.id, last_frame.id) if ((self.frame is not last_frame) and (self.frame.id > 0 and last_frame.id > 0)): frames = [] for i in range(0, self.frame.id - last_frame.id + 1): if self.controller.frame(i).is_valid: frames.append(self.controller.frame(i)) self._process_frames(frames) self._updating = False return {'PASS_THROUGH'} def execute(self, context): dir = self.direction.normalized() context.active_object.location += self.distance * dir return {'FINISHED'} 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 _process_frames(self, frames): """ Evaluate movements and moations in frame. """ for g in frames[-1].gestures(frames[0]): if (g.type == Leap.Gesture.TYPE_KEY_TAP # See settings for explanation. and time.time() > (self.key_tap_timestamp + settings.key_tap_seconds_lock)): self.activated = not self.activated self.key_tap_timestamp = time.time() if self.activated: # Same control movements for workspace and blender objects, # except use one hand for controlling workspace and two for blender objects. obj = None if len(frames[0].hands) > 1: obj1 = blender_object.BlenderObject() obj2 = workspace.Workspace() else: obj1 = workspace.Workspace() obj2 = blender_object.BlenderObject() #print(frames[0].hands.leftmost.palm_position) # Hand with fingers stretched. # Controls rotation and zooming. if len(frames[0].fingers) > 3: v = self._frames_difference_vector( frames, lambda x: x.hands.leftmost.palm_position) # Zoom just applies for workspace. # Not all objects can zoom. print(v) zoom = getattr(obj1, "zoom", None) if zoom is not None: obj1.zoom(v.z * settings.lb_factor) self._rotate(obj1, v) # Fist or hand without fingers. # Controls movements. elif len(frames[0].fingers) < 2 and len(frames[0].hands) > 0: v = self._frames_difference_vector( frames, lambda x: x.hands.leftmost.palm_position) print(v) self._move(obj2, v) #elif len(frames[0].fingers) < 2 and len(frames[0].hands) > 0: # w = blender_object.BlenderObject() # v = self._frames_difference_vector(frames, lambda x: x.hands[0].palm_position) # self._move(o, v) #elif len(frames[0].fingers) == 1: # o = workspace.Workspace() # v = self._frames_difference_vector(frames, lambda x: x.hands[0].palm_position) # self._move(o, v) def _frames_difference_vector(self, frames, attr_func): """ Calculates the difference Vector of attribute on the first and the last frame. """ res = Vector((0.0, 0.0, 0.0)) for i in (0, -1): #get attributes from frames[i] v = attr_func(frames[i]) if i == 0: res.y -= v.y res.x -= v.x res.z -= v.z else: res.y += v.y res.x += v.x res.z += v.z if res.x > settings.max_x and res.x < -settings.max_x: res.y = 0 if res.y > settings.max_y and res.y < -settings.max_y: res.y = 0 if res.z > settings.max_z and res.z < -settings.max_z: res.z = 0 return res def _move(self, obj, v): obj.move_horizontal(v.x * settings.lb_factor * (-1)) obj.move_vertical(v.y * settings.lb_factor * (-1)) obj.move_straightforward(v.z * settings.lb_factor * (-1)) def _rotate(self, obj, v): obj.rotate_horizontal(v.y * settings.ld_factor * (-1)) obj.rotate_vertical(v.x * settings.ld_factor)
def main(): #create socket serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #get local machine name host = socket.gethostname() port = 9999 #bind the port serversocket.bind((host, port)) #queue up requests serversocket.listen(5) #establish connection clientsocket, addr = serversocket.accept() #open motion builder '''try: subprocess.Popen('C:\\Program Files\\Autodesk\\MotionBuilder 2016\\bin\\x64\\motionbuilder.exe') except: print 'Motion Builder Failed To Open' else: print 'Motion Builder Opened Successfully''' #create csv dir = os.path.dirname(os.path.realpath(__file__)) csv = open(str(dir.replace("\\", "/")) + "/animDataLocal.csv", 'w+b') listener = SampleListener() controller = Leap.Controller() LFrame = Leap.Frame() controller.add_listener(listener) connected = listener.on_connect(controller) #recieve returns oldFrame, leftWristPosition, leftWristRotation, rightWristPosition, rightWristRotation, IndexLeftData, ThumbLeftData, MiddleLeftData, RingLeftData, PinkyLeftData, IndexRightData, ThumbRightData, MiddleRightData, RingRightData, PinkyRightData = listener.on_frame( controller) #set some variables for counting and checking frameCount = 0 testArray = [0, 0, 0] while True: if not msvcrt.kbhit(): if oldFrame != listener.on_frame(controller)[0]: if (str(leftWristPosition) != '0'): #if we have legit values continue if (leftWristPosition[0] == 0) and (leftWristPosition[1] == 0) and (leftWristPosition[2] == 0): #write 0's if we cant find wrist csv.write(('"leftWrist"') + "," + str(frameCount) + "," + "0" + "," + "0" + "," + "0" + "," + "0" + "," + "0" + "," + "0" + "\n") else: #write wrist location and rotations and bone rotations leftWristPosition = re.sub('[()]', '', str(leftWristPosition)) leftWristPosition = re.sub(r'\s+', '', str(leftWristPosition)) csv.write(('"leftWrist"') + "," + str(frameCount) + "," + str(leftWristPosition) + "," + str(360 - leftWristRotation[1]) + "," + str(leftWristRotation[0]) + "," + str(leftWristRotation[2]) + "," + "\n") for i in range(0, 4): csv.write(('"Left Thumb "') + str(ThumbLeftData[i][0]) + ":" + "," + str(frameCount) + "," + str(ThumbLeftData[i][1][0]) + "," + str(ThumbLeftData[i][1][1]) + "," + str(ThumbLeftData[i][1][2]) + "\n") for i in range(0, 4): csv.write(('"Left Index "') + str(IndexLeftData[i][0]) + ":" + "," + str(frameCount) + "," + str(IndexLeftData[i][1][0]) + "," + str(IndexLeftData[i][1][1]) + "," + str(IndexLeftData[i][1][2]) + "\n") for i in range(0, 4): csv.write(('"Left Middle "') + str(MiddleLeftData[i][0]) + ":" + "," + str(frameCount) + "," + str(MiddleLeftData[i][1][0]) + "," + str(MiddleLeftData[i][1][1]) + "," + str(MiddleLeftData[i][1][2]) + "\n") for i in range(0, 4): csv.write(('"Left Ring "') + str(RingLeftData[i][0]) + ":" + "," + str(frameCount) + "," + str(RingLeftData[i][1][0]) + "," + str(RingLeftData[i][1][1]) + "," + str(RingLeftData[i][1][2]) + "\n") for i in range(0, 4): csv.write(('"Left Pinky "') + str(PinkyLeftData[i][0]) + ":" + "," + str(frameCount) + "," + str(PinkyLeftData[i][1][0]) + "," + str(PinkyLeftData[i][1][1]) + "," + str(PinkyLeftData[i][1][2]) + "\n") if (str(rightWristPosition) != '0'): #if we have legit values continue if (rightWristPosition[0] == 0) and (rightWristPosition[1] == 0) and (rightWristPosition[2] == 0): csv.write(('"rightWrist"') + "," + str(frameCount) + "," + "0" + "," + "0" + "," + "0" + "," + "0" + "," + "0" + "," + "0" + "\n") else: #write wrist location and rotations and bone rotations rightWristPosition = re.sub('[()]', '', str(rightWristPosition)) rightWristPosition = re.sub(r'\s+', '', str(rightWristPosition)) csv.write(('"rightWrist"') + "," + str(frameCount) + "," + str(rightWristPosition) + "," + str(360 - rightWristRotation[1]) + "," + str(rightWristRotation[0]) + "," + str(rightWristRotation[2]) + "," + "\n") for i in range(0, 4): csv.write(('"Right Thumb "') + str(ThumbRightData[i][0]) + ":" + "," + str(frameCount) + "," + str(ThumbRightData[i][1][0]) + "," + str(ThumbRightData[i][1][1]) + "," + str(ThumbRightData[i][1][2]) + "\n") for i in range(0, 4): csv.write(('"Right Index "') + str(IndexRightData[i][0]) + ":" + "," + str(frameCount) + "," + str(IndexRightData[i][1][0]) + "," + str(IndexRightData[i][1][1]) + "," + str(IndexRightData[i][1][2]) + "\n") for i in range(0, 4): csv.write(('"Right Middle "') + str(MiddleRightData[i][0]) + ":" + "," + str(frameCount) + "," + str(MiddleRightData[i][1][0]) + "," + str(MiddleRightData[i][1][1]) + "," + str(MiddleRightData[i][1][2]) + "\n") for i in range(0, 4): csv.write(('"Right Ring "') + str(RingRightData[i][0]) + ":" + "," + str(frameCount) + "," + str(RingRightData[i][1][0]) + "," + str(RingRightData[i][1][1]) + "," + str(RingRightData[i][1][2]) + "\n") for i in range(0, 4): csv.write(('"Right Pinky "') + str(PinkyRightData[i][0]) + ":" + "," + str(frameCount) + "," + str(PinkyRightData[i][1][0]) + "," + str(PinkyRightData[i][1][1]) + "," + str(PinkyRightData[i][1][2]) + "\n") oldFrame, leftWristPosition, leftWristRotation, rightWristPosition, rightWristRotation, IndexLeftData, ThumbLeftData, MiddleLeftData, RingLeftData, PinkyLeftData, IndexRightData, ThumbRightData, MiddleRightData, RingRightData, PinkyRightData = listener.on_frame( controller) frameCount += 1 else: break connected = listener.on_disconnect(controller) csv.close()
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