def main(pathToMainDataset, dog, motion, cameraFrame):


	pathToMotion = os.path.join(pathToMainDataset, dog, 'motion_%s'%motion)

	bvhSkelFile = os.path.join(pathToMotion, 'motion_capture', 'skeleton.bvh')
	skelConnections_asNp = os.path.join(pathToMotion, 'motion_capture', 'skelConnections.npy')
	bvhSkelFile_asNp = bvhSkelFile[:bvhSkelFile.rfind('.')] + '.npy'
	# ----------------------------------- load the skeleton -----------------------------
	print('loading skeleton...')
	if os.path.isfile(bvhSkelFile_asNp):
		bvhJoints = np.load(bvhSkelFile_asNp)
		skelConnections = np.load(skelConnections_asNp)
	else:
		bvhJoints, skelConnections, nodes = utils.ReadBvhFile(bvhSkelFile, False)
		bvhJoints = utils.MovePointsOutOfMayaCoordSystem(bvhJoints, 1)
		np.save(bvhSkelFile_asNp, bvhJoints)
		np.save(skelConnections_asNp, skelConnections)

	bvhJoints = bvhJoints[:,:,cameraFrame+1]
	
	
	
	markerFile = os.path.join(pathToMotion, 'motion_capture', 'markers.json')
	markerFile_asNp = markerFile[:markerFile.rfind('.')] + '.npy'
	markerFile_namesText = markerFile[:markerFile.rfind('.')] + '_names.txt'
	# ----------------------------------- load the markers -----------------------------
	print('loading markers...')
	if os.path.isfile(markerFile_asNp):
		markers = np.load(markerFile_asNp)
		with open(markerFile_namesText, 'r') as f:
			markerNames_joined = f.read()
		markerNames = markerNames_joined.split(',')
	else:
		[markerNames, markers] = utils.GetPointsFromJsonFbx(markerFile)
		markers = utils.MovePointsOutOfMayaCoordSystem(markers)
		np.save(markerFile_asNp, markers)
		markerNames_joined = ','.join(markerNames)
		with open(markerFile_namesText, 'w') as f:
			f.write(markerNames_joined)
	markers = markers[:,:,cameraFrame]	
	
	# ----------------------------------- load the stick file that defines the relationship between the markers -----------------------------
	# use the vskSticks.txt file to get the connection between markers
	vskSticks = os.path.join(pathToMainDataset, dog, 'meta', 'vskSticks.txt')
	
	markerConn = []
	with open(vskSticks, 'r') as myFile:
		for f in myFile:
			res = [x.strip() for x in f.split(' ')]
			#convert from name to index
			markerConn.append([markerNames.index(res[0]), markerNames.index(res[1])])
				
				
	fig = plt.figure(); ax = fig.add_subplot(111, projection='3d') 
	ax, fig = utils.Plot3d(bvhJoints, connections=skelConnections, style='bo-', ax=ax, jointSpecificClrs=utils.GetDefaultColours())
	ax, fig = utils.Plot3d(markers, connections=markerConn, style='ko--', ax=ax, differentColoursForSides=True)
	ax.set_xlabel('x');ax.set_ylabel('y');ax.set_aspect('auto');
	plt.title('markers in black, skeleton in colour'); plt.show()
