neural_renderer/neural_renderer/load_obj.py
def load_obj(filename_obj, normalization=True, texture_size=4, load_texture=False,
             texture_wrapping='REPEAT', use_bilinear=True, use_cuda = True):
    """
    Load Wavefront .obj file.
    This function only supports vertices (v x x x) and faces (f x x x).
    """

    # read in all lines
    with open(filename_obj) as f:
        lines = f.readlines()

    # load vertices
    vertices = []
    for line in lines:
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'v':
            vertices.append([float(val) for val in line.split()[1:4]])
    vertices = torch.from_numpy(np.vstack(vertices).astype(np.float32))
    if use_cuda:
        vertices = vertices.cuda()

    # load vertex normal
    vertices_normal = []
    has_vn = False
    for line in lines:
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'vn':
            vertices_normal.append([float(val) for val in line.split()[1:4]])
    if vertices_normal:
        vertices_normal = torch.from_numpy(np.vstack(vertices_normal).astype(np.float32))
        if use_cuda:
            vertices_normal = vertices_normal.cuda()
        has_vn = True

    # load vertex texture coordinates
    vertices_texcoords = []
    has_vt = False
    for line in lines:
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'vt':
            vertices_texcoords.append([float(val) for val in line.split()[1:3]])
    if vertices_texcoords:
        vertices_texcoords = torch.from_numpy(np.vstack(vertices_texcoords).astype(np.float32))
        if use_cuda:
            vertices_texcoords = vertices_texcoords.cuda()
        has_vt = True

    # load faces
    faces = []
    faces_vn_idx = []
    faces_vt_idx = []
    for line in lines:
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'f':
            vs = line.split()[1:]
            nv = len(vs)
            face = [int(vs[i].split('/')[0]) for i in range(nv)]
            faces.append(face)
            if has_vt:
                face_vt_idx = [int(vs[i].split('/')[1]) for i in range(nv)]
                faces_vt_idx.append(face_vt_idx)
            if has_vn:
                face_vn_idx = [int(vs[i].split('/')[-1]) for i in range(nv)]
                faces_vn_idx.append(face_vn_idx)
    faces = torch.from_numpy(np.vstack(faces).astype(np.int32)) - 1
    faces_vn_idx = torch.from_numpy(np.vstack(faces_vn_idx).astype(np.int32)) - 1
    faces_vt_idx = torch.from_numpy(np.vstack(faces_vt_idx).astype(np.int32)) - 1
    if use_cuda:
        faces = faces.cuda()
        faces_vn_idx = faces_vn_idx.cuda()
        faces_vt_idx = faces_vt_idx.cuda()

    # load textures
    textures = None
    if load_texture:
        for line in lines:
            if line.startswith('mtllib'):
                filename_mtl = os.path.join(os.path.dirname(filename_obj), line.split()[1])
                textures = load_textures(filename_obj, filename_mtl, texture_size,
                                         texture_wrapping=texture_wrapping,
                                         use_bilinear=use_bilinear)
        if textures is None:
            raise Exception('Failed to load textures.')

    # normalize into a unit cube centered zero
    if normalization:
        vertices -= vertices.min(0)[0][None, :]
        vertices /= torch.abs(vertices).max()
        vertices *= 2
        vertices -= vertices.max(0)[0][None, :] / 2

    v_attr = {'v' : vertices, 'vn' : vertices_normal, 'vt' : vertices_texcoords}
    f_attr = {'f_v_idx' : faces, 'f_vn_idx' : faces_vn_idx, 'f_vt_idx' : faces_vt_idx}

    if load_texture:
        return v_attr, f_attr, textures 
    else:
        return v_attr, f_attr

    Load Wavefront .obj file.
    This function only supports vertices (v x x x) and faces (f x x x).
    
