File size: 2,171 Bytes
a80d6bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
71
72
73
74
75
76
77
import os
import re
import cv2
import numpy as np

from ..utils.common import Notify

def read_list(list_path):
    """Read list."""
    if list_path is None or not os.path.exists(list_path):
        print(Notify.FAIL, 'Not exist', list_path, Notify.ENDC)
        exit(-1)
    content = open(list_path).read().splitlines()
    return content


def load_pfm(pfm_path):
    with open(pfm_path, 'rb') as fin:
        color = None
        width = None
        height = None
        scale = None
        data_type = None
        header = str(fin.readline().decode('UTF-8')).rstrip()

        if header == 'PF':
            color = True
        elif header == 'Pf':
            color = False
        else:
            raise Exception('Not a PFM file.')

        dim_match = re.match(r'^(\d+)\s(\d+)\s$',
                             fin.readline().decode('UTF-8'))
        if dim_match:
            width, height = map(int, dim_match.groups())
        else:
            raise Exception('Malformed PFM header.')
        scale = float((fin.readline().decode('UTF-8')).rstrip())
        if scale < 0:  # little-endian
            data_type = '<f'
        else:
            data_type = '>f'  # big-endian
        data_string = fin.read()
        data = np.fromstring(data_string, data_type)
        shape = (height, width, 3) if color else (height, width)
        data = np.reshape(data, shape)
        data = np.flip(data, 0)
    return data


def _parse_img(img_paths, idx, config):
    img_path = img_paths[idx]
    img = cv2.imread(img_path)[:, :, ::-1]
    if config['resize'] > 0:
        img = cv2.resize(
            img, (config['resize'], config['resize']))
    return img


def _parse_depth(depth_paths, idx, config):
    depth = load_pfm(depth_paths[idx])

    if config['resize'] > 0:
        target_size = config['resize']
    if config['input_type'] == 'raw':
        depth = cv2.resize(depth, (int(target_size/2), int(target_size/2)))
    else:
        depth = cv2.resize(depth, (target_size, target_size))
    return depth


def _parse_kpts(kpts_paths, idx, config):
    kpts = np.load(kpts_paths[idx])['pts']
    # output: [N, 2] (W first H last)
    return kpts