示例#2
0
def main(pathToMainDataset, dog, motion, cam, cameraFrame=0):


	pathToMotion = os.path.join(pathToMainDataset, dog, 'motion_%s'%motion)

	# ----------------------------------- load the calibration -----------------------------
	cameraType='kinect_depth'
	pathToCalib = os.path.join(pathToMainDataset, dog, 'calibration', cameraType)#, 'calibFile%s'%cam)
	calib = utils.GetCalibData(pathToCalib, cam)

	# ----------------------------------- load the image -----------------------------
	frame = None
	reconPoints = None
	scaleAppliedAfterProjection = 1
	pathToImages = os.path.join(pathToMotion, cameraType, 'camera%s'%cam, 'images')
	startingImageName = '%s_%08d' % (cam, cameraFrame)
	imName = utils.GetFilesInFolder(pathToImages, contains=startingImageName)
	assert len(imName) > 0, 'no images found for frame %d' % cameraFrame

	lineThickness_forPlotting = 2
	frame = cv2.imread(os.path.join(pathToImages, imName[0]),-1)

	bvhSkelFile = os.path.join(pathToMotion, 'motion_capture', 'skeleton.bvh')
	skelConnections_asNp = os.path.join(pathToMotion, 'motion_capture', 'skelConnections.npy')
	bvhSkelFile_asNp = bvhSkelFile[:bvhSkelFile.rfind('.')] + '.npy'

	# ----------------------------------- load the skeleton -----------------------------
	print('loading skeleton...')
	if os.path.isfile(bvhSkelFile_asNp):
		bvhJoints = np.load(bvhSkelFile_asNp)
		skelConnections = np.load(skelConnections_asNp)
	else:
		bvhJoints, skelConnections, nodes = utils.ReadBvhFile(bvhSkelFile, False)
		bvhJoints = utils.MovePointsOutOfMayaCoordSystem(bvhJoints, 1)
		np.save(bvhSkelFile_asNp, bvhJoints)
		np.save(skelConnections_asNp, skelConnections)

	bvhJoints = bvhJoints[:,:,cameraFrame+1]
	reconPoints = utils.ReconstructDepthImage(frame, calib['K'])
	sampleRate = 30 # for speed
	reconPoints = reconPoints[::sampleRate,:]
	# move from camera space to world space
	reconPoints = np.hstack((reconPoints,np.ones((reconPoints.shape[0],1))))
	# L moves 3D points from world space to camera space -> inv(L) moves points from camera space to world space
	
	L = np.array(calib['calib']['L'])
	invL = np.linalg.inv(L)
	numPointsRecon = reconPoints.shape[0]
	reconPoints_worldSpace = reconPoints.dot(invL.T)
	reconPoints_worldSpace = reconPoints_worldSpace[:,0:3]/ np.reshape(reconPoints_worldSpace[:,3], (numPointsRecon, 1)) # divide by the last column
	

	fig = plt.figure(); ax = fig.add_subplot(111, projection='3d') 
	ax.plot(reconPoints_worldSpace[:,0], reconPoints_worldSpace[:,1], reconPoints_worldSpace[:,2], 'ro', markersize=3)
	ax, fig = utils.Plot3d(bvhJoints, connections=skelConnections, style='bo-', ax=ax, differentColoursForSides=True, markerSize=3)
	ax.set_xlabel('x');ax.set_ylabel('y');ax.set_aspect('auto');plt.show()
	reconPoints = reconPoints_worldSpace
