Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load_from_ply(self, fp):
scene_name = Path(fp).with_suffix('').name
try:
plydata = PlyData.read(fp)
if plydata['vertex'].count == 0:
return Building()
cor = np.vstack((plydata['vertex']['x'],
plydata['vertex']['y'],
plydata['vertex']['z'])).transpose()
building_model = Building()
building_model.scene_name = scene_name
face_name = plydata['face'].data.dtype.names[0]
for face_index in plydata['face'].data[face_name]:
face_cor = cor[face_index]
building_model.add_topsurface(Surface(face_cor))
return building_model
except:
cor, f = ply_parser(fp)
def load_ply_data(filename, point_num):
plydata = PlyData.read(filename)
pc = plydata['vertex'].data[:point_num]
pc_array = np.array([[x, y, z] for x,y,z in pc])
return pc_array
if save_ply:
ply_dir = out_root + "/ply/" + spaceid + scanid + "/"
if not os.path.exists(ply_dir):
print(ply_dir, "Not Exists! Create", ply_dir)
os.makedirs(ply_dir)
ply_file = scene_path + "/scene" + sceneid + "_vh_clean_2.ply"
jsonflie = scene_path + "/scene" + sceneid + "_vh_clean_2.0.010000.segs.json"
aggjsonfile = scene_path + "/scene" + sceneid + ".aggregation.json"
# Read ply file
print("\nRead ply file:", ply_file)
plydata = PlyData.read(ply_file).elements[0].data
pts_num = len(plydata)
print("points num:", pts_num)
# Read json file
print("Read json file:", jsonflie)
json_data = json.load(open(jsonflie))
# check json file
if json_data['sceneId'].strip() == ('scene' + sceneid):
segIndices = json_data['segIndices']
seg_num = len(segIndices)
# check num
if seg_num != pts_num:
print("seg num != pts num!")
def load_shape(filename, load_triangles = False):
mesh = plyfile.PlyData.read(filename)
# convert vertices to numpy array
vertices = np.transpose(np.vstack((mesh['vertex']['x'],mesh['vertex']['y'],mesh['vertex']['z'])))
# get triangles
if load_triangles:
tridata = mesh['face'].data['vertex_indices']
triangles = plyfile.make2d(tridata)
return vertices, triangles
return vertices
Paths = ['./3DFace/Probe', './3DFace/Gallery']
for sDefaultPath in Paths:
sSavePath = sDefaultPath
x0 = 0;
y0 = 0;
sDefaultPath = sSavePath
dir = [f for f in listdir(sDefaultPath) if isfile(join(sDefaultPath, f)) and (f.endswith('.ply'))]
for sFilePath in dir:
sPointCloud = PlyData.read(sDefaultPath + '/' + sFilePath)
sPointCloud = sPointCloud['vertex'][:]
sPointCloudVerts = [list(row)[:3] for row in sPointCloud]
sPointCloudVerts = np.array(sPointCloudVerts)
normalizationFactor = 100.0
normalizedVerts_ = []
meanX = np.mean(sPointCloudVerts[:, 0])
meanY = np.mean(sPointCloudVerts[:, 1])
meanZ = np.mean(sPointCloudVerts[:, 2])
for v in sPointCloudVerts:
v[0] = (v[0] - meanX)/normalizationFactor
v[1] = (v[1] - meanY)/normalizationFactor
v[2] = (v[2] - meanZ)/normalizationFactor
normalizedVerts_.append(v)
def readMesh(scene_id):
filename = ROOT_FOLDER + scene_id + '/' + scene_id + '.aggregation.json'
data = json.load(open(filename, 'r'))
aggregation = np.array(data['segGroups'])
high_res = False
if high_res:
filename = ROOT_FOLDER + scene_id + '/' + scene_id + '_vh_clean.labels.ply'
else:
filename = ROOT_FOLDER + scene_id + '/' + scene_id + '_vh_clean_2.labels.ply'
pass
plydata = PlyData.read(filename)
vertices = plydata['vertex']
points = np.stack([vertices['x'], vertices['y'], vertices['z']], axis=1)
faces = np.array(plydata['face']['vertex_indices'])
semanticSegmentation = vertices['label']
if high_res:
filename = ROOT_FOLDER + scene_id + '/' + scene_id + '_vh_clean.segs.json'
else:
filename = ROOT_FOLDER + scene_id + '/' + scene_id + '_vh_clean_2.0.010000.segs.json'
pass
data = json.load(open(filename, 'r'))
segmentation = np.array(data['segIndices'])
def read_scene_pc(file_path):
with open(file_path, 'rb') as f:
plydata = PlyData.read(f)
dtype = plydata['vertex'].data.dtype
print('dtype of file{}: {}'.format(file_path, dtype))
points_data = np.array(plydata['vertex'].data.tolist())
return points_data
def read_ply_pcl(filename):
plydata = plyfile.PlyData.read(filename)
v = np.vstack((plydata['vertex']['x'], plydata['vertex']['y'], plydata['vertex']['z'])).T
normals = np.vstack((plydata['vertex']['nx'], plydata['vertex']['ny'], plydata['vertex']['nz'])).T
values = plydata['vertex']['value'].T
confidence = plydata['vertex']['confidence'].T
ret = {
'v': v,
'confidence': confidence,
'normals': normals,
'value': values,
}
if 'face' in plydata:
ret['f'] = plyfile.make2d(plydata['face'].data['vertex_indices'])
return ret
def load(self, path, demean=False, scale=1.0):
data = PlyData.read(path)
self.vertices = np.zeros((data['vertex'].count, 3))
self.vertices[:, 0] = np.array(data['vertex']['x'])
self.vertices[:, 1] = np.array(data['vertex']['y'])
self.vertices[:, 2] = np.array(data['vertex']['z'])
self.vertices *= scale
self.centroid = np.mean(self.vertices, 0)
if demean:
self.centroid = np.zeros((1, 3), np.float32)
self.vertices -= self.centroid
self._compute_bbox()
self.indices = np.asarray(list(data['face']['vertex_indices']), np.uint32)
# Look for texture map as jpg or png
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