Python plyfile.PlyData.read() Examples

The following are 30 code examples of plyfile.PlyData.read(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module plyfile.PlyData , or try the search function .
Example #1
Source File: io.py    From occupancy_flow with MIT License 6 votes vote down vote up
def load_mesh(mesh_file):
    with open(mesh_file, 'r') as f:
        str_file = f.read().split('\n')
        n_vertices, n_faces, _ = list(
            map(lambda x: int(x), str_file[1].split(' ')))
        str_file = str_file[2:]  # Remove first 2 lines

        v = [l.split(' ') for l in str_file[:n_vertices]]
        f = [l.split(' ') for l in str_file[n_vertices:]]

    v = np.array(v).astype(np.float32)
    f = np.array(f).astype(np.uint64)[:, 1:4]

    mesh = trimesh.Trimesh(vertices=v, faces=f, process=False)

    return mesh 
Example #2
Source File: pc_util.py    From H3DNet with MIT License 6 votes vote down vote up
def read_tsdf(filename):
  f = open(filename, 'rb')
  fileContent = f.read()

  A = struct.unpack("<3f", fileContent[:12])
  A = np.array(A, np.int32)
  # print(A)
  s1 = A[2]
  s2 = A[0]
  s3 = A[1]
  size = int(A[0]*A[1]*A[2])
  s = "<%df"%(int(size))
  TDF = struct.unpack(s, fileContent[12:12+size*4])

  out = np.zeros((s1, s2, s3))
  for i in range(s1):
    t = np.transpose(np.reshape(TDF[i*s2*s3: (i+1)*s2*s3], (s3, s2)))
    out[i] = t

  out = np.transpose(out, (1,2,0))

  out = out[20:-20, 20:-20, 20:-20]
  return out 
Example #3
Source File: kinect3Dcould.py    From python-urbanPlanning with MIT License 6 votes vote down vote up
def dataSource(fn):
    with open(fn, 'rb') as f:
            plydata=PlyData.read(f)
        #print(plydata,'\n',plydata['vertex'][0],'\n',plydata.elements[0].properties)        
    print(plydata) #可以通过打印,查看读取的点云数据格式,从而进一步提取所需数据。
    pointsX=plydata['vertex']['x']  #等同于plydata['vertex'][0]
    pointsY=plydata['vertex']['y']
    pointsZ=plydata['vertex']['z']
    pointsR=plydata['vertex']['red']
    pointsG=plydata['vertex']['green']
    pointsB=plydata['vertex']['blue']
    pointsXYZ=np.array([pointsX,pointsY,pointsZ])
    pointsRGB=np.array([pointsR,pointsG,pointsB])
    #[:,:1000]
    print(pointsRGB.shape,pointsXYZ.shape)
    print(pointsRGB.transpose())  
    return pointsXYZ,pointsRGB 
Example #4
Source File: plot_interpolation_3D.py    From geomloss with MIT License 6 votes vote down vote up
def load_ply_file(fname) :
    """Loads a .ply mesh to return a collection of weighted Dirac atoms: one per triangle face."""

    # Load the data, and read the connectivity information:
    plydata = PlyData.read(fname)
    triangles = np.vstack( plydata['face'].data['vertex_indices'] )

    # Normalize the point cloud, as specified by the user:
    points = np.vstack( [ [v[0],v[1],v[2]] for v in  plydata['vertex'] ] )

    return to_measure(points, triangles)
    

################################################################################
# Utility: load ".nii" volume file.
# 
Example #5
Source File: scannet_utils.py    From votenet with MIT License 6 votes vote down vote up
def read_mesh_vertices_rgb(filename):
    """ read XYZ RGB for each vertex.
    Note: RGB values are in 0-255
    """
    assert os.path.isfile(filename)
    with open(filename, 'rb') as f:
        plydata = PlyData.read(f)
        num_verts = plydata['vertex'].count
        vertices = np.zeros(shape=[num_verts, 6], dtype=np.float32)
        vertices[:,0] = plydata['vertex'].data['x']
        vertices[:,1] = plydata['vertex'].data['y']
        vertices[:,2] = plydata['vertex'].data['z']
        vertices[:,3] = plydata['vertex'].data['red']
        vertices[:,4] = plydata['vertex'].data['green']
        vertices[:,5] = plydata['vertex'].data['blue']
    return vertices 
Example #6
Source File: pc_util.py    From Pointnet2.ScanNet with MIT License 6 votes vote down vote up
def read_ply_xyzrgbnormal(filename):
    """ read XYZ RGB normals point cloud from filename PLY file """
    assert(os.path.isfile(filename))
    with open(filename, 'rb') as f:
        plydata = PlyData.read(f)
        num_verts = plydata['vertex'].count
        vertices = np.zeros(shape=[num_verts, 9], dtype=np.float32)
        vertices[:,0] = plydata['vertex'].data['x']
        vertices[:,1] = plydata['vertex'].data['y']
        vertices[:,2] = plydata['vertex'].data['z']
        vertices[:,3] = plydata['vertex'].data['red']
        vertices[:,4] = plydata['vertex'].data['green']
        vertices[:,5] = plydata['vertex'].data['blue']

        # compute normals
        xyz = np.array([[x, y, z] for x, y, z, _, _, _, _ in plydata["vertex"].data])
        face = np.array([f[0] for f in plydata["face"].data])
        nxnynz = compute_normal(xyz, face)
        vertices[:,6:] = nxnynz
    return vertices 
Example #7
Source File: dataset.py    From pointnet.pytorch with MIT License 6 votes vote down vote up
def __getitem__(self, index):
        fn = self.fns[index]
        cls = self.cat[fn.split('/')[0]]
        with open(os.path.join(self.root, fn), 'rb') as f:
            plydata = PlyData.read(f)
        pts = np.vstack([plydata['vertex']['x'], plydata['vertex']['y'], plydata['vertex']['z']]).T
        choice = np.random.choice(len(pts), self.npoints, replace=True)
        point_set = pts[choice, :]

        point_set = point_set - np.expand_dims(np.mean(point_set, axis=0), 0)  # center
        dist = np.max(np.sqrt(np.sum(point_set ** 2, axis=1)), 0)
        point_set = point_set / dist  # scale

        if self.data_augmentation:
            theta = np.random.uniform(0, np.pi * 2)
            rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
            point_set[:, [0, 2]] = point_set[:, [0, 2]].dot(rotation_matrix)  # random rotation
            point_set += np.random.normal(0, 0.02, size=point_set.shape)  # random jitter

        point_set = torch.from_numpy(point_set.astype(np.float32))
        cls = torch.from_numpy(np.array([cls]).astype(np.int64))
        return point_set, cls 
Example #8
Source File: ioUtils.py    From director with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def readPlyFile(filename):
    """
    Usese plyfile python pacakage to read a ply file.
    Gets around issues with pcl having a bad ply writer for pointclouds
    :param filename:
    :type filename: str
    :return: vtkPolyData
    :rtype:
    """

    from plyfile import PlyData

    plydata = PlyData.read(filename)
    vertex_data = plydata['vertex'].data # numpy array with fields ['x', 'y', 'z']
    pts = np.zeros([vertex_data.size, 3])
    pts[:, 0] = vertex_data['x']
    pts[:, 1] = vertex_data['y']
    pts[:, 2] = vertex_data['z']

    return vnp.numpyToPolyData(pts) 
Example #9
Source File: stanford.py    From SpatioTemporalSegmentation with MIT License 6 votes vote down vote up
def load_file(file_name, voxel_size):
  plydata = PlyData.read(file_name)
  data = plydata.elements[0].data
  coords = np.array([data['x'], data['y'], data['z']], dtype=np.float32).T
  colors = np.array([data['red'], data['green'], data['blue']], dtype=np.float32).T / 255
  labels = np.array(data['label'], dtype=np.int32)

  # Generate input pointcloud
  pcd = o3d.geometry.PointCloud()
  pcd.points = o3d.utility.Vector3dVector(coords)
  pcd.colors = o3d.utility.Vector3dVector(colors)

  # Normalize feature
  norm_coords = coords - coords.mean(0)
  feats = np.concatenate((colors - 0.5, norm_coords), 1)

  coords, feats, labels = ME.utils.sparse_quantize(
      coords, feats, labels, quantization_size=voxel_size)

  return coords, feats, labels, pcd 
Example #10
Source File: meshutil.py    From DenseHumanBodyCorrespondences with The Unlicense 6 votes vote down vote up
def load_ply_mesh(mesh_path):
    data = PlyData.read(mesh_path)

    vertex_data = data['vertex'].data
    vertices = np.zeros([vertex_data.shape[0], 3], dtype=np.float32)
    for i in range(vertices.shape[0]):
        for j in range(3):
            vertices[i, j] = vertex_data[i][j]

    face_data = data['face'].data
    faces = np.zeros([face_data.shape[0], 3], dtype=np.int32)
    for i in range(faces.shape[0]):
        for j in range(3):
            faces[i, j] = face_data[i][0][j]

    return vertices, faces 
Example #11
Source File: io.py    From occupancy_networks with MIT License 5 votes vote down vote up
def load_pointcloud(in_file):
    plydata = PlyData.read(in_file)
    vertices = np.stack([
        plydata['vertex']['x'],
        plydata['vertex']['y'],
        plydata['vertex']['z']
    ], axis=1)
    return vertices 
Example #12
Source File: pc_util.py    From PointCNN.Pytorch with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #13
Source File: pc_util.py    From path_invariance_map_network with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #14
Source File: InputOutput.py    From DDGSpring2016 with MIT License 5 votes vote down vote up
def readMesh_PLY(filename, output="soup"):

    if(output != 'soup'):
        raise Exception('Mesh types other than soup not yet supported')

    # Read the actual file
    # TODO This takes a long time, maybe try to replace with something faster of my own?
    plydata = PlyData.read(filename)

    # Read vertices
    # If the mesh has more than three columns of vertex data, ignore the later columns
    # (for instance, Stanford Mesh Repo meshes store intensity and confidence here)
    nVerts = plydata['vertex'].count
    verts = np.zeros((nVerts,3))
    verts[:,0] = np.array(plydata['vertex'].data['x'])
    verts[:,1] = np.array(plydata['vertex'].data['y'])
    verts[:,2] = np.array(plydata['vertex'].data['z'])


    # Read faces
    faces = make2d(plydata['face'].data['vertex_indices'])

    # Build a mesh from these vertices and faces
    mesh = TriSoupMesh(verts, faces)

    return mesh


# Read a .ply mesh
# TODO Ignores everything execpt for vertex position and face indices
# TODO Will only process triangular vertices 
Example #15
Source File: pc_util.py    From dfc2019 with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #16
Source File: pc_util.py    From JSNet with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x, y, z in pc])
    return pc_array 
