Beispiel #1
0
def visualize(image,
              masks,
              planes,
              camera,
              postfix,
              sceneid,
              depthonly=False,
              sensor=False):
    if sensor == False:
        param_map = np.zeros((480, 640, 4))
        semantic_map = np.full((480, 640), False, dtype=np.bool)
        color_map = np.full((480, 640, 3), 0, dtype=np.uint8)
        for planeIndex, plane in enumerate(planes):
            m = masks[planeIndex] > 0.5
            param_map[m, :] = plane
            semantic_map[m] = True
            color_map[m, :] = np.random.randint(255, size=3)
        points, depth = calc_dp_by_param_map(param_map, camera)
    else:
        semantic_map = masks > 1e-4  # masks become depth
        points = calc_points_by_depth(masks, camera)
        depth = np.zeros((480, 640))
    if depthonly:
        return depth.reshape(480, 640)

    pt_colors = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).reshape(
        -1, 3
    )  #(cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + color_map).reshape(-1,3) / 2.0
    points = np.concatenate([points, pt_colors], 1)
    points = points[semantic_map.reshape(-1), :]
    filename = f'{sceneid}{postfix}'
    writePointCloud(filename + '.ply', points)
    #return depth.reshape(480,640)

    m = pyrender.Mesh.from_points(points[:, :3], colors=points[:, 3:6] / 255.0)
    scene = pyrender.Scene()
    scene.add(m)
    v = pyrender.Viewer(scene,
                        use_raymond_lighting=True,
                        run_in_thread=True,
                        record=True,
                        rotate=True,
                        rotate_axis=[0, 0, -1],
                        rotate_rate=np.pi / 4.0,
                        refresh_rate=10.0)
    v.on_mouse_scroll(0, 0, 5, 5)
    time.sleep(8)
    v.close_external()
    v.save_gif(filename + '.gif')
    return depth.reshape(480, 640)
Beispiel #2
0
 def visualize(self, image, masks, planes, camera, postfix, dst_path):
     # visualization
     param_map = np.zeros((480,640,4))
     semantic_map = np.full((480,640),False,dtype=np.bool)
     color_map = np.full((480,640,3),0,dtype=np.uint8)
     for planeIndex, plane in enumerate(planes):
         m = masks[planeIndex]
         param_map[m,:] = plane
         semantic_map[m] = True
         color_map[m,:] = np.random.randint(255, size=3)
     points = utils.calc_points_by_param_map(param_map, camera)
     #return points, semantic_map
     pt_colors = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).reshape(-1,3)#(cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + color_map).reshape(-1,3) / 2.0
     points = np.concatenate([points, pt_colors],1)
     points = points[semantic_map.reshape(-1),:]
     utils.writePointCloud(f'{dst_path}pt-{postfix}.ply',points)
Beispiel #3
0
def visualize_mesh(image,
                   masks,
                   planes,
                   camera,
                   postfix,
                   sceneid,
                   line_equs,
                   line_flags,
                   split_semantic,
                   along_line_mask,
                   depthonly=False):
    height, width = 480, 640
    param_map = np.zeros((height, width, 4))
    semantic_map = np.full((height, width), False, dtype=np.bool)
    planeid_map = np.full((height, width), -1, dtype=np.int)
    for planeIndex, plane in enumerate(planes):
        m = masks[planeIndex] > 0.5
        param_map[m, :] = plane
        semantic_map[m] = True
        planeid_map[m] = planeIndex
    #planeid_map[split_semantic<0.5] = -1
    points, depth = calc_dp_by_param_map(param_map, camera)

    valid_pixel_num = np.sum(semantic_map)  # do not write those zeros
    id_global = np.zeros((height, width), dtype=np.int)
    id_global[semantic_map] = np.arange(valid_pixel_num) + 1
    faces, contours = extract_polygon(image, masks, id_global)

    urange = np.arange(width, dtype=np.float32).reshape(1, -1).repeat(
        height, 0) / width
    vrange = np.arange(height, dtype=np.float32).reshape(-1, 1).repeat(
        width, 1) / height
    urange = urange.reshape(-1, 1)
    vrange = 1 - vrange.reshape(-1, 1)
    uvrange = np.concatenate([urange, vrange], axis=-1)

    normal = param_map[:, :, :3]
    normal = normal.reshape(-1, 3)

    semantic1d = np.arange(height * width).reshape(height, width)
    semantic1d = semantic1d[contours[:, 1], contours[:, 0]]
    #semantic1d = semantic_map.reshape(-1)
    filename = f'{sceneid}{postfix}'
    faces = duplicate_face(faces)
    write_obj_file(filename + '.obj', points[semantic1d, :],
                   normal[semantic1d, :], uvrange[semantic1d, :], faces)
    if type(line_equs) == type([]):
        return 0
    new_points = modify_points(points.copy(), contours, planeid_map, line_equs,
                               line_flags, along_line_mask, planes)
    filename = f'{sceneid}{postfix}'
    write_obj_file(filename + '_f.obj', new_points[semantic1d, :],
                   normal[semantic1d, :], uvrange[semantic1d, :], faces)
    exit()
    return 0

    pt_colors = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).reshape(
        -1, 3
    )  #(cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + color_map).reshape(-1,3) / 2.0
    points = np.concatenate([points, pt_colors], 1)
    points = points[semantic_map.reshape(-1), :]

    writePointCloud(filename + '.ply', points)