示例#3
0
def main():

    modelPath = os.path.join(datasetFolder, 'shapeModel', 'dynaDog_v1.0.p')

    print('loading model from', modelPath)
    dynaDog = DynaDog(modelPath)

    # shape parameters
    beta = np.zeros(dynaDog.beta_shape)

    # pose parameters
    pose = np.zeros(dynaDog.pose_shape)
    rootTrans = np.zeros(dynaDog.trans_shape)
    shldr = np.zeros(dynaDog.shoulderEarTrans_shape)
    '''
	# Other examples:
	# dynaDog.set_params()
	
	# or 
	
	# read animation from bvh file. NOTE the translation for the shoulder and ears is not currently included in this feature
	applyMotionFrom_dog = 'dog2'
	applyMotionFrom_motion = 'trot'
	applyMotionFrom_frame = 1
	pathToBvh = os.path.join(datasetFolder, applyMotionFrom_dog, 'motion_%s'%applyMotionFrom_motion, 'motion_capture', 'skeleton.bvh')
	bvhPoints, connections, nodes = utils.ReadBvhFile(pathToBvh)
	pose = utils.GetLocalRotationsForFrame(nodes, applyMotionFrom_frame, asType='rodrigues')
	'''

    dynaDog.set_params(beta=beta,
                       pose=pose,
                       rootTrans=rootTrans,
                       shoulderEarTrans=shldr)

    # save to obj file
    # dynaDog.save_to_obj('full/path/to/file.obj')

    # plot the dog in the animated pose
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(dynaDog.verts[:, 0],
               dynaDog.verts[:, 1],
               dynaDog.verts[:, 2],
               c="limegreen")
    ax, fig = utils.Plot3d(dynaDog.J,
                           style='bo-',
                           ax=ax,
                           differentColoursForSides=True)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_aspect('auto')
    plt.show()

    # plot the dog in the neutral standing pose
    '''
示例#4
0
def main(pathToMainDataset, dog, motion, cameraFrame):

    skelFrame = cameraFrame + 1

    pathToMotion = os.path.join(pathToMainDataset, dog, 'motion_%s' % motion)
    bvhSkelFile = os.path.join(pathToMotion, 'motion_capture', 'skeleton.bvh')
    skelConnections_asNp = os.path.join(pathToMotion, 'motion_capture',
                                        'skelConnections.npy')
    skelNodes_asPck = os.path.join(pathToMotion, 'motion_capture',
                                   'skelNodes.pkl')
    bvhSkelFile_asNp = bvhSkelFile[:bvhSkelFile.rfind('.')] + '.npy'

    # ----------------------------------- load the skeleton -----------------------------
    if os.path.isfile(bvhSkelFile_asNp):
        bvhJoints = np.load(bvhSkelFile_asNp)
        skelConnections = np.load(skelConnections_asNp)
        with open(skelNodes_asPck, 'rb') as f:
            nodes = pickle.load(f)
    else:
        bvhJoints, skelConnections, nodes = utils.ReadBvhFile(
            bvhSkelFile, False)
        bvhJoints = utils.MovePointsOutOfMayaCoordSystem(bvhJoints, 1)

        with open(skelNodes_asPck, 'wb') as f:
            pickle.dump(nodes, f)
        np.save(bvhSkelFile_asNp, bvhJoints)
        np.save(skelConnections_asNp, skelConnections)

    # load skinning weights and neutral mesh
    pathToSkinningWeights = os.path.join(pathToMainDataset, dog, 'meta',
                                         'skinningWeights.mat')
    weights = utils.LoadMatFile(pathToSkinningWeights)

    pathToObj = os.path.join(pathToMainDataset, dog, 'meta', 'neutralMesh.obj')
    meshObj = utils.LoadObjFile(pathToObj, rotate=True)
    verts_neutral = meshObj['vertices']

    J_neutral = bvhJoints[:, :,
                          0]  # the first frame of the bvh file is the neutral skeleton
    numJoints = J_neutral.shape[0]

    # rotate about z, to match dynaDog_np.py
    verts_neutral = verts_neutral[:, np.array((1, 0, 2))]
    verts_neutral[:, 0] *= -1
    J_neutral = J_neutral[:, np.array((1, 0, 2))]
    J_neutral[:, 0] *= -1

    # the world positions of the joints in this frame
    bvhJoints = bvhJoints[:, :, skelFrame]
    bvhJoints = bvhJoints[:, np.array((1, 0, 2))]
    bvhJoints[:, 0] *= -1

    root_trans = bvhJoints[0]

    world_rot = utils.GetWorldRotationsForFrame(nodes, skelFrame, 'rodrigues')

    G = np.zeros((numJoints, 4, 4))
    # to get the correct rotations, we have to rearrange the axis
    for jntIdx, rot in enumerate(world_rot):
        G[jntIdx, 0:3, 0:3] = Rsci.from_rotvec(rot).as_dcm()

        # remove root translation
        G[jntIdx, 0:3, 3] = bvhJoints[jntIdx] - bvhJoints[0]
        G[jntIdx, 3, 3] = 1

    # remove the neutral pose from the transformations
    G = G - utils.pack(
        np.matmul(
            G,
            np.hstack([J_neutral, np.zeros([numJoints, 1])]).reshape(
                [numJoints, 4, 1])))

    # transform each vertex
    T = np.tensordot(weights, G, axes=[[1], [0]])
    rest_shape_h = np.hstack(
        (verts_neutral, np.ones([verts_neutral.shape[0], 1])))
    # T.shape (2426, 4, 4) rest_shape_h.shape (2426, 4)
    v = np.matmul(T, rest_shape_h.reshape([-1, 4, 1])).reshape([-1, 4])[:, :3]

    # apply root translation
    verts = v + root_trans.reshape([1, 3])

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax, fig = utils.Plot3d(bvhJoints,
                           connections=skelConnections,
                           style='bo-',
                           ax=ax,
                           differentColoursForSides=False)
    ax.plot(verts[:, 0], verts[:, 1], verts[:, 2], 'go', markersize=2)
    plt.title('skeleton and mesh with skinning applied')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_aspect('auto')
    plt.show()
示例#5
0
                       pose=pose,
                       rootTrans=rootTrans,
                       shoulderEarTrans=shldr)

    # save to obj file
    # dynaDog.save_to_obj('full/path/to/file.obj')

    # plot the dog in the animated pose
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(dynaDog.verts[:, 0],
               dynaDog.verts[:, 1],
               dynaDog.verts[:, 2],
               c="limegreen")
    ax, fig = utils.Plot3d(dynaDog.J,
                           style='bo-',
                           ax=ax,
                           differentColoursForSides=True)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_aspect('auto')
    plt.show()

    # plot the dog in the neutral standing pose
    '''
	fig = plt.figure(); ax = fig.add_subplot(111, projection='3d') 
	ax.scatter(dynaDog.verts_neutral[:,0], dynaDog.verts_neutral[:,1], dynaDog.verts_neutral[:,2], c="limegreen")
	ax, fig = utils.Plot3d(dynaDog.J_neutral, style='bo-', ax=ax, differentColoursForSides=True)
	ax.set_xlabel('x');ax.set_ylabel('y');ax.set_aspect('auto');plt.show()
	'''
示例#6
0
def main(pathToMainDataset, dog, motion, cameraFrame, PATH_TO_SMAL):

    assert dog not in [
        'dog6', 'dog7'
    ], 'barycentric variables are not available for these dogs'

    bary_file = os.path.join(pathToMainDataset, dog, 'meta',
                             'SMAL_barycen.mat')
    assert os.path.isfile(
        bary_file
    ), 'file could not be located. Please ensure you have the latest version of the dog\'s meta folder'

    barycen = utils.LoadMatFile(bary_file)
    barycen_neighVertIds = utils.LoadMatFile(
        os.path.join(pathToMainDataset, dog, 'meta',
                     'SMAL_barycen_neighVertIds.mat'))
    barycen_neighVertIds -= 1  # created in Matlab, so start index from 0
    numVertsInSmalMesh = barycen.shape[0]

    # get the mesh and joint locations for the specified frame
    verts, bvhJoints = test_skinning.main(pathToMainDataset,
                                          dog,
                                          motion,
                                          cameraFrame,
                                          plotMesh=False)

    # get SMAL vertex locations from the mesh we've generated
    verts_smal = np.zeros((numVertsInSmalMesh, 3))
    for v in range(numVertsInSmalMesh):
        nn = barycen_neighVertIds[v]
        barycen_currFace = barycen[v]
        nn_location = np.transpose(verts[nn])
        verts_smal[v] = nn_location.dot(barycen_currFace)

    # use the SMAL joint regressor to get the 3D joint locations
    assert os.path.isfile(
        PATH_TO_SMAL), 'invalid location for the SMAL pickle file'
    with open(PATH_TO_SMAL, 'rb') as f:
        smalData = pickle.load(f, encoding='latin1')
    J_regressor = smalData['J_regressor']
    skelConnections = smalData['kintree_table'][0]
    joint_locations = J_regressor.dot(verts_smal)

    # plot
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax, fig = utils.Plot3d(joint_locations,
                           connections=skelConnections,
                           style='bo-',
                           ax=ax,
                           differentColoursForSides=False)
    ax.plot(verts_smal[:, 0],
            verts_smal[:, 1],
            verts_smal[:, 2],
            'go',
            markersize=2)
    plt.title('skeleton and mesh with skinning applied')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_aspect('auto')
    plt.show()