Load Wavefront .obj file.
Load Wavefront . obj file .
def load_obj(filename_obj, normalization=True, texture_size=4, load_texture=False,
             texture_wrapping='REPEAT', use_bilinear=True, use_cuda = True):
    with open(filename_obj) as f:
        lines = f.readlines()
    vertices = []
    for line in lines:
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'v':
            vertices.append([float(val) for val in line.split()[1:4]])
    vertices = torch.from_numpy(np.vstack(vertices).astype(np.float32))
    if use_cuda:
        vertices = vertices.cuda()
    vertices_normal = []
    has_vn = False
    for line in lines:
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'vn':
            vertices_normal.append([float(val) for val in line.split()[1:4]])
    if vertices_normal:
        vertices_normal = torch.from_numpy(np.vstack(vertices_normal).astype(np.float32))
        if use_cuda:
            vertices_normal = vertices_normal.cuda()
        has_vn = True
    vertices_texcoords = []
    has_vt = False
    for line in lines:
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'vt':
            vertices_texcoords.append([float(val) for val in line.split()[1:3]])
    if vertices_texcoords:
        vertices_texcoords = torch.from_numpy(np.vstack(vertices_texcoords).astype(np.float32))
        if use_cuda:
            vertices_texcoords = vertices_texcoords.cuda()
        has_vt = True
    faces = []
    faces_vn_idx = []
    faces_vt_idx = []
    for line in lines:
        if len(line.split()) == 0:
            continue
        if line.split()[0] == 'f':
            vs = line.split()[1:]
            nv = len(vs)
            face = [int(vs[i].split('/')[0]) for i in range(nv)]
            faces.append(face)
            if has_vt:
                face_vt_idx = [int(vs[i].split('/')[1]) for i in range(nv)]
                faces_vt_idx.append(face_vt_idx)
            if has_vn:
                face_vn_idx = [int(vs[i].split('/')[-1]) for i in range(nv)]
                faces_vn_idx.append(face_vn_idx)
    faces = torch.from_numpy(np.vstack(faces).astype(np.int32)) - 1
    faces_vn_idx = torch.from_numpy(np.vstack(faces_vn_idx).astype(np.int32)) - 1
    faces_vt_idx = torch.from_numpy(np.vstack(faces_vt_idx).astype(np.int32)) - 1
    if use_cuda:
        faces = faces.cuda()
        faces_vn_idx = faces_vn_idx.cuda()
        faces_vt_idx = faces_vt_idx.cuda()
    textures = None
    if load_texture:
        for line in lines:
            if line.startswith('mtllib'):
                filename_mtl = os.path.join(os.path.dirname(filename_obj), line.split()[1])
                textures = load_textures(filename_obj, filename_mtl, texture_size,
                                         texture_wrapping=texture_wrapping,
                                         use_bilinear=use_bilinear)
        if textures is None:
            raise Exception('Failed to load textures.')
    if normalization:
        vertices -= vertices.min(0)[0][None, :]
        vertices /= torch.abs(vertices).max()
        vertices *= 2
        vertices -= vertices.max(0)[0][None, :] / 2
    v_attr = {'v' : vertices, 'vn' : vertices_normal, 'vt' : vertices_texcoords}
    f_attr = {'f_v_idx' : faces, 'f_vn_idx' : faces_vn_idx, 'f_vt_idx' : faces_vt_idx}
    if load_texture:
        return v_attr, f_attr, textures 
    else:
        return v_attr, f_attr