Example #17
Source File: pc_util.py    From pcrnet with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #18
Source File: pc_util.py    From deep_gcns with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #19
Source File: pc_util.py    From scanobjectnn with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #20
Source File: pc_util.py    From scanobjectnn with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #21
Source File: pc_util.py    From scanobjectnn with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #22
Source File: pc_util.py    From dfc2019 with MIT License 5 votes vote down vote up
def read_ply_xyzrgb(filename):
    """ read XYZRGB point cloud from filename PLY file """
    assert(os.path.isfile(filename))
    with open(filename, 'rb') as f:
        plydata = PlyData.read(f)
        num_verts = plydata['vertex'].count
        vertices = np.zeros(shape=[num_verts, 6], dtype=np.float32)
        vertices[:,0] = plydata['vertex'].data['x']
        vertices[:,1] = plydata['vertex'].data['y']
        vertices[:,2] = plydata['vertex'].data['z']
        vertices[:,3] = plydata['vertex'].data['red']
        vertices[:,4] = plydata['vertex'].data['green']
        vertices[:,5] = plydata['vertex'].data['blue']
    return vertices 
Example #23
Source File: pc_util.py    From dfc2019 with MIT License 5 votes vote down vote up
def read_ply_xyz(filename):
    """ read XYZ point cloud from filename PLY file """
    assert(os.path.isfile(filename))
    with open(filename, 'rb') as f:
        plydata = PlyData.read(f)
        num_verts = plydata['vertex'].count
        vertices = np.zeros(shape=[num_verts, 3], dtype=np.float32)
        vertices[:,0] = plydata['vertex'].data['x']
        vertices[:,1] = plydata['vertex'].data['y']
        vertices[:,2] = plydata['vertex'].data['z']
    return vertices 
