Skip to content

Commit 836830a

Browse files
committed
support reading wavefront obj for generating video
1 parent 86b5865 commit 836830a

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

scripts/depthmap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
from pix2pix.models.pix2pix4depth_model import Pix2Pix4DepthModel
5454

5555
# 3d-photo-inpainting imports
56-
from inpaint.mesh import write_ply, read_ply, output_3d_photo
56+
from inpaint.mesh import write_mesh, read_mesh, output_3d_photo
5757
from inpaint.networks import Inpaint_Color_Net, Inpaint_Depth_Net, Inpaint_Edge_Net
5858
from inpaint.utils import path_planning
5959
from inpaint.bilateral_filtering import sparse_bilateral_filtering
@@ -805,7 +805,7 @@ def run_3dphoto(device, img_rgb, img_depth, inputnames, outpath, fnExt, vid_ssaa
805805
#bilat_fn = os.path.join(outpath, basename +'_bilatdepth.png')
806806
#cv2.imwrite(bilat_fn, depth)
807807

808-
rt_info = write_ply(img,
808+
rt_info = write_mesh(img,
809809
depth,
810810
int_mtx,
811811
mesh_fi,
@@ -847,7 +847,7 @@ def run_3dphoto_videos(mesh_fi, basename, outpath, num_frames, fps, crop_border,
847847
vispy.use(app='egl')
848848

849849
# read ply
850-
verts, colors, faces, Height, Width, hFov, vFov, mean_loc_depth = read_ply(mesh_fi)
850+
verts, colors, faces, Height, Width, hFov, vFov, mean_loc_depth = read_mesh(mesh_fi)
851851

852852
original_w = output_w = W = Width
853853
original_h = output_h = H = Height
@@ -1100,7 +1100,7 @@ def on_ui_tabs():
11001100
with gr.Column():
11011101
vid_html_info_x = gr.HTML()
11021102
vid_html_info = gr.HTML()
1103-
fn_mesh = gr.Textbox(label="Input Mesh (.ply)", **shared.hide_dirs, placeholder="A file on the same machine where the server is running.")
1103+
fn_mesh = gr.Textbox(label="Input Mesh (.ply | .obj)", **shared.hide_dirs, placeholder="A file on the same machine where the server is running.")
11041104
with gr.Row():
11051105
vid_numframes = gr.Textbox(label="Number of frames", value="300")
11061106
vid_fps = gr.Textbox(label="Framerate", value="40")

scripts/inpaint/mesh.py

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
21892261
def read_ply(mesh_fi):
21902262
#bty: implement binary support (assume same endianness for now)
21912263
# read header in text mode

0 commit comments

Comments
 (0)