show_result.py 3.67 KB
Newer Older
liyinhao's avatar
liyinhao committed
1
2
3
import mmcv
import numpy as np
import trimesh
zhangwenwei's avatar
zhangwenwei committed
4
from os import path as osp
liyinhao's avatar
liyinhao committed
5
6
7


def _write_ply(points, out_filename):
Wenwei Zhang's avatar
Wenwei Zhang committed
8
    """Write points into ``ply`` format for meshlab visualization.
zhangwenwei's avatar
zhangwenwei committed
9
10
11
12
13

    Args:
        points (np.ndarray): Points in shape (N, dim).
        out_filename (str): Filename to be saved.
    """
liyinhao's avatar
liyinhao committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    N = points.shape[0]
    fout = open(out_filename, 'w')
    for i in range(N):
        if points.shape[1] == 6:
            c = points[i, 3:].astype(int)
            fout.write(
                'v %f %f %f %d %d %d\n' %
                (points[i, 0], points[i, 1], points[i, 2], c[0], c[1], c[2]))

        else:
            fout.write('v %f %f %f\n' %
                       (points[i, 0], points[i, 1], points[i, 2]))
    fout.close()


def _write_oriented_bbox(scene_bbox, out_filename):
zhangwenwei's avatar
zhangwenwei committed
30
    """Export oriented (around Z axis) scene bbox to meshes.
liyinhao's avatar
liyinhao committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

    Args:
        scene_bbox(list[ndarray] or ndarray): xyz pos of center and
            3 lengths (dx,dy,dz) and heading angle around Z axis.
            Y forward, X right, Z upward. heading angle of positive X is 0,
            heading angle of positive Y is 90 degrees.
        out_filename(str): Filename.
    """

    def heading2rotmat(heading_angle):
        rotmat = np.zeros((3, 3))
        rotmat[2, 2] = 1
        cosval = np.cos(heading_angle)
        sinval = np.sin(heading_angle)
        rotmat[0:2, 0:2] = np.array([[cosval, -sinval], [sinval, cosval]])
        return rotmat

    def convert_oriented_box_to_trimesh_fmt(box):
        ctr = box[:3]
        lengths = box[3:6]
        trns = np.eye(4)
        trns[0:3, 3] = ctr
        trns[3, 3] = 1.0
        trns[0:3, 0:3] = heading2rotmat(box[6])
        box_trimesh_fmt = trimesh.creation.box(lengths, trns)
        return box_trimesh_fmt

    if len(scene_bbox) == 0:
        scene_bbox = np.zeros((1, 7))
    scene = trimesh.scene.Scene()
    for box in scene_bbox:
        scene.add_geometry(convert_oriented_box_to_trimesh_fmt(box))

    mesh_list = trimesh.util.concatenate(scene.dump())
    # save to ply file
    trimesh.io.export.export_mesh(mesh_list, out_filename, file_type='ply')

    return


71
def show_result(points, gt_bboxes, pred_bboxes, out_dir, filename, show=True):
zhangwenwei's avatar
zhangwenwei committed
72
73
74
75
76
77
78
79
    """Convert results into format that is directly readable for meshlab.

    Args:
        points (np.ndarray): Points.
        gt_bboxes (np.ndarray): Ground truth boxes.
        pred_bboxes (np.ndarray): Predicted boxes.
        out_dir (str): Path of output directory
        filename (str): Filename of the current frame.
80
        show (bool): Visualize the results online.
zhangwenwei's avatar
zhangwenwei committed
81
    """
82
83
    from .open3d_vis import Visualizer

84
85
86
87
88
89
90
91
    if show:
        vis = Visualizer(points)
        if pred_bboxes is not None:
            vis.add_bboxes(bbox3d=pred_bboxes)
        if gt_bboxes is not None:
            vis.add_bboxes(bbox3d=gt_bboxes, bbox_color=(0, 0, 1))
        vis.show()

liyinhao's avatar
liyinhao committed
92
93
    result_path = osp.join(out_dir, filename)
    mmcv.mkdir_or_exist(result_path)
liyinhao's avatar
liyinhao committed
94

95
96
97
    if points is not None:
        _write_ply(points, osp.join(result_path, f'{filename}_points.obj'))

liyinhao's avatar
liyinhao committed
98
    if gt_bboxes is not None:
99
100
101
        # bottom center to gravity center
        gt_bboxes[..., 2] += gt_bboxes[..., 5] / 2
        # the positive direction for yaw in meshlab is clockwise
liyinhao's avatar
liyinhao committed
102
        gt_bboxes[:, 6] *= -1
liyinhao's avatar
liyinhao committed
103
104
        _write_oriented_bbox(gt_bboxes,
                             osp.join(result_path, f'{filename}_gt.ply'))
liyinhao's avatar
liyinhao committed
105
106

    if pred_bboxes is not None:
107
108
109
        # bottom center to gravity center
        pred_bboxes[..., 2] += pred_bboxes[..., 5] / 2
        # the positive direction for yaw in meshlab is clockwise
liyinhao's avatar
liyinhao committed
110
        pred_bboxes[:, 6] *= -1
liyinhao's avatar
liyinhao committed
111
        _write_oriented_bbox(pred_bboxes,
liyinhao's avatar
liyinhao committed
112
                             osp.join(result_path, f'{filename}_pred.ply'))