gha / data_tool /obj_combine.py
vegagoto123's picture
Add files using upload-large-folder tool
3217786 verified
Raw History Blame Contribute Delete
1.98 kB
import cv2
import numpy as np
obj_file = '/data_160TB/2021/yangkun/gaussian-splatting-main/flame/head_template.obj'
mtl_file = '/data_160TB/2021/yangkun/gaussian-splatting-main/flame/mean_texture.jpg'
def convert_obj_with_uv_to_xyzrgb(obj_file, uv_texture_file, output_file):
# Load the UV texture image
uv_texture = cv2.imread(uv_texture_file)
# Read the OBJ file
vertices = []
tex_coords = []
faces = []
with open(obj_file, 'r') as f:
for line in f:
if line.startswith('v '):
vertex = list(map(float, line.strip().split()[1:4]))
vertices.append(vertex)
elif line.startswith('vt '):
tex_coord = list(map(float, line.strip().split()[1:3]))
tex_coords.append(tex_coord)
elif line.startswith('f '):
face = []
for vertex_data in line.strip().split()[1:]:
vertex_index, tex_coord_index= map(int, vertex_data.split('/'))
face.append((vertex_index, tex_coord_index))
faces.append(face)
# Convert UV coordinates to RGB colors
colors = []
for face in faces:
for vertex_index, tex_coord_index in face:
u, v = tex_coords[tex_coord_index-1]
x = int(u * (uv_texture.shape[1] - 1))
y = int(v * (uv_texture.shape[0] - 1))
color = uv_texture[y, x]
colors.append(color)
# Write the modified OBJ file
with open(output_file, 'w') as f:
for vertex, color in zip(vertices, colors):
f.write(f'v {" ".join(map(str, vertex))} {color[2]} {color[1]} {color[0]}\n')
for face in faces:
f.write('f ' + ' '.join([f'{vertex_index}/{tex_coord_index}' for vertex_index, tex_coord_index in face]) + '\n')
# Example usage
convert_obj_with_uv_to_xyzrgb(obj_file, mtl_file, '/data_160TB/2021/yangkun/gaussian-splatting-main/flame/head_template_combine.obj')