Free Motion Capture for Everyone πβ¨
TypeScript
Reworked data handler and Maya tools. Now Python 3 native
Jakob Welnerd551fd92df7f
diff --git a/freemocap/fmc_maya_tools.py b/freemocap/fmc_maya_tools.pynew file mode 100644index 00000000..41417382--- /dev/null+++ b/freemocap/fmc_maya_tools.py@@ -0,0 +1,95 @@+++import maya.cmds as cmds+import math+++def write_to_maya(fmcDataObj, slice=False, sample_by = 5):+ """+ takes an Actor dictionary dataset and and creates animated maya objects accordingly++ input:+ actor_dict: actor dictionary extracted from mmcap_datastructure+ slice: if not false should be an int tuple of len=2 defining start and end frame to write to Maya+ """+ for actor_name in fmcDataObj.list_actors():+ ### Creating point_names+ sample_length = fmcDataObj.get_actor_sample_count(actor_name)+ tracking_points = fmcDataObj.get_actor_tracking_points(actor_name)+ point_names = tracking_points.keys()++ # Create point locators+ pnt_locs = {}+ for pnt_name in point_names:+ pnt_locs[pnt_name] = cmds.spaceLocator(n=pnt_name+ "_loc")[0]++ #cmds.scale(0.2, 0.2, 0.2, list(pnt_locs.values()))+++ # Make line connector between point locators according to "parents"+ connect_lines = []+ for pnt_name in point_names:+ parents = fmcDataObj.get_point_parents(actor_name, pnt_name)+ for parent_name in parents:+ line = make_line_between(pnt_locs[pnt_name], pnt_locs[parent_name], "connectorLine_" + pnt_name + "_" + parent_name)+ connect_lines.append(line)++ # group everything under the same transform+ grp = cmds.group(list(pnt_locs.values()) + connect_lines, name="mmcap_" + actor_name + "_grp")++ # Adding position data to point locators+ if not slice == False and len(slice)==2:+ print("Slice active: using samples in range: %s:%s" % (slice[0], slice[1]))+ sample_range = range(slice[0], slice[1], sample_by)+ else:+ sample_range = range(0, sample_length, sample_by)++ progress_step = 100./len(sample_range)+ progress = 0+ last_progress = 0+ for sample_i in sample_range:+ for pnt_name in point_names:+ loc_name = pnt_locs[pnt_name]+ pos = tracking_points[pnt_name]["samples"][sample_i]+ if not math.isnan(pos[0]):+ cmds.setKeyframe(loc_name, v=pos[0], at='translateX', time=sample_i )+ cmds.setKeyframe(loc_name, v=pos[1], at='translateY', time=sample_i )+ cmds.setKeyframe(loc_name, v=pos[2], at='translateZ', time=sample_i )+ progress += progress_step+ if int(progress) > int(last_progress):+ print(str(int(progress)).zfill(2) + "%")+ last_progress = progress+++ # Reorienting base group to align with grid+ for attr in reorient_transform:+ cmds.setAttr(grp+"." + attr, reorient_transform[attr])++ cmds.playbackOptions(minTime= sample_range[0], maxTime = sample_range[-1])+++++def make_line_between(obj1, obj2, line_name):+ """+ creates a curve segment and attaches each end to obj1 and obj2 accordingly+ """+ line = cmds.curve(name=line_name, d=1, p=[(0,0,0),(0,0,0)])+ cmds.connectAttr(obj1+".t", line + ".cv[0]")+ cmds.connectAttr(obj2+".t", line + ".cv[1]")+ return line++++# Imperically derived orientation to place actor on grid in Maya+reorient_transform = {+ "translateX": 6.976,+ "translateY": -21.502,+ "translateZ": 47.516,+ "rotateX": -114.918,+ "rotateY": 4.391,+ "rotateZ":0,+ "scaleX": 0.031,+ "scaleY": 0.031,+ "scaleZ": 0.031,+ }diff --git a/freemocap/fmc_trackdata_handler.py b/freemocap/fmc_trackdata_handler.pynew file mode 100644index 00000000..16513ccd--- /dev/null+++ b/freemocap/fmc_trackdata_handler.py@@ -0,0 +1,236 @@+import numpy as np+from pathlib import Path++FILE_SUFFIX = ".fmcData"++class FmcTracDataHandler(object):+ """+ # data structure:+ {+ "header":{+ "version": str(),+ "tags": list(),+ "author": str(),+ "export_date": datetime(),+ "camera_count": int(),+ "license": str(),+ "calibration_obj": class,+ },+ "content":{+ str(actor_name): {+ "sample_count": int(),+ "tracking_points": {+ str(tracking_point_name): {"parents": list(), samples: list()}+ }+ }+ },+ }+ """++ def __init__(self):+ self.data = {"header":{}, "content":{}}++ def has_actor(self, actor_name):+ return actor_name in self.data["content"]++ def has_trackingpoint_name(self, actor_name, point_name):+ return self.has_actor(actor_name) and point_name in self.data["content"][actor_name]["tracking_points"]++ def set_point_name_mapping(self, point_name_mapping):+ self.point_name_mapping = point_name_mapping++ def set_parent_mapping(self, parent_mapping):+ self.parent_mapping = parent_mapping++ def init_actor(self, actor_name):+ if not self.has_actor(actor_name):+ self.data["content"][actor_name] = {}+ else:+ raise Exception("Actor already exist in dataset. Please remove it before re-adding")++ def init_tracking_point_name(self, actor_name, point_name):+ if not self.has_actor(actor_name):+ self.init_actor(actor_name)+ if not "tracking_points" in self.data["content"][actor_name]:+ self.data["content"][actor_name]["tracking_points"] = {}+ self.data["content"][actor_name]["tracking_points"][point_name] = {}++ def set_actor_data(self, actor_name, data_label, data):+ if not self.has_actor(actor_name):+ self.init_actor(actor_name)+ self.data["content"][actor_name][data_label] = data+++ def set_tracking_point_samples(self, actor_name, point_name, samples):+ if not self.has_trackingpoint_name(actor_name, point_name):+ self.init_tracking_point_name(actor_name, point_name)+ self.data["content"][actor_name]["tracking_points"][point_name]["samples"] = samples++ def set_tracking_point_parents(self, actor_name, point_name, parents):+ if not self.has_trackingpoint_name(actor_name, point_name):+ self.init_tracking_point_name(actor_name, point_name)+ self.data["content"][actor_name]["tracking_points"][point_name]["parents"] = parents++ def set_actor_sample_count(self, actor_name, sample_count):+ if self.has_actor(actor_name):+ self.data["content"][actor_name]["sample_count"] = sample_count+ else:+ raise ValueError("Object does not contain an actor called: %s" % actor_name)++ def set_header(key, value):+ self.data["header"][key] = value++ def set_version(self, version):+ self.set_header("version", version)++ def set_tags(self, tag_list):+ self.set_header("tags", tag_list)++ def set_author(self, author):+ self.set_header("author", author)++ def set_date(self, datetime_obj):+ self.set_header("export_date", datetime_obj)++ def set_camera_count(self, camera_count):+ self.set_header("camera_count", camera_count)++ def set_license(self, license_string):+ self.set_header("license", license_string)++ def set_calibration_obj(self, calibration_obj):+ self.set_header("calibration_obj", calibration_obj)++ def import_actor_raw_data(self, actor_label, raw_data):+ mapped_data = self.map_point_names(raw_data)+ parent_mapping = self.get_parent_mapping()++ sample_count = False+ for point_name in mapped_data:+ parent_list = []+ if point_name in parent_mapping:+ parent_list.append(parent_mapping[point_name])++ #print("Trackingpoint: %s" % point_name)+ samples = mapped_data[point_name]+ self.set_tracking_point_samples(actor_label, point_name, samples)+ self.set_tracking_point_parents(actor_label, point_name, parent_list)++ if sample_count == False:+ sample_count = len(samples)+ else:+ if not sample_count == len(samples):+ raise NotImplementedError("Tracking points have different sample count. This is not supported yet")+ else:+ sample_count = len(samples)+ self.set_actor_sample_count(actor_label, sample_count)+++ def get_data(self):+ return self.data++ def get_actor_data(self, actor_label):+ if self.has_actor(actor_name):+ return self.data["content"][actor_label]+ else:+ raise ValueError("Object has no actor named %s" % actor_label)++ def list_actors(self):+ return list(self.data["content"].keys())++ def get_actor_tracking_points(self, actor_name):+ return self.data["content"][actor_name]["tracking_points"]++ def get_point_parents(self, actor_name, point_name):+ return self.data["content"][actor_name]["tracking_points"][point_name]["parents"]++ def get_actor_sample_count(self, actor_name):+ if self.has_actor(actor_name):+ return self.data["content"][actor_name]["sample_count"]+ else:+ raise ValueError("Object does not contain an actor called: %s" % actor_name)++++++ ### OpenPose mapping+ ################################################################################+ def map_point_names(self, data):+ """+ input data of format:+ [sample_idx][point_index] = (x,y,z)++ output format+ {point_name:+ [(x,y,z), (x,y,z),...]} # list of samples+ """+ point_names = self.get_point_name_mapping()++ # initiating data container for each point in point_names, added generated names for indices beyond length of point_names+ first_sample = data[0]+ if len(first_sample) > len(point_names): # Check if there is an equal amount of names points and indices in the data+ surplus_sample_points = len(first_sample) - len(point_names)+ for i in range(surplus_sample_points):+ point_names.append("generatedName%s" % str(i).zfill(3))++ # reordering data to be ["point_name"][sample_i][x,y,z]+ data_reform = {}+ for i, point_name in enumerate(point_names):+ data_reform[point_name] = data[:,i,:]++ return data_reform++++ def get_parent_mapping(self):+ """+ Returns a dictionary that maps point names to their parent point+ """+ if not self.parent_mapping == False:+ return self.parent_mapping+ else:+ raise ValueError("No parent mapping is set. Use set_parent_mapping() to define an appropriate map")++ def get_point_name_mapping(self):+ """+ Returns a list of names corresponding to the data positions+ """+ if not self.point_name_mapping == False:+ return self.point_name_mapping+ else:+ raise ValueError("No point name mapping is set. Use set_point_name_mapping() to define an appropriate map")++++++def load_obj_from_file(file_path):+ # Loads file saved through this class+ f = Path(file_path)++ if f.is_file():+ if f.suffix.lower() == FILE_SUFFIX:+ print(f"Reading file from: {f.absolute()}")+ with open(file_path, "rb") as infile:+ data = pickle.load(infile)+ if not isinstance(data, FmcTracDataHandler):+ raise ValueError("Loaded data is not an instance of FmcTracDataHandler")+ else:+ raise ValueError("File has wrong extension. Should be '%s'" % FILE_SUFFIX)+ else:+ raise ValueError("File does not exist")++ return data+++++def save_obj_to_file(self, file_path, fmxDataObj, override=False):+ f = Path(file_path)+ f= f.with_suffix(FILE_SUFFIX)+ if f.exists() and override == False:+ raise NotImplementedError(f"File already exists, Skipping\nFilepath: {f.absolute()}")+ with open(f, 'wb') as handle:+ pickle.dump(fmxDataObj, handle, protocol=2)+ print(f"Saved file to: {f.absolute()}")diff --git a/freemocap/openpose_data_mappings.py b/freemocap/openpose_data_mappings.pynew file mode 100644index 00000000..019bd33a--- /dev/null+++ b/freemocap/openpose_data_mappings.py@@ -0,0 +1,80 @@+# child_name : parent_name+parent_mapping = {+ 'REye': 'Nose',+ 'LKnee': 'LHip',+ 'LWrist': 'LElbow',+ 'LSmallToe': 'LBigToe',+ 'RShoulder': 'Neck',+ 'LBigToe': 'LAnkle',+ 'RKnee': 'RHip',+ 'LEar': 'LEye',+ 'REar': 'REye',+ 'RBigToe': 'RAnkle',+ 'RAnkle': 'RKnee',+ 'LElbow': 'LShoulder',+ 'RElbow': 'RShoulder',+ 'LAnkle': 'LKnee',+ 'RHeel': 'RAnkle',+ 'RWrist': 'RElbow',+ 'RSmallToe': 'RBigToe',+ 'RHip': 'MidHip',+ 'Nose': 'Neck',+ 'MidHip': 'Neck',+ 'LShoulder': 'Neck',+ 'LEye': 'Nose',+ 'LHip': 'MidHip',+ 'LHeel': 'LAnkle',+ 'HandL15': 'HandL14',+ #'HandL13': 'HandL00',+ 'HandL14': 'HandL13',+ 'HandL20': 'HandL19',+ 'RWrist': 'RElbow',+ 'HandL12': 'HandL11',+ 'HandL18': 'HandL17',+ #'HandL02': 'HandL01',+ 'HandL03': 'HandL02',+ #'HandL17': 'HandL00',+ #'HandL01': 'HandL00',+ 'HandL06': 'HandL05',+ 'HandL07': 'HandL06',+ 'HandL04': 'HandL03',+ #'HandL05': 'HandL00',+ 'HandL11': 'HandL10',+ 'HandL08': 'HandL07',+ #'HandL09': 'HandL00',+ 'HandL19': 'HandL18',+ 'HandL16': 'HandL15',+ 'HandL10': 'HandL09',+ 'HandR14': 'HandR13',+ 'HandR08': 'HandR07',+ #'HandR09': 'HandR00',+ 'HandR19': 'HandR18',+ 'HandR18': 'HandR17',+ 'HandR04': 'HandR03',+ #'HandR05': 'HandR00',+ 'HandR06': 'HandR05',+ 'HandR07': 'HandR06',+ #'HandR13': 'HandR00',+ #'HandR01': 'HandR00',+ #'HandR02': 'HandR01',+ 'HandR03': 'HandR02',+ #'HandR17': 'HandR00',+ 'HandR20': 'HandR19',+ 'LWrist': 'LElbow',+ 'HandR11': 'HandR10',+ 'HandR12': 'HandR11',+ 'HandR16': 'HandR15',+ 'HandR10': 'HandR09',+ 'HandR15': 'HandR14'+}+++# point name corresponding to Open Pose list indices+point_name_mapping = [+ "Nose", "Neck", "RShoulder", "RElbow", "RWrist", "LShoulder", # limbs+ "LElbow", "LWrist", "MidHip", "RHip", "RKnee", "RAnkle", "LHip", "LKnee", # limbs+ "LAnkle", "REye", "LEye", "REar", "LEar", "LBigToe", "LSmallToe", "LHeel", # limbs+ "RBigToe", "RSmallToe", "RHeel", # limbs+ "HandR00", "HandR01", "HandR02", "HandR03", "HandR04", "HandR05", "HandR06", "HandR07", "HandR08", "HandR09", "HandR10", "HandR11", "HandR12", "HandR13", "HandR14", "HandR15", "HandR16", "HandR17", "HandR18", "HandR19", "HandR20", # Right Hand+ "HandL00", "HandL01", "HandL02", "HandL03", "HandL04", "HandL05", "HandL06", "HandL07", "HandL08", "HandL09", "HandL10", "HandL11", "HandL12", "HandL13", "HandL14", "HandL15", "HandL16", "HandL17", "HandL18", "HandL19", "HandL20", # Left Hand+]