def gen_label(model_range, seg_range, swi_range, dis_range, rot_range, depth_and_vertex=True):
    b = conf.zfar * conf.znear / (conf.znear - conf.zfar)
    a = -b / conf.znear

    # preparation
    if depth_and_vertex:
        seg_range = [-1] + seg_range
    seg_color_map = distinct_colors(conf.num_classes)

    renderer = GLRenderer(b'gen_label', (conf.width, conf.height), (0, 0), toTexture=True)
    proj = glm.perspective(glm.radians(70), 1.0, conf.znear, conf.zfar)
    for model in model_range:
        vertex_color = None
        for mesh_idx in range(conf.num_meshes[model]):
            print('Generate label for model {} Mesh {}...'.format(model, mesh_idx))
            mesh_path = conf.mesh_path(model, mesh_idx)
            vertices, faces = load_mesh(mesh_path)
            regularize_mesh(vertices, model)
            faces = faces.reshape([faces.shape[0] * 3])
            vertex_buffer = vertices[faces]

            if vertex_color is None:
                vertex_color = distinct_colors(vertices.shape[0])
            vertex_color_buffer = (vertex_color[faces] / 255.0).astype(np.float32)

            for seg_idx in seg_range:
                # prepare segmentation color
                if seg_idx != -1:
                    seg_path = conf.segmentation_path(model, seg_idx)
                    segmentation = np.load(seg_path)
                    seg_color_buffer = np.zeros([faces.shape[0], 3], np.float32)
                    face_colors = seg_color_map[segmentation] / 255.0
                    seg_color_buffer[2::3,:] = seg_color_buffer[1::3,:] = seg_color_buffer[0::3,:] = face_colors

                for swi in swi_range:
                    for dis in dis_range:
                        for rot in rot_range:
                            mod = glm.identity()
                            mod = glm.rotate(mod, glm.radians(swi - conf.max_swi / 2), glm.vec3(0, 1, 0))
                            mod = glm.translate(mod, glm.vec3(0, 0, -dis / 100.0))
                            mod = glm.rotate(mod, glm.radians(rot), glm.vec3(0, 1, 0))
                            mvp = proj.dot(mod)

                            view_name = conf.view_name(swi, dis, rot)
                            if seg_idx == -1:
                                rgb, z = renderer.draw(vertex_buffer, vertex_color_buffer, mvp.T)
                                # save depth view
                                depth = ((conf.zfar - b / (z - a)) / (conf.zfar - conf.znear) * 255).astype(np.uint8)
                                depth_view_path = conf.depth_view_path(model, mesh_idx, view_name)
                                cv.imwrite(depth_view_path, depth)
                                # save vertex view
                                vertex_view_path = conf.vertex_view_path(model, mesh_idx, view_name)
                                cv.imwrite(vertex_view_path, rgb)
                            else:
                                rgb, z = renderer.draw(vertex_buffer, seg_color_buffer, mvp.T)
                                # save segmentation view
                                seg_view_path = conf.segmentation_view_path(model, mesh_idx, seg_idx, view_name)
                                cv.imwrite(seg_view_path, rgb)
예제 #2
0
def generate_mesh_feature(sess, input, output, mesh_path, flipyz=False):
    b = conf.zfar * conf.znear / (conf.znear - conf.zfar)
    a = -b / conf.znear

    renderer = GLRenderer(b'generate_mesh_feature', (conf.width, conf.height),
                          (0, 0),
                          toTexture=True)
    proj = glm.perspective(glm.radians(70), 1.0, conf.znear, conf.zfar)

    vertices, faces = load_mesh(mesh_path)
    vertices = regularize_mesh(vertices, flipyz)
    faces = faces.reshape([faces.shape[0] * 3])
    vertex_buffer = vertices[faces]
    vertex_color = distinct_colors(vertices.shape[0])
    vertex_color_buffer = (vertex_color[faces] / 255.0).astype(np.float32)

    cnt = np.zeros([vertices.shape[0]], dtype=np.int32)
    feat = np.zeros([vertices.shape[0], 16], dtype=np.float32)

    swi = 35
    dis = 200
    for rot in range(0, 360, 15):
        mod = glm.identity()
        mod = glm.rotate(mod, glm.radians(swi - conf.max_swi / 2),
                         glm.vec3(0, 1, 0))
        mod = glm.translate(mod, glm.vec3(0, 0, -dis / 100.0))
        mod = glm.rotate(mod, glm.radians(rot), glm.vec3(0, 1, 0))
        mvp = proj.dot(mod)

        rgb, z = renderer.draw(vertex_buffer, vertex_color_buffer, mvp.T)

        depth = ((conf.zfar - b / (z - a)) / (conf.zfar - conf.znear) *
                 255).astype(np.uint8)
        features = sess.run(output,
                            feed_dict={
                                input: depth.reshape([1, 512, 512, 1])
                            }).reshape([512, 512, 16])

        vertex = image_color2idx(rgb)

        mask = vertex > 0
        vertex = vertex[mask]
        features = features[mask]
        for i in range(vertex.shape[0]):
            cnt[vertex[i] - 1] += 1
            feat[vertex[i] - 1] += features[i]

    for i in range(vertices.shape[0]):
        if cnt[i] > 0:
            feat[i] /= cnt[i]
    return feat
예제 #3
0
def regularize_mesh_old(vertices, model):
    tmp = np.ones([vertices.shape[0], 4], dtype=np.float32)
    tmp[:, :3] = vertices

    if model.startswith('SCAPE'):
        m = glm.identity()
        m = glm.rotate(m, glm.radians(90), glm.vec3(0, 0, 1))
        m = glm.rotate(m, glm.radians(270), glm.vec3(1, 0, 0))
        tmp = glm.transform(tmp, m)
    elif model.startswith('MIT'):
        m = glm.identity()
        m = glm.rotate(m, glm.radians(90), glm.vec3(0, 1, 0))
        tmp = glm.transform(tmp, m)

    vertices[:, :] = tmp[:, :3]

    mean = np.mean(vertices, 0)
    vertices -= mean