def __init__(self): ShowBase.__init__(self) self.bandit = Actor("banditRiggedNoHat.egg") self.bandit.makeSubpart("r_leg", self.R_LEG_JOINTS) self.bandit.makeSubpart("l_leg", self.L_LEG_JOINTS) headJoint = self.bandit.exposeJoint(None, "modelRoot", "joint5") hat = loader.loadModel("{}_hat.egg".format(random.randint(0, 7))) hat.setPos(0, 0, -0.08) hat.setHpr(-90, 0, 0) hat.setScale(0.35) hat.reparentTo(headJoint) self.bandit.reparentTo(render) r_hip_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[0]) r_hip_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[0]) r_knee_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[1]) r_knee_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[1]) r_ankle_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[2]) r_ankle_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[2]) distances = np.array([ r_hip_ro.getDistance(r_knee_ro), r_knee_ro.getDistance(r_ankle_ro) ]) self.r_leg = Leg.Leg(r_hip_ro, r_hip_wo, r_knee_ro, r_knee_wo, r_ankle_ro, r_ankle_wo, distances) l_hip = self.bandit.controlJoint(None, "modelRoot", self.L_LEG_JOINTS[0]) l_knee = self.bandit.controlJoint(None, "modelRoot", self.L_LEG_JOINTS[1]) l_ankle = self.bandit.exposeJoint(None, "modelRoot", self.L_LEG_JOINTS[2]) distances = np.array( [l_hip.getDistance(l_knee), l_knee.getDistance(l_ankle)]) #self.l_leg = Leg.Leg(l_hip, l_knee, l_ankle, distances) self.accept("arrow_up", self.r_leg.moveAnkle, [(0, 0, 0.1)]) self.accept("arrow_down", self.r_leg.moveAnkle, [(0, 0, -0.1)]) self.accept("arrow_left", self.r_leg.rotateAnkle, [(0, 0, 10)]) self.accept("arrow_right", self.r_leg.rotateAnkle, [(0, 0, -10)]) # Draws debug skeleton self.bandit.setBin('background', 1) self.walkJointHierarchy(self.bandit, self.bandit.getPartBundle('modelRoot')) self.stream = StreamRead("/dev/input/smartshoes") self.last_t = time.time() taskMgr.add(self.getDeviceData, 'Stream reader')
def main(args): maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect to Maya (Maya must be listening) maya.connect(("127.0.0.1", 7777)) vals = {"tx" : 0, "ty" : 0, "tz" : 0, "rx" : 0, "ry" : 0, "rz" : 0} data = {"ANKLE_L" : vals.copy(), "TOE_L" : vals.copy(), "ANKLE_R" : vals.copy(), "TOE_R" : vals.copy()} startTime = time.time() duration = 60 stream = StreamRead("/dev/input/smartshoes") # DEBUGGING (RUN WITHOUT DEVICE) if args.debug: while time.time() < startTime + duration: # Random rotation values data["ANKLE_L"]["rx"] = random.uniform(0,20) data["ANKLE_L"]["ry"] = random.uniform(0,20) data["ANKLE_L"]["rz"] = random.uniform(0,20) data["ANKLE_L"]["tx"] = random.uniform(0,10) data["ANKLE_L"]["ty"] = random.uniform(0,10) data["ANKLE_L"]["tz"] = random.uniform(0,10) data["ANKLE_R"]["rx"] = random.uniform(0,20) data["ANKLE_R"]["ry"] = random.uniform(0,20) data["ANKLE_R"]["rz"] = random.uniform(0,20) data["ANKLE_R"]["tx"] = random.uniform(0,10) data["ANKLE_R"]["ty"] = random.uniform(0,10) data["ANKLE_R"]["tz"] = random.uniform(0,10) jsonData = re.sub(r"\"", "\'", json.dumps(data)) print(jsonData) maya.send(jsonData) time.sleep(0.1) return r_leg_rotation = RotationCorrector() while time.time() < startTime + duration: records = stream.readFromStream() if records and len(records[0]) == 10: records = map(float, records[0]) angular_velocity, acceleration, magnetic_field = [records[x:x+3] for x in range(0, 9, 3)] # Switch axis orientations #angular_velocity[2], angular_velocity[0] = angular_velocity[0], angular_velocity[2] #acceleration[2], acceleration[0] = acceleration[0], acceleration[2] #magnetic_field[2], magnetic_field[0] = magnetic_field[0], magnetic_field[2] r_leg_rotation.rotationMagic(records[9], angular_velocity, acceleration, magnetic_field) data["ANKLE_R"]["ry"], data["ANKLE_R"]["rx"], data["ANKLE_R"]["rz"] = r_leg_rotation.getHpr() jsonData = re.sub(r"\"", "\'", json.dumps(data)) maya.send(jsonData)
def __init__(self): ShowBase.__init__(self) self.bandit = Actor("banditRiggedNoHat.egg") self.bandit.makeSubpart("r_leg", self.R_LEG_JOINTS) self.bandit.makeSubpart("l_leg", self.L_LEG_JOINTS) headJoint = self.bandit.exposeJoint(None, "modelRoot", "joint5") hat = loader.loadModel("{}_hat.egg".format(random.randint(0, 7))) hat.setPos(0, 0, -0.08) hat.setHpr(-90, 0, 0) hat.setScale(0.35) hat.reparentTo(headJoint) self.bandit.reparentTo(render) r_hip_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[0]) r_hip_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[0]) r_knee_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[1]) r_knee_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[1]) r_ankle_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[2]) r_ankle_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[2]) distances = np.array([r_hip_ro.getDistance(r_knee_ro), r_knee_ro.getDistance(r_ankle_ro)]) self.r_leg = Leg.Leg(r_hip_ro, r_hip_wo, r_knee_ro, r_knee_wo, r_ankle_ro, r_ankle_wo, distances) l_hip = self.bandit.controlJoint(None, "modelRoot", self.L_LEG_JOINTS[0]) l_knee = self.bandit.controlJoint(None, "modelRoot", self.L_LEG_JOINTS[1]) l_ankle = self.bandit.exposeJoint(None, "modelRoot", self.L_LEG_JOINTS[2]) distances = np.array([l_hip.getDistance(l_knee), l_knee.getDistance(l_ankle)]) #self.l_leg = Leg.Leg(l_hip, l_knee, l_ankle, distances) self.accept("arrow_up", self.r_leg.moveAnkle, [(0, 0, 0.1)]) self.accept("arrow_down", self.r_leg.moveAnkle, [(0, 0, -0.1)]) self.accept("arrow_left", self.r_leg.rotateAnkle, [(0, 0, 10)]) self.accept("arrow_right", self.r_leg.rotateAnkle, [(0, 0, -10)]) # Draws debug skeleton self.bandit.setBin('background', 1) self.walkJointHierarchy(self.bandit, self.bandit.getPartBundle('modelRoot')) self.stream = StreamRead("/dev/input/smartshoes") self.last_t = time.time() taskMgr.add(self.getDeviceData, 'Stream reader')
# Prints out the calibration values for the gyro # This can be terminated early with Ctrl+C and still print out the values #Allows for relative import import sys, os, argparse sys.path.append(os.path.abspath("../")) from utils.streamUtils.StreamRead import StreamRead parser = argparse.ArgumentParser(description = "Demo stream reading application - reads data from smart shoes stream and print to terminal") parser.add_argument("-s", action = "store", default = "/dev/input/smartshoes", type = str, dest = "streamFile", help = "The Name of the stream to write to (default: /dev/input/smartshoes)") inputArgs = parser.parse_args() #open stream reader stream = StreamRead(inputArgs.streamFile) #continually poll stream iterations = 0 x = 0.0 y = 0.0 z = 0.0 while iterations < 32: records = stream.readFromStream() if records and len(records[0]) == 10: x += float(records[0][3]) y += float(records[0][4]) z += float(records[0][5]) iterations += 1 print "Offsets: {}, {}, {}".format(x/iterations, y/iterations, z/iterations)
def main(args): maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect to Maya (Maya must be listening) maya.connect(("127.0.0.1", 7777)) vals = {"tx": 0, "ty": 0, "tz": 0, "rx": 0, "ry": 0, "rz": 0} data = { "ANKLE_L": vals.copy(), "TOE_L": vals.copy(), "ANKLE_R": vals.copy(), "TOE_R": vals.copy() } startTime = time.time() duration = 60 stream = StreamRead("/dev/input/smartshoes") # DEBUGGING (RUN WITHOUT DEVICE) if args.debug: while time.time() < startTime + duration: # Random rotation values data["ANKLE_L"]["rx"] = random.uniform(0, 20) data["ANKLE_L"]["ry"] = random.uniform(0, 20) data["ANKLE_L"]["rz"] = random.uniform(0, 20) data["ANKLE_L"]["tx"] = random.uniform(0, 10) data["ANKLE_L"]["ty"] = random.uniform(0, 10) data["ANKLE_L"]["tz"] = random.uniform(0, 10) data["ANKLE_R"]["rx"] = random.uniform(0, 20) data["ANKLE_R"]["ry"] = random.uniform(0, 20) data["ANKLE_R"]["rz"] = random.uniform(0, 20) data["ANKLE_R"]["tx"] = random.uniform(0, 10) data["ANKLE_R"]["ty"] = random.uniform(0, 10) data["ANKLE_R"]["tz"] = random.uniform(0, 10) jsonData = re.sub(r"\"", "\'", json.dumps(data)) print(jsonData) maya.send(jsonData) time.sleep(0.1) return r_leg_rotation = RotationCorrector() while time.time() < startTime + duration: records = stream.readFromStream() if records and len(records[0]) == 10: records = map(float, records[0]) angular_velocity, acceleration, magnetic_field = [ records[x:x + 3] for x in range(0, 9, 3) ] # Switch axis orientations #angular_velocity[2], angular_velocity[0] = angular_velocity[0], angular_velocity[2] #acceleration[2], acceleration[0] = acceleration[0], acceleration[2] #magnetic_field[2], magnetic_field[0] = magnetic_field[0], magnetic_field[2] r_leg_rotation.rotationMagic(records[9], angular_velocity, acceleration, magnetic_field) data["ANKLE_R"]["ry"], data["ANKLE_R"]["rx"], data["ANKLE_R"][ "rz"] = r_leg_rotation.getHpr() jsonData = re.sub(r"\"", "\'", json.dumps(data)) maya.send(jsonData)
class MayaDemo(ShowBase): R_LEG_JOINTS = ["joint6", "joint7", "joint8"] L_LEG_JOINTS = ["joint10", "joint11", "joint12"] def __init__(self): ShowBase.__init__(self) self.bandit = Actor("banditRiggedNoHat.egg") self.bandit.makeSubpart("r_leg", self.R_LEG_JOINTS) self.bandit.makeSubpart("l_leg", self.L_LEG_JOINTS) headJoint = self.bandit.exposeJoint(None, "modelRoot", "joint5") hat = loader.loadModel("{}_hat.egg".format(random.randint(0, 7))) hat.setPos(0, 0, -0.08) hat.setHpr(-90, 0, 0) hat.setScale(0.35) hat.reparentTo(headJoint) self.bandit.reparentTo(render) r_hip_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[0]) r_hip_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[0]) r_knee_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[1]) r_knee_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[1]) r_ankle_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[2]) r_ankle_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[2]) distances = np.array([ r_hip_ro.getDistance(r_knee_ro), r_knee_ro.getDistance(r_ankle_ro) ]) self.r_leg = Leg.Leg(r_hip_ro, r_hip_wo, r_knee_ro, r_knee_wo, r_ankle_ro, r_ankle_wo, distances) l_hip = self.bandit.controlJoint(None, "modelRoot", self.L_LEG_JOINTS[0]) l_knee = self.bandit.controlJoint(None, "modelRoot", self.L_LEG_JOINTS[1]) l_ankle = self.bandit.exposeJoint(None, "modelRoot", self.L_LEG_JOINTS[2]) distances = np.array( [l_hip.getDistance(l_knee), l_knee.getDistance(l_ankle)]) #self.l_leg = Leg.Leg(l_hip, l_knee, l_ankle, distances) self.accept("arrow_up", self.r_leg.moveAnkle, [(0, 0, 0.1)]) self.accept("arrow_down", self.r_leg.moveAnkle, [(0, 0, -0.1)]) self.accept("arrow_left", self.r_leg.rotateAnkle, [(0, 0, 10)]) self.accept("arrow_right", self.r_leg.rotateAnkle, [(0, 0, -10)]) # Draws debug skeleton self.bandit.setBin('background', 1) self.walkJointHierarchy(self.bandit, self.bandit.getPartBundle('modelRoot')) self.stream = StreamRead("/dev/input/smartshoes") self.last_t = time.time() taskMgr.add(self.getDeviceData, 'Stream reader') def getDeviceData(self, task): records = self.stream.readFromStream() if records and len(records[0]) == 10: records = map(float, records[0]) angular_velocity, acceleration, magnetic_field = [ records[x:x + 3] for x in range(0, 9, 3) ] # Switch axis orientations angular_velocity[2], angular_velocity[0] = angular_velocity[ 0], angular_velocity[2] acceleration[2], acceleration[0] = acceleration[0], acceleration[2] magnetic_field[2], magnetic_field[0] = magnetic_field[ 0], magnetic_field[2] self.r_leg.ankle_rotation.rotationMagic(records[9], angular_velocity, acceleration, magnetic_field) self.r_leg.updateAnkleRotation() return task.again def walkJointHierarchy(self, actor, part, parentNode=None, indent=""): if isinstance(part, CharacterJoint): np = actor.exposeJoint(None, 'modelRoot', part.getName()) if parentNode and parentNode.getName() != "root": lines = LineSegs() lines.setThickness(3.0) lines.setColor(random.random(), random.random(), random.random()) lines.moveTo(0, 0, 0) lines.drawTo(np.getPos(parentNode)) lnp = parentNode.attachNewNode(lines.create()) lnp.setBin("fixed", 40) lnp.setDepthWrite(False) lnp.setDepthTest(False) parentNode = np for child in part.getChildren(): self.walkJointHierarchy(actor, child, parentNode, indent + " ")
class MayaDemo(ShowBase): R_LEG_JOINTS = ["joint6", "joint7", "joint8"] L_LEG_JOINTS = ["joint10", "joint11", "joint12"] def __init__(self): ShowBase.__init__(self) self.bandit = Actor("banditRiggedNoHat.egg") self.bandit.makeSubpart("r_leg", self.R_LEG_JOINTS) self.bandit.makeSubpart("l_leg", self.L_LEG_JOINTS) headJoint = self.bandit.exposeJoint(None, "modelRoot", "joint5") hat = loader.loadModel("{}_hat.egg".format(random.randint(0, 7))) hat.setPos(0, 0, -0.08) hat.setHpr(-90, 0, 0) hat.setScale(0.35) hat.reparentTo(headJoint) self.bandit.reparentTo(render) r_hip_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[0]) r_hip_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[0]) r_knee_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[1]) r_knee_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[1]) r_ankle_ro = self.bandit.exposeJoint(None, "modelRoot", self.R_LEG_JOINTS[2]) r_ankle_wo = self.bandit.controlJoint(None, "modelRoot", self.R_LEG_JOINTS[2]) distances = np.array([r_hip_ro.getDistance(r_knee_ro), r_knee_ro.getDistance(r_ankle_ro)]) self.r_leg = Leg.Leg(r_hip_ro, r_hip_wo, r_knee_ro, r_knee_wo, r_ankle_ro, r_ankle_wo, distances) l_hip = self.bandit.controlJoint(None, "modelRoot", self.L_LEG_JOINTS[0]) l_knee = self.bandit.controlJoint(None, "modelRoot", self.L_LEG_JOINTS[1]) l_ankle = self.bandit.exposeJoint(None, "modelRoot", self.L_LEG_JOINTS[2]) distances = np.array([l_hip.getDistance(l_knee), l_knee.getDistance(l_ankle)]) #self.l_leg = Leg.Leg(l_hip, l_knee, l_ankle, distances) self.accept("arrow_up", self.r_leg.moveAnkle, [(0, 0, 0.1)]) self.accept("arrow_down", self.r_leg.moveAnkle, [(0, 0, -0.1)]) self.accept("arrow_left", self.r_leg.rotateAnkle, [(0, 0, 10)]) self.accept("arrow_right", self.r_leg.rotateAnkle, [(0, 0, -10)]) # Draws debug skeleton self.bandit.setBin('background', 1) self.walkJointHierarchy(self.bandit, self.bandit.getPartBundle('modelRoot')) self.stream = StreamRead("/dev/input/smartshoes") self.last_t = time.time() taskMgr.add(self.getDeviceData, 'Stream reader') def getDeviceData(self, task): records = self.stream.readFromStream() if records and len(records[0]) == 10: records = map(float, records[0]) angular_velocity, acceleration, magnetic_field = [records[x:x+3] for x in range(0, 9, 3)] # Switch axis orientations angular_velocity[2], angular_velocity[0] = angular_velocity[0], angular_velocity[2] acceleration[2], acceleration[0] = acceleration[0], acceleration[2] magnetic_field[2], magnetic_field[0] = magnetic_field[0], magnetic_field[2] self.r_leg.ankle_rotation.rotationMagic(records[9], angular_velocity, acceleration, magnetic_field) self.r_leg.updateAnkleRotation() return task.again def walkJointHierarchy(self, actor, part, parentNode = None, indent = ""): if isinstance(part, CharacterJoint): np = actor.exposeJoint(None, 'modelRoot', part.getName()) if parentNode and parentNode.getName() != "root": lines = LineSegs() lines.setThickness(3.0) lines.setColor(random.random(), random.random(), random.random()) lines.moveTo(0, 0, 0) lines.drawTo(np.getPos(parentNode)) lnp = parentNode.attachNewNode(lines.create()) lnp.setBin("fixed", 40) lnp.setDepthWrite(False) lnp.setDepthTest(False) parentNode = np for child in part.getChildren(): self.walkJointHierarchy(actor, child, parentNode, indent + " ")
#Allows for relative import import sys, os, argparse sys.path.append(os.path.abspath("../")) from utils.streamUtils.StreamRead import StreamRead parser = argparse.ArgumentParser( description= "Demo stream reading application - reads data from smart shoes stream and print to terminal" ) parser.add_argument( "-s", action="store", default="/dev/input/smartshoes", type=str, dest="streamFile", help="The Name of the stream to write to (default: /dev/input/smartshoes)") inputArgs = parser.parse_args() #open stream reader stream = StreamRead(inputArgs.streamFile) #continually poll stream while True: records = stream.readFromStream() if records: for recordList in records: print recordList #print map((lambda x: "%05d" % int(x)), recordList)
def __init__(self, inputArgs): self.debug = inputArgs.debug exclude_idxs = [9] # Value to determine how long to wait for hold gestures, and how many # samples to take for explicit gestures self.numSamples = 5 # Timeout for taking explicit samples self.ExplTimeout = 3 # Center the window on the screen os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.init() size = width, height = 1500, 800 speed = [2, 2] black = 0, 0, 0 self.screen = pygame.display.set_mode(size) pygame.display.set_caption('FootGest Data capture') # Fill background self.background = pygame.Surface(self.screen.get_size()) self.background = self.background.convert() self.background.fill((250, 250, 250)) # Blit everything to the screen self.screen.blit(self.background, (0, 0)) pygame.display.flip() #self.c = calibration.Calibration() # Open input file if not self.debug: self.stream = StreamRead(inputArgs.streamFile, exclude_indexes = exclude_idxs) self.gesture_classes = [{ "className" : "REST", "type" : "HOLD", "desc" : "keep your feet at rest"}, { "className" : "LEFT-FOOT-STOMP", "type" : "EXPL", "desc" : "stamp your left foot"}, { "className" : "LEFT-TOE-UP", "type" : "HOLD", "desc" : "raise your left toe while your heel is on the ground"}, { "className" : "LEFT-TOE-TAP", "type" : "EXPL", "desc" : "tap your left toe once"}, { "className" : "LEFT-HEEL-TAP", "type" : "EXPL", "desc" : "tap your left heel once"}, { "className" : "LEFT-HEEL-RAISE", "type" : "HOLD", "desc" : "raise your left heel"}, { "className" : "LEFT-FOOT-FLICKLEFT", "type" : "EXPL", "desc" : "flick your left foot to the left"}, { "className" : "LEFT-FOOT-FLICKRIGHT", "type" : "EXPL", "desc" : "flick your left foot to the right"}, ] #generate trial orders trials = self.genTrials(inputArgs.num_trials) #run trials for trial in trials: print "Capturing Gesture:", trial["className"] if trial["type"] == "HOLD": self.captureHold(inputArgs.DIR, trial["className"], trial["desc"]) self.endTrial() else: # Explicit gesture movement for i in xrange(0, self.numSamples): self.captureExplicit(inputArgs.DIR, trial["className"], trial["desc"]) self.endTrial()
class main(): def __init__(self, inputArgs): self.debug = inputArgs.debug exclude_idxs = [9] # Value to determine how long to wait for hold gestures, and how many # samples to take for explicit gestures self.numSamples = 5 # Timeout for taking explicit samples self.ExplTimeout = 3 # Center the window on the screen os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.init() size = width, height = 1500, 800 speed = [2, 2] black = 0, 0, 0 self.screen = pygame.display.set_mode(size) pygame.display.set_caption('FootGest Data capture') # Fill background self.background = pygame.Surface(self.screen.get_size()) self.background = self.background.convert() self.background.fill((250, 250, 250)) # Blit everything to the screen self.screen.blit(self.background, (0, 0)) pygame.display.flip() #self.c = calibration.Calibration() # Open input file if not self.debug: self.stream = StreamRead(inputArgs.streamFile, exclude_indexes = exclude_idxs) self.gesture_classes = [{ "className" : "REST", "type" : "HOLD", "desc" : "keep your feet at rest"}, { "className" : "LEFT-FOOT-STOMP", "type" : "EXPL", "desc" : "stamp your left foot"}, { "className" : "LEFT-TOE-UP", "type" : "HOLD", "desc" : "raise your left toe while your heel is on the ground"}, { "className" : "LEFT-TOE-TAP", "type" : "EXPL", "desc" : "tap your left toe once"}, { "className" : "LEFT-HEEL-TAP", "type" : "EXPL", "desc" : "tap your left heel once"}, { "className" : "LEFT-HEEL-RAISE", "type" : "HOLD", "desc" : "raise your left heel"}, { "className" : "LEFT-FOOT-FLICKLEFT", "type" : "EXPL", "desc" : "flick your left foot to the left"}, { "className" : "LEFT-FOOT-FLICKRIGHT", "type" : "EXPL", "desc" : "flick your left foot to the right"}, ] #generate trial orders trials = self.genTrials(inputArgs.num_trials) #run trials for trial in trials: print "Capturing Gesture:", trial["className"] if trial["type"] == "HOLD": self.captureHold(inputArgs.DIR, trial["className"], trial["desc"]) self.endTrial() else: # Explicit gesture movement for i in xrange(0, self.numSamples): self.captureExplicit(inputArgs.DIR, trial["className"], trial["desc"]) self.endTrial() ## Utility Methods ## #generate a random ordering of trials def genTrials(self, num_trials): trials = [] for c in self.gesture_classes: for i in xrange(0, num_trials): trials.append(c) random.shuffle(trials) return trials #Write test message to screen def textToScreen(self, text, y_pos=10): self.background = pygame.Surface(self.screen.get_size()) self.background = self.background.convert() self.background.fill((250, 250, 250)) font = pygame.font.Font(None, 28) text = font.render(text, 1, (10, 10, 10)) textpos = text.get_rect() textpos.centerx = self.background.get_rect().centerx self.background.blit(text, textpos) self.screen.blit(self.background, (0, y_pos)) pygame.display.flip() #Display message and wait for next test def endTrial(self): self.textToScreen("Thank you. Please rest your feet and wait for the next test.") sleepEnd = time.time() + 2 while time.time() < sleepEnd: for event in pygame.event.get(): if event.type == pygame.KEYDOWN : # Means that spaces don't carry over to next trial pass ## File Utilities ## #make data file with Class name an Uniqie ID def makeFile(self, dir_name, class_name): return file(os.path.join(dir_name, class_name + '_' + str(uuid.uuid1()) + '.csv'), 'w+') #Write list of data points to file def writeData(self, file, data): enc_data = ','.join(map(str,data)) + '\n' file.write(enc_data) ## Trial Methods ## # Base capture method - Press space, do gesture and hold def captureHold(self, dir_name, gesture, displayText): self.textToScreen("Press SPACE, " + displayText + ", and hold for the next " + str(self.numSamples) + " seconds") mov = GestVid(gesture, self.screen) ready = False # Wait for user to begin test while not ready: mov.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN : if event.key == pygame.K_SPACE : ready = True if self.debug: for i in xrange(0, self.numSamples): self.textToScreen("Please wait " + str(self.numSamples-i) + " seconds") # Don't capture data time.sleep(1) return # Capture data for as long as holdLength (seconds) for i in xrange(0, self.numSamples): self.textToScreen("Please wait " + str(self.numSamples-i) + " seconds") output_file = self.makeFile(dir_name, gesture) #output_file_calibrated = self.makeFile(dir_name + "_calib", gesture) self.stream.emptyStreamBuffer() stop = time.time() + 1 while time.time() < stop: records = self.stream.readFromStream() for record in records: self.writeData(output_file, record) #if len(record) < 9: # self.writeData(output_file_calibrated, record) #else: # self.writeData(output_file_calibrated, list(self.c.process(*[float(i) for i in record]))) # Base capture method - Press space, do gesture, then press space def captureExplicit(self, dir_name, gesture, displayText): self.textToScreen("Press SPACE, " + displayText + ", and then press SPACE as soon as you are done") mov = GestVid(gesture, self.screen) if not self.debug: output_file = self.makeFile(dir_name, gesture) #output_file_calibrated = self.makeFile(dir_name + "_calib", gesture) ready = False done = False # Wait for user to begin test while not ready: mov.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN : if event.key == pygame.K_SPACE : ready = True startTime = time.time() if not self.debug: self.stream.emptyStreamBuffer() self.textToScreen(displayText + ", and then press SPACE as soon as you are done") # Capture data until space is pressed or reached timeout while not done and time.time() < startTime+self.ExplTimeout : if not self.debug: records = self.stream.readFromStream() for record in records: self.writeData(output_file, record) #if len(record) < 9: # self.writeData(output_file_calibrated, record) #else: # self.writeData(output_file_calibrated, list(self.c.process(*[float(i) for i in record]))) for event in pygame.event.get(): if event.type == pygame.KEYDOWN : # Check that we've captured data for at least 0.3 seconds before quitting if event.key == pygame.K_SPACE and time.time() > startTime+0.3 : done = True
parser = argparse.ArgumentParser( description= "Demo stream reading application - reads data from smart shoes stream and print to terminal" ) parser.add_argument( "-s", action="store", default="/dev/input/smartshoes", type=str, dest="streamFile", help="The Name of the stream to write to (default: /dev/input/smartshoes)") inputArgs = parser.parse_args() #open stream reader stream = StreamRead(inputArgs.streamFile) class SubplotAnimation(animation.TimedAnimation): def __init__(self): fig = plt.figure() self.axes = [] self.lines = [] self.annotations = [] for i in xrange(0, 9): axis = fig.add_subplot(3, 3, i + 1) axis.set_ylim(-1, 1) axis.set_xlim(0, 50) axis.grid() self.axes.append(axis)
class main(): def __init__(self, inputArgs): self.debug = inputArgs.debug exclude_idxs = [9] # Value to determine how long to wait for hold gestures, and how many # samples to take for explicit gestures self.numSamples = 5 # Timeout for taking explicit samples self.ExplTimeout = 3 # Center the window on the screen os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.init() size = width, height = 1500, 800 speed = [2, 2] black = 0, 0, 0 self.screen = pygame.display.set_mode(size) pygame.display.set_caption('FootGest Data capture') # Fill background self.background = pygame.Surface(self.screen.get_size()) self.background = self.background.convert() self.background.fill((250, 250, 250)) # Blit everything to the screen self.screen.blit(self.background, (0, 0)) pygame.display.flip() #self.c = calibration.Calibration() # Open input file if not self.debug: self.stream = StreamRead(inputArgs.streamFile, exclude_indexes=exclude_idxs) self.gesture_classes = [ { "className": "REST", "type": "HOLD", "desc": "keep your feet at rest" }, { "className": "LEFT-FOOT-STOMP", "type": "EXPL", "desc": "stamp your left foot" }, { "className": "LEFT-TOE-UP", "type": "HOLD", "desc": "raise your left toe while your heel is on the ground" }, { "className": "LEFT-TOE-TAP", "type": "EXPL", "desc": "tap your left toe once" }, { "className": "LEFT-HEEL-TAP", "type": "EXPL", "desc": "tap your left heel once" }, { "className": "LEFT-HEEL-RAISE", "type": "HOLD", "desc": "raise your left heel" }, { "className": "LEFT-FOOT-FLICKLEFT", "type": "EXPL", "desc": "flick your left foot to the left" }, { "className": "LEFT-FOOT-FLICKRIGHT", "type": "EXPL", "desc": "flick your left foot to the right" }, ] #generate trial orders trials = self.genTrials(inputArgs.num_trials) #run trials for trial in trials: print "Capturing Gesture:", trial["className"] if trial["type"] == "HOLD": self.captureHold(inputArgs.DIR, trial["className"], trial["desc"]) self.endTrial() else: # Explicit gesture movement for i in xrange(0, self.numSamples): self.captureExplicit(inputArgs.DIR, trial["className"], trial["desc"]) self.endTrial() ## Utility Methods ## #generate a random ordering of trials def genTrials(self, num_trials): trials = [] for c in self.gesture_classes: for i in xrange(0, num_trials): trials.append(c) random.shuffle(trials) return trials #Write test message to screen def textToScreen(self, text, y_pos=10): self.background = pygame.Surface(self.screen.get_size()) self.background = self.background.convert() self.background.fill((250, 250, 250)) font = pygame.font.Font(None, 28) text = font.render(text, 1, (10, 10, 10)) textpos = text.get_rect() textpos.centerx = self.background.get_rect().centerx self.background.blit(text, textpos) self.screen.blit(self.background, (0, y_pos)) pygame.display.flip() #Display message and wait for next test def endTrial(self): self.textToScreen( "Thank you. Please rest your feet and wait for the next test.") sleepEnd = time.time() + 2 while time.time() < sleepEnd: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: # Means that spaces don't carry over to next trial pass ## File Utilities ## #make data file with Class name an Uniqie ID def makeFile(self, dir_name, class_name): return file( os.path.join(dir_name, class_name + '_' + str(uuid.uuid1()) + '.csv'), 'w+') #Write list of data points to file def writeData(self, file, data): enc_data = ','.join(map(str, data)) + '\n' file.write(enc_data) ## Trial Methods ## # Base capture method - Press space, do gesture and hold def captureHold(self, dir_name, gesture, displayText): self.textToScreen("Press SPACE, " + displayText + ", and hold for the next " + str(self.numSamples) + " seconds") mov = GestVid(gesture, self.screen) ready = False # Wait for user to begin test while not ready: mov.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: ready = True if self.debug: for i in xrange(0, self.numSamples): self.textToScreen("Please wait " + str(self.numSamples - i) + " seconds") # Don't capture data time.sleep(1) return # Capture data for as long as holdLength (seconds) for i in xrange(0, self.numSamples): self.textToScreen("Please wait " + str(self.numSamples - i) + " seconds") output_file = self.makeFile(dir_name, gesture) #output_file_calibrated = self.makeFile(dir_name + "_calib", gesture) self.stream.emptyStreamBuffer() stop = time.time() + 1 while time.time() < stop: records = self.stream.readFromStream() for record in records: self.writeData(output_file, record) #if len(record) < 9: # self.writeData(output_file_calibrated, record) #else: # self.writeData(output_file_calibrated, list(self.c.process(*[float(i) for i in record]))) # Base capture method - Press space, do gesture, then press space def captureExplicit(self, dir_name, gesture, displayText): self.textToScreen("Press SPACE, " + displayText + ", and then press SPACE as soon as you are done") mov = GestVid(gesture, self.screen) if not self.debug: output_file = self.makeFile(dir_name, gesture) #output_file_calibrated = self.makeFile(dir_name + "_calib", gesture) ready = False done = False # Wait for user to begin test while not ready: mov.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: ready = True startTime = time.time() if not self.debug: self.stream.emptyStreamBuffer() self.textToScreen(displayText + ", and then press SPACE as soon as you are done") # Capture data until space is pressed or reached timeout while not done and time.time() < startTime + self.ExplTimeout: if not self.debug: records = self.stream.readFromStream() for record in records: self.writeData(output_file, record) #if len(record) < 9: # self.writeData(output_file_calibrated, record) #else: # self.writeData(output_file_calibrated, list(self.c.process(*[float(i) for i in record]))) for event in pygame.event.get(): if event.type == pygame.KEYDOWN: # Check that we've captured data for at least 0.3 seconds before quitting if event.key == pygame.K_SPACE and time.time( ) > startTime + 0.3: done = True
def __init__(self, inputArgs): self.debug = inputArgs.debug exclude_idxs = [9] # Value to determine how long to wait for hold gestures, and how many # samples to take for explicit gestures self.numSamples = 5 # Timeout for taking explicit samples self.ExplTimeout = 3 # Center the window on the screen os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.init() size = width, height = 1500, 800 speed = [2, 2] black = 0, 0, 0 self.screen = pygame.display.set_mode(size) pygame.display.set_caption('FootGest Data capture') # Fill background self.background = pygame.Surface(self.screen.get_size()) self.background = self.background.convert() self.background.fill((250, 250, 250)) # Blit everything to the screen self.screen.blit(self.background, (0, 0)) pygame.display.flip() #self.c = calibration.Calibration() # Open input file if not self.debug: self.stream = StreamRead(inputArgs.streamFile, exclude_indexes=exclude_idxs) self.gesture_classes = [ { "className": "REST", "type": "HOLD", "desc": "keep your feet at rest" }, { "className": "LEFT-FOOT-STOMP", "type": "EXPL", "desc": "stamp your left foot" }, { "className": "LEFT-TOE-UP", "type": "HOLD", "desc": "raise your left toe while your heel is on the ground" }, { "className": "LEFT-TOE-TAP", "type": "EXPL", "desc": "tap your left toe once" }, { "className": "LEFT-HEEL-TAP", "type": "EXPL", "desc": "tap your left heel once" }, { "className": "LEFT-HEEL-RAISE", "type": "HOLD", "desc": "raise your left heel" }, { "className": "LEFT-FOOT-FLICKLEFT", "type": "EXPL", "desc": "flick your left foot to the left" }, { "className": "LEFT-FOOT-FLICKRIGHT", "type": "EXPL", "desc": "flick your left foot to the right" }, ] #generate trial orders trials = self.genTrials(inputArgs.num_trials) #run trials for trial in trials: print "Capturing Gesture:", trial["className"] if trial["type"] == "HOLD": self.captureHold(inputArgs.DIR, trial["className"], trial["desc"]) self.endTrial() else: # Explicit gesture movement for i in xrange(0, self.numSamples): self.captureExplicit(inputArgs.DIR, trial["className"], trial["desc"]) self.endTrial()