nyu_depth_v2数据读取

数据路径:NYU Depth V2 « Nathan Silberman

自写的读取代码:

# 这是一个示例 Python 脚本。
import os

import matplotlib.pyplot as plt
import numpy as np
from scipy.io import loadmat
import h5py

from PIL import Image

import warnings

warnings.filterwarnings('ignore')


def ASCII_str(rawDepthFilenames, feature, i):
    # print(rawDepthFilenames)
    st = rawDepthFilenames[0][i]
    obj = feature[st]  # 
    strsss = "".join(chr(int(i)) for i in obj[:])  # rawDepthFilenames 存储的是ASCII码值,需要转换成字符
    return strsss


def save_RGB(iconpath, a, i):
    r = Image.fromarray(a[0]).convert('L')
    g = Image.fromarray(a[1]).convert('L')
    b = Image.fromarray(a[2]).convert('L')
    img = Image.merge("RGB", (r, g, b))
    img = img.transpose(Image.ROTATE_270)

    img.save(os.path.join(iconpath, str(i) + '.jpg'), optimize=True)


def save_depth(path_converted, depths, i):
    max = np.max(depths)

    depths = depths / max * 255
    depths = np.transpose(depths, (1, 0))
    depths_img = Image.fromarray(np.uint8(depths))
    depths_img = depths_img.transpose(Image.FLIP_LEFT_RIGHT)

    depths_img.save(os.path.join(path_converted, str(i) + '.png'), 'PNG', optimize=True)


def save_label(path_converted, labels, y):

    label_img = Image.fromarray(np.uint8(labels))

    label_img = label_img.transpose(Image.ROTATE_270)
    label_img.save(os.path.join(path_converted, str(y) + '.png'), 'PNG', optimize=True)


def main1():
    feature = h5py.File(filename + "nyu_depth_v2_labeled.mat")  # 读取mat文件
    # data = feature[:]  # 读取mat文件中所有数据存储到array中
    # print(feature.keys())
    data_len = len(feature[list(feature.keys())[4]])
    print(list(feature.keys()))
    print('数据长度:', data_len)

    depths = feature[list(feature.keys())[3]]  # 深度图像H*W*N
    images = feature[list(feature.keys())[4]]  # RGB图像H*W*3*N
    instances = feature[list(feature.keys())[5]]  #
    labels = feature[list(feature.keys())[6]]  # 标签值,对应范围从1-C
    names = feature[list(feature.keys())[7]]  # 数据类别名
    rawDepthFilenames = feature[list(feature.keys())[9]]  # 深度图像数据类别名
    rawDepths = feature[list(feature.keys())[10]]  #
    rawRgbFilenames = feature[list(feature.keys())[11]]  # RGB数据类别名
    scenes = feature[list(feature.keys())[-2]]  # 场景名
    sceneTypes = feature[list(feature.keys())[-1]]  # 场景类型

    # ss = []
    # for x in range(names.shape[1]):
    #     names_i = ASCII_str(names, feature, x)  # /r-1294851097.528505-2321469380.ppm  r-表示RGB
    #     # print(names_i)
    #     ss.append(names_i)
    # print(len(list(set(ss)))) # 每个类的英文名称 都不一样
    # exit()

    save_path_RGB = os.path.join(filename, 'nyu_depth_RGB')
    os.makedirs(save_path_RGB, exist_ok=True)
    save_path_depth = os.path.join(filename, 'nyu_depth_depth')
    os.makedirs(save_path_depth, exist_ok=True)
    save_path_depth2 = os.path.join(filename, 'nyu_depth_depth2')
    os.makedirs(save_path_depth2, exist_ok=True)
    nyu_depth_label = os.path.join(filename, 'nyu_depth_label')
    os.makedirs(nyu_depth_label, exist_ok=True)

    labels_number = []
    # 读取每个图像的深度图像,RGB图像,以及图像名,深度图像名,以及场景名
    for y in range(data_len):
        # print('depth:', depths.shape)  #  (1449, 640, 480)
        # print('images:', images.shape) # (1449, 3, 640, 480)
        # print('instances:', depths[y].shape)  # (640, 480)
        # print('rawDepth_name:', rawDepthFilenames[0].shape) #  (1449,)
        # print('rawRGB_name:', rawRgbFilenames[0].shape) #  (1449,)

        rawDepth_i = ASCII_str(rawDepthFilenames, feature, y)  # d-1294851097.542259-2322437371.pgm d- 表示深度相机的帧
        rawRgb_i = ASCII_str(rawRgbFilenames, feature, y)  # /r-1294851097.528505-2321469380.ppm  r-表示RGB
        scenes_i = ASCII_str(scenes, feature, y)  # /r-1294851097.528505-2321469380.ppm  r-表示RGB
        sceneTypes_i = ASCII_str(sceneTypes, feature, y)  # /r-1294851097.528505-2321469380.ppm  r-表示RGB


        # 设置存放数据文件
        save_depth(save_path_depth, depths[y], y)
        save_RGB(save_path_RGB, images[y], y)
        save_depth(save_path_depth2, rawDepths[y], y)

        # np.save(os.path.join(save_path_depth, ''.join(rawRgb_i.split('/')[-1].split('.')[:-1]) + '_depth.npy'), depths[y])
        # np.save(os.path.join(save_path_RGB, ''.join(rawRgb_i.split('/')[-1].split('.')[:-1]) + '_RGB.npy'), images[y])
        # np.save(os.path.join(save_path, ''.join(rawRgb_i.split('/')[-1].split('.')[:-1]) + '_label.npy'), labels[y])

        save_label(nyu_depth_label, labels[y], y)



def main2():
    # loadmat 不能读取超过2G的数据
    feature = loadmat(filename + "nyu_depth_v2_labeled.mat")
    data_len = len(feature[list(feature.keys())[4]])
    print(list(feature.keys()))
    print('数据长度:', data_len)


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    filename = './' # 存放nyu_depth_v2的路径

    main1()

你可能感兴趣的:(python,numpy,开发语言)