loading.py 2.52 KB
Newer Older
zhangwenwei's avatar
zhangwenwei committed
1
2
3
import mmcv
import numpy as np

4
from mmdet.datasets.builder import PIPELINES
zhangwenwei's avatar
zhangwenwei committed
5
6


7
@PIPELINES.register_module()
zhangwenwei's avatar
zhangwenwei committed
8
9
class LoadMultiViewImageFromFiles(object):
    """ Load multi channel images from a list of separate channel files.
zhangwenwei's avatar
zhangwenwei committed
10
11

    Expects results['img_filename'] to be a list of filenames
zhangwenwei's avatar
zhangwenwei committed
12
    """
zhangwenwei's avatar
zhangwenwei committed
13

zhangwenwei's avatar
zhangwenwei committed
14
15
16
    def __init__(self, to_float32=False, color_type='unchanged'):
        self.to_float32 = to_float32
        self.color_type = color_type
zhangwenwei's avatar
zhangwenwei committed
17
18

    def __call__(self, results):
zhangwenwei's avatar
zhangwenwei committed
19
        filename = results['img_filename']
zhangwenwei's avatar
zhangwenwei committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        img = np.stack(
            [mmcv.imread(name, self.color_type) for name in filename], axis=-1)
        if self.to_float32:
            img = img.astype(np.float32)
        results['filename'] = filename
        results['img'] = img
        results['img_shape'] = img.shape
        results['ori_shape'] = img.shape
        # Set initial values for default meta_keys
        results['pad_shape'] = img.shape
        results['scale_factor'] = 1.0
        num_channels = 1 if len(img.shape) < 3 else img.shape[2]
        results['img_norm_cfg'] = dict(
            mean=np.zeros(num_channels, dtype=np.float32),
            std=np.ones(num_channels, dtype=np.float32),
            to_rgb=False)
zhangwenwei's avatar
zhangwenwei committed
36
37
38
        return results

    def __repr__(self):
zhangwenwei's avatar
zhangwenwei committed
39
40
        return "{} (to_float32={}, color_type='{}')".format(
            self.__class__.__name__, self.to_float32, self.color_type)
zhangwenwei's avatar
zhangwenwei committed
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
71
72
73
74
75


@PIPELINES.register_module()
class LoadPointsFromMultiSweeps(object):

    def __init__(self, sweeps_num=10):
        self.sweeps_num = sweeps_num

    def __call__(self, results):
        points = results['points']
        points[:, 3] /= 255
        points[:, 4] = 0
        sweep_points_list = [points]
        ts = results['timestamp']

        for idx, sweep in enumerate(results['sweeps']):
            if idx >= self.sweeps_num:
                break
            points_sweep = np.fromfile(
                sweep['data_path'], dtype=np.float32,
                count=-1).reshape([-1, 5])
            sweep_ts = sweep['timestamp'] / 1e6
            points_sweep[:, 3] /= 255
            points_sweep[:, :3] = points_sweep[:, :3] @ sweep[
                'sensor2lidar_rotation'].T
            points_sweep[:, :3] += sweep['sensor2lidar_translation']
            points_sweep[:, 4] = ts - sweep_ts
            sweep_points_list.append(points_sweep)

        points = np.concatenate(sweep_points_list, axis=0)[:, [0, 1, 2, 4]]
        results['points'] = points
        return results

    def __repr__(self):
        return f'{self.__class__.__name__}(sweeps_num={self.sweeps_num})'