@@ -1825,7 +1825,7 @@ def DL_inpaint_edge(mesh,
18251825 return mesh , info_on_pix , specific_mask_nodes , new_edge_ccs , connnect_points_ccs , np_image
18261826
18271827
1828- def write_ply (image ,
1828+ def write_mesh (image ,
18291829 depth ,
18301830 int_mtx ,
18311831 ply_name ,
@@ -2076,13 +2076,15 @@ def write_ply(image,
20762076 obj_name = basename + '.obj'
20772077 print ("Writing mesh file %s ..." % obj_name )
20782078 with open (obj_name , 'w' ) as obj_fi :
2079- obj_fi .write (('# H ' + str (int (input_mesh .graph ['H' ])) + '\n ' ))
2080- obj_fi .write (('# W ' + str (int (input_mesh .graph ['W' ])) + '\n ' ))
2081- obj_fi .write (('# hFov ' + str (float (input_mesh .graph ['hFov' ])) + '\n ' ))
2082- obj_fi .write (('# vFov ' + str (float (input_mesh .graph ['vFov' ])) + '\n ' ))
2083- obj_fi .write (('# meanLoc ' + str (float (mean_loc_depth )) + '\n ' ))
2084- obj_fi .write (('# vertices ' + str (len (node_str_list )) + '\n ' ))
2085- obj_fi .write (('# faces ' + str (len (str_faces )) + '\n ' ))
2079+ obj_fi .write ('# depthmap-script\n ' )
2080+ obj_fi .write ('# H ' + str (int (input_mesh .graph ['H' ])) + '\n ' )
2081+ obj_fi .write ('# W ' + str (int (input_mesh .graph ['W' ])) + '\n ' )
2082+ obj_fi .write ('# hFov ' + str (float (input_mesh .graph ['hFov' ])) + '\n ' )
2083+ obj_fi .write ('# vFov ' + str (float (input_mesh .graph ['vFov' ])) + '\n ' )
2084+ obj_fi .write ('# meanLoc ' + str (float (mean_loc_depth )) + '\n ' )
2085+ obj_fi .write ('# vertices ' + str (len (node_str_list )) + '\n ' )
2086+ obj_fi .write ('# faces ' + str (len (str_faces )) + '\n ' )
2087+ obj_fi .write ('o depthmap\n ' )
20862088
20872089 pbar = tqdm .tqdm (total = len (node_str_list )+ len (str_faces ))
20882090 pbar .set_description ("Saving vertices" )
@@ -2186,6 +2188,76 @@ def write_ply(image,
21862188
21872189 return node_str_point , node_str_color , str_faces , H , W , hFov , vFov
21882190
2191+ def read_mesh (mesh_fi ):
2192+ ext = os .path .splitext (mesh_fi )[1 ]
2193+ if ext == '.ply' :
2194+ return read_ply (mesh_fi )
2195+ elif ext == '.obj' :
2196+ return read_obj (mesh_fi )
2197+ else :
2198+ raise Exception ('Unknown file format' )
2199+
2200+ def read_obj (mesh_fi ):
2201+ mfile = open (mesh_fi , 'r' , encoding = "utf8" )
2202+ Height = None
2203+ Width = None
2204+ hFov = None
2205+ vFov = None
2206+ mean_loc_depth = None
2207+
2208+ firstline = mfile .readline ().split ('\n ' )[0 ]
2209+ if not firstline .startswith ('# depthmap-script' ):
2210+ raise Exception ('File was not generated with this extension.' )
2211+
2212+ while True :
2213+ line = mfile .readline ().split ('\n ' )[0 ]
2214+ if line .startswith ('#' ):
2215+ if line .split (' ' )[1 ] == 'H' :
2216+ Height = int (line .split (' ' )[- 1 ].split ('\n ' )[0 ])
2217+ elif line .split (' ' )[1 ] == 'W' :
2218+ Width = int (line .split (' ' )[- 1 ].split ('\n ' )[0 ])
2219+ elif line .split (' ' )[1 ] == 'hFov' :
2220+ hFov = float (line .split (' ' )[- 1 ].split ('\n ' )[0 ])
2221+ elif line .split (' ' )[1 ] == 'vFov' :
2222+ vFov = float (line .split (' ' )[- 1 ].split ('\n ' )[0 ])
2223+ elif line .split (' ' )[1 ] == 'meanLoc' :
2224+ mean_loc_depth = float (line .split (' ' )[- 1 ].split ('\n ' )[0 ])
2225+ elif line .split (' ' )[1 ] == 'vertices' :
2226+ num_vertex = int (line .split (' ' )[- 1 ])
2227+ elif line .split (' ' )[1 ] == 'faces' :
2228+ num_face = int (line .split (' ' )[- 1 ])
2229+ # check for start of object
2230+ elif line .startswith ('o depthmap' ):
2231+ break
2232+
2233+ contents = mfile .readlines ()
2234+ mfile .close ()
2235+
2236+ vertex_infos = contents [:num_vertex ]
2237+ face_infos = contents [num_vertex :]
2238+
2239+ verts = [None ] * num_vertex
2240+ colors = [None ] * num_vertex
2241+ faces = [None ] * num_face
2242+ i = 0
2243+ for v_info in vertex_infos :
2244+ str_info = [float (v ) for v in v_info .split ('\n ' )[0 ].split (' ' )[1 :]]
2245+ vx , vy , vz , r , g , b = str_info
2246+ verts [i ] = [vx , vy , vz ]
2247+ colors [i ] = [r , g , b ]
2248+ i = i + 1
2249+ verts = np .array (verts )
2250+ colors = np .array (colors )
2251+
2252+ i = 0
2253+ for f_info in face_infos :
2254+ v1 , v2 , v3 = [int (f ) for f in f_info .split ('\n ' )[0 ].split (' ' )[1 :]]
2255+ faces [i ] = [v1 - 1 , v2 - 1 , v3 - 1 ]
2256+ i = i + 1
2257+ faces = np .array (faces )
2258+
2259+ return verts , colors , faces , Height , Width , hFov , vFov , mean_loc_depth
2260+
21892261def read_ply (mesh_fi ):
21902262 #bty: implement binary support (assume same endianness for now)
21912263 # read header in text mode
0 commit comments