def test_draw_polygon_cv2_smokescreen():
    """
        Test ability to fill a nonconvex polygon.

        We don't verify the rendered values since this requires
        scanline rendering computation to find the polygon's
        exact boundaries on a rasterized grid.
        """
    UINT8_MAX = 255
    img_w = 40
    img_h = 20
    for dtype in [np.uint8, np.float32]:

        # (x,y) points: Numpy array of shape (N,2), not (u,v) but (v,u)
        pentagon_pts = np.array([[1, 0], [2, 2], [0, 4], [-2, 2], [-1, 0]])
        # move the pentagon origin to (10,20) so in image center
        pentagon_pts[:, 0] += int(img_w / 2)
        pentagon_pts[:, 1] += int(img_h / 2)
        # img: Numpy array of shape (M,N,3)
        img = np.ones((img_h, img_w, 3), dtype=dtype) * UINT8_MAX
        # color: Numpy array of shape (3,)
        color = np.array([255.0, 0.0, 0.0])

        img_w_polygon = draw_polygon_cv2(pentagon_pts, img.copy(), color)

        assert isinstance(img_w_polygon, np.ndarray)
        assert img_w_polygon.shape == img.shape
        assert img_w_polygon.dtype == dtype
예제 #2
0
def render_global_city_map_bev(
    lane_centerlines: LaneCenterline,
    driveable_areas: np.ndarray,
    city_name: str,
    avm: MapProtocol,
    plot_rasterized_roi: bool = True,
    plot_vectormap_roi: bool = False,
    centerline_color_scheme: str = "constant",
) -> None:
    """
    Assume indegree and outdegree are always less than 7.
    Since 1 pixel-per meter resolution is very low, we upsample the resolution
    by some constant factor.

    Args:
        lane_centerlines: Line centerline data
        driveable_areas: Driveable area data
        city_name: The city name (eg. "PIT")
        avm: The map
        plot_rasterized_roi: Whether to show the rasterized ROI
        plot_vectormap_roi: Whether to show the vector map ROI
        centerline_color_scheme: `"indegree"`, `"outdegree"`, or `"constant"`
    """
    UPSAMPLE_FACTOR = 2
    GRID_NUMBER_INTERVAL = 500
    NUM_COLOR_BINS = 7

    warnings.filterwarnings("error")
    im_h, im_w, xmin, xmax, ymin, ymax = _get_int_image_bounds_from_city_coords(
        driveable_areas, lane_centerlines)
    rendered_image = np.ones(
        (im_h * UPSAMPLE_FACTOR, im_w * UPSAMPLE_FACTOR, 3))
    image_to_city_se2 = SE2(rotation=np.eye(2),
                            translation=np.array([-xmin, -ymin]))

    if plot_rasterized_roi:
        grid_2d_pts = get_mesh_grid_as_point_cloud(xmin + 1, xmax - 1,
                                                   ymin + 1, ymax - 1)
        pink = np.array([180.0, 105.0, 255.0]) / 255
        roi_2d_pts = avm.remove_non_driveable_area_points(
            grid_2d_pts, city_name)
        roi_2d_pts = image_to_city_se2.transform_point_cloud(roi_2d_pts)
        roi_2d_pts *= UPSAMPLE_FACTOR
        roi_2d_pts = np.round(roi_2d_pts).astype(np.int32)
        for pt in roi_2d_pts:
            row = pt[1]
            col = pt[0]
            rendered_image[row, col, :] = pink

    if plot_vectormap_roi:
        driveable_areas = copy.deepcopy(driveable_areas)
        for da_idx, da in enumerate(driveable_areas):
            da = image_to_city_se2.transform_point_cloud(da[:, :2])
            rendered_image = draw_polygon_cv2(da * UPSAMPLE_FACTOR,
                                              rendered_image, pink)

    for x in range(0, im_w * UPSAMPLE_FACTOR, GRID_NUMBER_INTERVAL):
        for y in range(0, im_h * UPSAMPLE_FACTOR, GRID_NUMBER_INTERVAL):
            coords_str = f"{x}_{y}"
            cv2.putText(
                rendered_image,
                coords_str,
                (x, y),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,
                (255, 0, 0),
                2,
                cv2.LINE_AA,
                bottomLeftOrigin=True,
            )

    # Color the graph
    max_outdegree = 0
    max_indegree = 0

    colors_arr = _get_opencv_green_to_red_colormap(NUM_COLOR_BINS)

    blue = [0, 0, 1][::-1]
    for lane_id, lane_obj in lane_centerlines.items():
        centerline_2d = lane_obj.centerline
        centerline_2d = image_to_city_se2.transform_point_cloud(centerline_2d)
        centerline_2d = np.round(centerline_2d).astype(np.int32)

        preds = lane_obj.predecessors
        succs = lane_obj.successors

        indegree = 0 if preds is None else len(preds)
        outdegree = 0 if succs is None else len(succs)

        if indegree > max_indegree:
            max_indegree = indegree
            print("max indegree", indegree)

        if outdegree > max_outdegree:
            max_outdegree = outdegree
            print("max outdegree", outdegree)

        if centerline_color_scheme == "indegree":
            color = colors_arr[indegree]
        elif centerline_color_scheme == "outdegree":
            color = colors_arr[outdegree]
        elif centerline_color_scheme == "constant":
            color = blue

        draw_polyline_cv2(
            centerline_2d * UPSAMPLE_FACTOR,
            rendered_image,
            color,
            im_h * UPSAMPLE_FACTOR,
            im_w * UPSAMPLE_FACTOR,
        )

    # provide colormap in corner
    for i in range(NUM_COLOR_BINS):
        rendered_image[0, i, :] = colors_arr[i]

    rendered_image = np.flipud(rendered_image)
    rendered_image *= 255
    rendered_image = rendered_image.astype(np.uint8)

    cur_datetime = generate_datetime_string()
    img_save_fpath = f"{city_name}_{centerline_color_scheme}_{cur_datetime}.png"
    cv2.imwrite(filename=img_save_fpath, img=rendered_image)

    warnings.resetwarnings()