Diff of /matlab_to_numpy.py [000000] .. [082204]

Switch to unified view

a b/matlab_to_numpy.py
1
"""
2
Read all the Matlab files in the 'data' directory and export 3 numpy arrays:
3
- labels.npy
4
- images.npy
5
- masks.npy
6
7
Usage: python matlab_to_numpy.py ~/brain_tumor_dataset
8
"""
9
10
import os
11
import argparse
12
import sys
13
import numpy as np
14
import hdf5storage
15
import cv2
16
17
18
class NoDataFound(Exception):
19
    pass
20
21
22
def dir_path(path):
23
    """Check the path and the existence of a data directory"""
24
    # replace '\' in path for Windows users
25
    path = path.replace('\\', '/')
26
    data_path = os.path.join(path, 'data').replace('\\', '/')
27
28
    if os.path.isdir(data_path):
29
        return path
30
    elif os.path.isdir(path):
31
        raise NoDataFound('Could not find a "data" folder inside directory. {} does not exist.'
32
                          .format(data_path))
33
    else:
34
        raise NotADirectoryError(path)
35
36
37
parser = argparse.ArgumentParser()
38
parser.add_argument('path', help='path to the brain_tumor_dataset directory', type=dir_path)
39
parser.add_argument('--image-dimension', '-d', default=512, help='dimension of the image', type=int)
40
args = parser.parse_args()
41
42
labels = []
43
images = []
44
masks = []
45
46
data_dir = os.path.join(args.path, 'data').replace('\\', '/')
47
files = os.listdir(data_dir)
48
for i, file in enumerate(files, start=1):
49
    if i % 10 == 0:
50
        # print the percentage of images loaded
51
        sys.stdout.write('\r[{}/{}] images loaded: {:.1f} %'
52
                         .format(i, len(files), i / float(len(files)) * 100))
53
        sys.stdout.flush()
54
55
    # load matlab file with hdf5storage as scipy.io.loadmat does not support v7.3 files
56
    mat_file = hdf5storage.loadmat(os.path.join(data_dir, file))['cjdata'][0]
57
58
    # resize image and mask to a unique size
59
    image = cv2.resize(mat_file[2], dsize=(args.image_dimension, args.image_dimension),
60
                       interpolation=cv2.INTER_CUBIC)
61
    mask = cv2.resize(mat_file[4].astype('uint8'), dsize=(args.image_dimension, args.image_dimension),
62
                      interpolation=cv2.INTER_CUBIC)
63
64
    labels.append(int(mat_file[0]))
65
    images.append(image)
66
    masks.append(mask.astype(bool))
67
68
sys.stdout.write('\r[{}/{}] images loaded: {:.1f} %'
69
                 .format(i, len(files), i / float(len(files)) * 100))
70
sys.stdout.flush()
71
72
labels = np.array(labels)
73
images = np.array(images)
74
masks = np.array(masks)
75
76
print('\nlabels:', labels.shape)
77
print('images:', images.shape)
78
print('masks:', masks.shape)
79
80
np.save(os.path.join(args.path, 'labels.npy'), labels)
81
np.save(os.path.join(args.path, 'images.npy'), images)
82
np.save(os.path.join(args.path, 'masks.npy'), masks)
83
84
print('labels.npy, images.npy, masks.npy saved in', args.path)