def PCA(self): if self.pcs == None: print "CANNOT DO PCA, scipy isn't loaded" print "doing pca" motion = self.tracks[self.selectedTrack].GetMotion() splits = self.tracks[self.selectedTrack].GetSplits() if len(splits) > 0: motions = [ Piavca.SubMotion(motion, split.GetStart(), split.GetEnd()) for split in splits ] else: motions = [motion] splits = [(0, split.GetStart(), split.GetEnd()) for split in splits] length = Track.PositionToTime( self.track_length) * Track.frames_per_second #self.pcs.do_pca([motion], splits, length, Track.frames_per_second) self.pcs.addMotions(motions) self.pcs.setFramesPerSecond(Track.frames_per_second) self.pcs.create() #self.pcs.setMotion(motion) # load the PCAs into the avatar to play them Piavca.Core.getCore().setCurrentTime(0) #self.avatar.playMotionDirect(Piavca.PyMotion(self.pcs)) self.avatar.playMotionDirect(self.pcs) Piavca.Core.getCore().setCurrentTime(self.time)
def query(self, e): mot = self.motion.getValue() compMotion = self.comparisonMotion.getValue() fps = self.fps.getValue() threshold = self.threshold.getValue() compMotSeq = MotionList(compMotion, fps) #frameslist = self.backend.getMotionFramesByName(framelist) frameslist = findMinima(mot, compMotion, 0.0, threshold, fps) print frameslist framepairs = itertools.chain(zip(frameslist[:-1], frameslist[1:]),zip(frameslist[:-2], frameslist[2:]),zip(frameslist[:-3], frameslist[3:])) submots = ((MotionList(Piavca.SubMotion(mot, f1, f2), fps),f1,f2) for f1, f2 in framepairs) #submots = [a for a in submots] #print submots #print "about to call dynamic timewarp" self.results = [(DynamicTimewarpCost(compMotSeq, submot, poseDifference), f1, f2) for submot, f1, f2 in submots] self.results.sort() print "results", self.results for i, result in enumerate(self.results): self.list.InsertStringItem(i, str(result[0]) + " " + str(result[1]) + " " + str(result[2]))
def getMotion(self): motion = self.motion.getValue() i = self.list.GetNextSelected(-1) print i if i >= 0: return Piavca.SubMotion(motion, self.results[i][1], self.results[i][2]) else: return None
def getMotion(self): mot = self.backend.motion.getMotion() if (mot == None): return None start, end = self.backend.getRange() #start += mot.getStartTime() #end += mot.getStart() submot = Piavca.SubMotion(mot, start, end) return submot
def getMotion(self): mot = self.motion.getValue() compMotion = self.comparisonMotion.getValue() compFrame = self.comparisonFrame.getValue() fps = self.fps.getValue() threshold = self.threshold.getValue() name = self.name.getValue() framelist = self.FrameLists.getValue() list = self.backend.getMotionFramesByName(framelist) if list == None: minima = findMinima(mot, compMotion, compFrame, threshold, fps) print minima self.backend.addMotionFrames(name, minima) self.backend.setMotionFrames(name) else: self.backend.setMotionFrames(framelist) return Piavca.SubMotion(mot, 0, mot.getMotionLength())
def SlowSubspace(self): if self.pcs == None: print "CANNOT DO PCA, scipy isn't loaded" print "doing slow subspace" motion = self.tracks[self.selectedTrack].GetMotion() splits = self.tracks[self.selectedTrack].GetSplits() if len(splits) > 0: motions = [ Piavca.SubMotion(motion, split.GetStart(), split.GetEnd()) for split in splits ] else: motions = [motion] splits = [(0, split.GetStart(), split.GetEnd()) for split in splits] length = Track.PositionToTime( self.track_length) * Track.frames_per_second #self.pcs.do_pca([motion], splits, length, Track.frames_per_second) self.pcs.addMotions(motions) print "doing sub space on motions", motions self.pcs.setFramesPerSecond(Track.frames_per_second) ssa = SlowSubSpace.SlowSubSpace() val = None dialog_return = textEntryDialog( None, message="enter slow subspace trade off (alpha)", defaultText="0.2") val = float(dialog_return.text.encode("latin-1")) #dialog.SetValue("0.2") # The user pressed the "OK" button in the dialog #if dialog.ShowModal() == wxID_OK: # print 'Position of selection:', dialog.GetValue() # val = float(dialog.GetValue()) # print val #else: # path = None # print 'You did not select anything.' #dialog.Destroy() if val != None: ssa.setAlpha(val) val = None dialog_return = textEntryDialog(None, message="enter percentage to keep", defaultText="0.99") val = float(dialog_return.text.encode("latin-1")) #dialog = wxTextEntryDialog ( None, message="enter percentage to keep" ) #dialog.SetValue("0.99") # The user pressed the "OK" button in the dialog #if dialog.ShowModal() == wxID_OK: # print 'Position of selection:', dialog.GetValue() # val = float(dialog.GetValue()) # print val #else: # path = None # print 'You did not select anything.' #dialog.Destroy() if val != None: ssa.setPercentageToKeep(val) self.pcs.setAnalysis(ssa) self.pcs.create() #self.pcs.setMotion(motion) # load the PCAs into the avatar to play them Piavca.Core.getCore().setCurrentTime(0) #self.avatar.playMotionDirect(Piavca.PyMotion(self.pcs)) self.avatar.playMotionDirect(self.pcs) Piavca.Core.getCore().setCurrentTime(self.time)
def Save(self): if self.selectedTrack == None: return dialog_return = saveFileDialog() path = dialog_return.paths[0] if path == "": return #dialog = wxFileDialog ( None, style = wxSAVE ) # The user pressed the "OK" button in the dialog #if dialog.ShowModal() == wxID_OK: # print 'Position of selection:', dialog.GetPath() # path = dialog.GetPath() # dotpos = path.rfind(".") # scriptpath = path[:dotpos]+".act" #else: # path = None # print 'You did not select anything.' # return #dialog.Destroy() #preloaded = [(self.motion, self.motion_name)] # save the motion pack file motstosave = [] motion = self.tracks[self.selectedTrack].GetMotion() if self.tracks[self.selectedTrack].IsMasked(): if motion.getName() == "": motion.setName(self.motion_name + "_masked") motstosave.append(motion) splits = self.tracks[self.selectedTrack].GetSplits() for i, split in enumerate(splits): mot = Piavca.SubMotion(motion, split.GetStart(), split.GetEnd()) mot.setName(self.motion_name + "_split_" + str(i)) motstosave.append(mot) if self.motiongraph: if self.motiongraph.getName() == "": self.motiongraph.setName(self.motion_name + "_motiongraph") motstosave.append(self.motiongraph) Piavca.XMLMotionFile.saveMotions(path, motstosave) # save the scripts file dotpos = path.rfind(".") scriptpath = path[:dotpos] + ".act" scriptfile = open(scriptpath, "w") for motion in motstosave: scriptfile.write("script ") scriptfile.write(motion.getName()) scriptfile.write("\n") scriptfile.write("0 motion ") scriptfile.write(motion.getName()) scriptfile.write("\n") scriptfile.write("stop\n") scriptfile.close() # save the main conf file confpath = path[:dotpos] + ".conf" conffile = open(confpath, "w") conffile.write("avatar ") conffile.write(self.avatar.getName()) conffile.write("\n") conffile.write("motionpack default ") slashpos = path.rfind("\\") if slashpos < 0: slashpos = path.rfind("/") if slashpos < 0: slashpos = 0 conffile.write(path[slashpos + 1:]) conffile.write("\n") conffile.write("script default ") slashpos = scriptpath.rfind("\\") if slashpos < 0: slashpos = scriptpath.rfind("/") if slashpos < 0: slashpos = 0 conffile.write(scriptpath[slashpos + 1:]) conffile.write("\n") conffile.write("GUI ") conffile.write(self.avatar.getName()) conffile.write("\n") conffile.close()
def InterruptableSequence(seq, interruptions, fps=20, threshold=6.5, window=0.5): expmaps = {} # create a set of exponential maps # what this does is find the average joint # rotations of all of start and end poses of the # interruptions, and get them in a form where # its easy to calculate the distance of a new quaternion to # them for j in Piavca.joints(seq): # don't take the root into account as we reposition the motion if j == Piavca.root_orientation_id or j == Piavca.root_position_id: continue if seq.isNull(j): continue joint_type = seq.getTrackType(j) # the joint orientations of # this joint at the start and end # frames of the interruptions for t in (Piavca.FLOAT_TYPE, Piavca.VEC_TYPE, Piavca.QUAT_TYPE): if not (t & joint_type): continue extremeFrames = [] for m in interruptions: if m.isNull(j): continue if not (m.getTrackType(j) & t): continue start_time = m.getStartTime() extremeFrames.append(Piavca.getValAtTime(m, j, start_time, t)) end_time = m.getEndTime() extremeFrames.append(Piavca.getValAtTime(m, j, end_time, t)) if len(extremeFrames) == 0: expmaps[(j, t)] = None elif t == Piavca.QUAT_TYPE: expmaps[(j, t)] = Piavca.ExpMap.TangentSpace(extremeFrames) elif t == Piavca.VEC_TYPE: expmaps[(j, t)] = sum(extremeFrames, Piavca.Vec()) / float( len(extremeFrames)) else: print len(extremeFrames), extremeFrames expmaps[(j, t)] = sum(extremeFrames) / float(len(extremeFrames)) d_minus = None d = None d_plus = None # find local minima of the distance function # if its lower than a threshold then we add it to the set of # of possible transition points minima = [seq.getStartTime()] values = [] #print "start time", seq.getStartTime(), "end time", seq.getEndTime() for i in range(int(seq.getStartTime() * fps), int(seq.getEndTime() * fps) - 1): d_plus = calculateDistance(seq, float(i + 1) / fps, expmaps) print d, d_plus, threshold if d: if d < d_plus: if d_minus != None and d < d_minus: print float(i) / fps, d, d_plus, threshold, 4.0 * window if d < threshold: if len(minima) == 0 or (float(i) / fps - minima[-1]) > 4.0 * window: values.append(d) minima.append(float(i) / fps) d_minus = d d = d_plus minima.append(seq.getEndTime()) values.sort(reverse=False) print "values", values print "minima", minima numMinima = len(minima) # create the motions # a submotion for each transition point # a sequential choice motion that plays the original motion # a choice motion with default that allows us to interrupt # and finally loop in all # we add "window" to the end time so that it works with # the smooth transitioning function submots = [ Piavca.SubMotion(seq, start, end) for start, end in zip(minima[:-1], minima[1:]) ] print submots choice1 = Piavca.SequentialChoiceMotion() choice1.setSmooth(False) choice1.setAccumulateRoot(False) choice1.setWindowLength(window) for mot in submots: print mot choice1.addMotion(mot) loop1 = Piavca.LoopMotion(choice1) choice2 = Piavca.ChoiceMotionWithDefault() choice2.setWindowLength(window) choice2.addMotion(loop1) for mot in interruptions: print mot choice2.addMotion(mot) loop2 = Piavca.LoopMotion(choice2) return loop2, numMinima
def Save(self, filename): SubMotion = Piavca.SubMotion(self.originalMotion, self.start, self.end) saver = Piavca.MotionSaver(SubMotion) saver.collectFrames(0.05) saver.save(filename)
def GetSubMotion(self): return Piavca.SubMotion(self.originalMotion, self.start, self.end)
def MotionGraph(motions, pcFile=None, fps=20, num_quants=6, threshold=1.0): if pcFile != None and pcFile != "": print "found pc file", pcFile pcs = Piavca.PCA() pcs.Load(pcfile) else: pcs = Piavca.PCA() pcs.setUseVels(True) pcs.do_analysis(motions, fps) projectedMotions = [] expmaps = [] # project all the motions onto the pcs for motion in motions: projectedMotions.append([ pw[1] for pw in pcs.projectMotion(motion, frames_per_second=fps) ]) #, file_extension="weights_out_"+ str(motion.getStart()))]) expmaps.append(pcs.getExpMaps(motion, frames_per_second=fps)) #f = open("weights_out_"+ str(motion.getStart()) +".csv", "w") #for weightset in projectedMotions[-1]: # for w in weightset: # print >> f, w, ",", # print >> f, "" #f.close() if pcs.quants == None: print "calculating cluster centres" weights = [] for projmot in projectedMotions: weights = weights + projmot weights = scipy.array(weights) quantizedWeights = pcs.KMeans(weights, num_quants) num_quants = pcs.numQuants() quants = [] for projmot in projectedMotions: quants.append(pcs.VectorQuantizeWithDistance(scipy.array(projmot))) minima = [] cluster_counts = [0 for i in range(num_quants)] for motNum, (motQuants, motDists) in enumerate(quants): minima.append([]) currentQuant = None currentMinimum = None currentMinimumVal = 1000000.0 added = False for i in range(1, len(motQuants) - 1): cluster_counts[motQuants[i]] += 1 if motQuants[i] != currentQuant: if currentQuant != None and not added: if currentMimimumVal < threshold: minima[motNum].append((currentQuant, currentMinimum)) added = False currentQuant = motQuants[i] currentMinimum = float(i) / fps currentMimimumVal = motDists[i] else: if motDists[i] < currentMinimumVal: currentMinimum = float(i) / fps currentMimimumVal = motDists[i] if motQuants[i - 1] == motQuants[i] and motQuants[i] == motQuants[i + 1]: if motDists[i - 1] > motDists[i] and motDists[i] < motDists[i + 1]: if motDists[i] < threshold: minima[motNum].append((motQuants[i], float(i) / fps)) added = True if currentQuant != None and not added: minima[motNum].append((currentQuant, currentMinimum)) # create the motions random_choices = [Piavca.RandomChoiceMotion() for i in range(num_quants)] #for i, rc in enumerate(random_choices): # rc.setName("Random_choice_"+str(i)) transitions = [] print len(minima) print "" num_transitions = [[0 for i in range(num_quants)] for j in range(num_quants)] for motNum, mot_minima in enumerate(minima): for start, end in zip(mot_minima[:-1], mot_minima[1:]): print start, end start_node = start[0] end_node = end[0] num_transitions[start_node][end_node] += 1 submot = Piavca.SubMotion(motions[motNum], start[1], end[1]) submot.setName( str(motNum) + "_" + str(start[1]) + "_" + str(end[1])) transitions.append((submot.getName(), end_node)) random_choices[start_node].addMotion(submot) print transitions[-1], random_choices[start_node].getNumMotions() for i, rc in enumerate(random_choices): print "random choice", i, rc.getNumMotions() #mo_graph = Piavca.MotionGraph() #mo_graph.addEvent("default") #for rc in random_choices: # mo_graph.addMotion(rc) #for motName, node in transitions: # print motName, type(motName), node, type(node) # mo_graph.addNextNode("default", motName, int(node)) mo_graph = Piavca.EventMapChoice() mo_graph.setResetOnEvent(False) # set the mo_graph as a listener for all random choices so that it # receieves their events for rc in random_choices: rc.addListener(mo_graph) # add the random choices to the motion graph # include an intermediary choice motion which handles # events that change the probabilities at a node for rc in random_choices: eventChoice = Piavca.ChoiceMotion() eventChoice.setResetOnPlay(True) rc.setName("default") eventChoice.addMotion(rc) mo_graph.addMotion(eventChoice) for motName, node in transitions: print motName, type(motName), node, type(node) mo_graph.addMapItem(motName, int(node)) for i in range(num_quants): for j in range(num_quants): print "tranisitions", i, j, num_transitions[i][j] #mo_graph.setSmooth(False) mo_graph.setAccumulateRoot(False) loop = Piavca.LoopMotion(mo_graph) return loop