def load_obj ( filename_obj , normalization = True , texture_size = 4 , load_texture = False , texture_wrapping = 'REPEAT' , use_bilinear = True , use_cuda = True ) : with open ( filename_obj ) as f : lines = f . readlines ( ) vertices = [ ] for line in lines : if len ( line . split ( ) ) == 0 : continue if line . split ( ) [ 0 ] == 'v' : vertices . append ( [ float ( val ) for val in line . split ( ) [ 1 : 4 ] ] ) vertices = torch . from_numpy ( np . vstack ( vertices ) . astype ( np . float32 ) ) if use_cuda : vertices = vertices . cuda ( ) vertices_normal = [ ] has_vn = False for line in lines : if len ( line . split ( ) ) == 0 : continue if line . split ( ) [ 0 ] == 'vn' : vertices_normal . append ( [ float ( val ) for val in line . split ( ) [ 1 : 4 ] ] ) if vertices_normal : vertices_normal = torch . from_numpy ( np . vstack ( vertices_normal ) . astype ( np . float32 ) ) if use_cuda : vertices_normal = vertices_normal . cuda ( ) has_vn = True vertices_texcoords = [ ] has_vt = False for line in lines : if len ( line . split ( ) ) == 0 : continue if line . split ( ) [ 0 ] == 'vt' : vertices_texcoords . append ( [ float ( val ) for val in line . split ( ) [ 1 : 3 ] ] ) if vertices_texcoords : vertices_texcoords = torch . from_numpy ( np . vstack ( vertices_texcoords ) . astype ( np . float32 ) ) if use_cuda : vertices_texcoords = vertices_texcoords . cuda ( ) has_vt = True faces = [ ] faces_vn_idx = [ ] faces_vt_idx = [ ] for line in lines : if len ( line . split ( ) ) == 0 : continue if line . split ( ) [ 0 ] == 'f' : vs = line . split ( ) [ 1 : ] nv = len ( vs ) face = [ int ( vs [ i ] . split ( '/' ) [ 0 ] ) for i in range ( nv ) ] faces . append ( face ) if has_vt : face_vt_idx = [ int ( vs [ i ] . split ( '/' ) [ 1 ] ) for i in range ( nv ) ] faces_vt_idx . append ( face_vt_idx ) if has_vn : face_vn_idx = [ int ( vs [ i ] . split ( '/' ) [ - 1 ] ) for i in range ( nv ) ] faces_vn_idx . append ( face_vn_idx ) faces = torch . from_numpy ( np . vstack ( faces ) . astype ( np . int32 ) ) - 1 faces_vn_idx = torch . from_numpy ( np . vstack ( faces_vn_idx ) . astype ( np . int32 ) ) - 1 faces_vt_idx = torch . from_numpy ( np . vstack ( faces_vt_idx ) . astype ( np . int32 ) ) - 1 if use_cuda : faces = faces . cuda ( ) faces_vn_idx = faces_vn_idx . cuda ( ) faces_vt_idx = faces_vt_idx . cuda ( ) textures = None if load_texture : for line in lines : if line . startswith ( 'mtllib' ) : filename_mtl = os . path . join ( os . path . dirname ( filename_obj ) , line . split ( ) [ 1 ] ) textures = load_textures ( filename_obj , filename_mtl , texture_size , texture_wrapping = texture_wrapping , use_bilinear = use_bilinear ) if textures is None : raise Exception ( 'Failed to load textures.' ) if normalization : vertices -= vertices . min ( 0 ) [ 0 ] [ None , : ] vertices /= torch . abs ( vertices ) . max ( ) vertices *= 2 vertices -= vertices . max ( 0 ) [ 0 ] [ None , : ] / 2 v_attr = { 'v' : vertices , 'vn' : vertices_normal , 'vt' : vertices_texcoords } f_attr = { 'f_v_idx' : faces , 'f_vn_idx' : faces_vn_idx , 'f_vt_idx' : faces_vt_idx } if load_texture : return v_attr , f_attr , textures else : return v_attr , f_attr
Load Wavefront .obj file.
Load Wavefront . obj file .
"""
    Load Wavefront .obj file.
    This function only supports vertices (v x x x) and faces (f x x x).
    """ # read in all lines # load vertices # load vertex normal # load vertex texture coordinates # load faces # load textures # normalize into a unit cube centered zero
filename_obj
normalization
texture_size
load_texture
texture_wrapping
use_bilinear
use_cuda