indoor_loading.py 3.44 KB
Newer Older
liyinhao's avatar
liyinhao committed
1
2
3
4
5
6
7
import os.path as osp

import numpy as np

from mmdet.datasets.registry import PIPELINES


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@PIPELINES.register_module()
class PointsColorNormalize(object):
    """Points Color Normalize

    Normalize color of the points.

    Args:
        color_mean (List[float]): Mean color of the point cloud.
    """

    def __init__(self, color_mean):
        self.color_mean = color_mean

    def __call__(self, results):
        points = results.get('results', None)
        assert points.shape[1] >= 6
        points[:, 3:6] = points[:, 3:6] - np.array(self.color_mean) / 256.0
        results['points'] = points
        return results


@PIPELINES.register_module()
liyinhao's avatar
liyinhao committed
30
31
class LoadPointsFromFile(object):
    """Load Points From File.
liyinhao's avatar
liyinhao committed
32

liyinhao's avatar
liyinhao committed
33
    Load sunrgbd and scannet points from file.
liyinhao's avatar
liyinhao committed
34
35
36

    Args:
        use_height (bool): Whether to use height.
liyinhao's avatar
liyinhao committed
37
38
39
40
        load_dim (int): The dimension of the loaded points.
            Default: 6.
        use_dim (List[int]): Which dimensions of the points to be used.
            Default: [0, 1, 2].
liyinhao's avatar
liyinhao committed
41
42
    """

43
    def __init__(self, use_height, load_dim=6, use_dim=[0, 1, 2]):
liyinhao's avatar
liyinhao committed
44
        self.use_height = use_height
liyinhao's avatar
liyinhao committed
45
46
47
        assert max(use_dim) < load_dim
        self.load_dim = load_dim
        self.use_dim = use_dim
liyinhao's avatar
liyinhao committed
48
49

    def __call__(self, results):
liyinhao's avatar
liyinhao committed
50
51
        pts_filename = results.get('pts_filename', None)
        assert osp.exists(pts_filename)
52
        points = np.load(pts_filename)
liyinhao's avatar
liyinhao committed
53
54
        points = points.reshape(-1, self.load_dim)
        points = points[:, self.use_dim]
liyinhao's avatar
liyinhao committed
55

liyinhao's avatar
liyinhao committed
56
        if self.use_height:
liyinhao's avatar
liyinhao committed
57
58
59
60
            floor_height = np.percentile(points[:, 2], 0.99)
            height = points[:, 2] - floor_height
            points = np.concatenate([points, np.expand_dims(height, 1)], 1)
        results['points'] = points
liyinhao's avatar
liyinhao committed
61
62
63
64
        return results

    def __repr__(self):
        repr_str = self.__class__.__name__
liyinhao's avatar
liyinhao committed
65
        repr_str += '(use_height={})'.format(self.use_height)
liyinhao's avatar
liyinhao committed
66
        repr_str += '(mean_color={})'.format(self.color_mean)
liyinhao's avatar
liyinhao committed
67
68
        repr_str += '(load_dim={})'.format(self.load_dim)
        repr_str += '(use_dim={})'.format(self.use_dim)
liyinhao's avatar
liyinhao committed
69
70
        return repr_str

liyinhao's avatar
liyinhao committed
71
72
73
74
75
76
77
78
79
80
81
82

@PIPELINES.register_module
class LoadAnnotations3D(object):
    """Load Annotations3D.

    Load sunrgbd and scannet annotations.
    """

    def __init__(self):
        pass

    def __call__(self, results):
liyinhao's avatar
liyinhao committed
83
84
        ins_labelname = results.get('ins_labelname', None)
        sem_labelname = results.get('sem_labelname', None)
liyinhao's avatar
liyinhao committed
85
86
        info = results.get('info', None)
        if info['annos']['gt_num'] != 0:
liyinhao's avatar
liyinhao committed
87
            gt_bboxes_3d = info['annos']['gt_boxes_upright_depth']
liyinhao's avatar
liyinhao committed
88
89
            gt_labels = info['annos']['class'].reshape(-1, 1)
            gt_bboxes_3d_mask = np.ones_like(gt_labels)
liyinhao's avatar
liyinhao committed
90
        else:
liyinhao's avatar
liyinhao committed
91
            gt_bboxes_3d = np.zeros((1, 6), dtype=np.float32)
liyinhao's avatar
liyinhao committed
92
            gt_labels = np.zeros((1, 1))
liyinhao's avatar
liyinhao committed
93
            gt_bboxes_3d_mask = np.zeros((1, 1))
liyinhao's avatar
liyinhao committed
94

liyinhao's avatar
liyinhao committed
95
        if ins_labelname is not None and sem_labelname is not None:
liyinhao's avatar
liyinhao committed
96
97
98
99
100
101
            assert osp.exists(ins_labelname)
            assert osp.exists(sem_labelname)
            pts_instance_mask = np.load(ins_labelname)
            pts_semantic_mask = np.load(sem_labelname)
            results['pts_instance_mask'] = pts_instance_mask
            results['pts_semantic_mask'] = pts_semantic_mask
liyinhao's avatar
liyinhao committed
102

liyinhao's avatar
liyinhao committed
103
        results['gt_bboxes_3d'] = gt_bboxes_3d
liyinhao's avatar
liyinhao committed
104
        results['gt_labels'] = gt_labels
liyinhao's avatar
liyinhao committed
105
        results['gt_bboxes_3d_mask'] = gt_bboxes_3d_mask
liyinhao's avatar
liyinhao committed
106
107
108
109
110
        return results

    def __repr__(self):
        repr_str = self.__class__.__name__
        return repr_str