Example #24
Source File: util.py    From fully-convolutional-point-network with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ Reads a PLY file from disk.

    Args:
    filename: string
    
    Returns: np.array, np.array, np.array

    """

    file = open(filename, 'rb')
    plydata = PlyData.read(file)

    points = np.stack((plydata['vertex']['x'], plydata['vertex'][
                      'y'], plydata['vertex']['z'])).transpose()

    try:
        labels = plydata['vertex']['label']
    except:
        labels = np.array([])

    try:
        faces = np.array(plydata['face'].data['vertex_indices'].tolist())
    except:
        faces = np.array([])

    file.close()

    return points, labels, faces 
Example #25
Source File: pc_util.py    From dfc2019 with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #26
Source File: pc_util.py    From dfc2019 with MIT License 5 votes vote down vote up
def read_ply(filename):
    """ read XYZ point cloud from filename PLY file """
    plydata = PlyData.read(filename)
    pc = plydata['vertex'].data
    pc_array = np.array([[x, y, z] for x,y,z in pc])
    return pc_array 
Example #27
Source File: base_utils.py    From pvnet-rendering with Apache License 2.0 5 votes vote down vote up
def load_ply_model(model_path):
        ply = PlyData.read(model_path)
        data = ply.elements[0].data
        x = data['x']
        y = data['y']
        z = data['z']
        return np.stack([x, y, z], axis=-1) 
Example #28
Source File: base_utils.py    From pvnet-rendering with Apache License 2.0 5 votes vote down vote up
def load_ply_model(model_path):
        ply = PlyData.read(model_path)
        data = ply.elements[0].data
        x = data['x']
        y = data['y']
        z = data['z']
        return np.stack([x, y, z], axis=-1) 
Example #29
Source File: fuse.py    From pvnet-rendering with Apache License 2.0 5 votes vote down vote up
def load_ply_model(model_path):
        ply = PlyData.read(model_path)
        data = ply.elements[0].data
        x = data['x']
        y = data['y']
        z = data['z']
        return np.stack([x, y, z], axis=-1) 
Example #30
Source File: dataset.py    From SpatioTemporalSegmentation with MIT License 5 votes vote down vote up
def load_ply(self, index):
    filepath = self.data_root / self.data_paths[index]
    plydata = PlyData.read(filepath)
    data = plydata.elements[0].data
    coords = np.array([data['x'], data['y'], data['z']], dtype=np.float32).T
    feats = np.array([data['red'], data['green'], data['blue']], dtype=np.float32).T
    labels = np.array(data['label'], dtype=np.int32)
    return coords, feats, labels, None