Matterport3D数据场景区域标签图片对应程序

最近基于Matterport3D数据做室内地图,需要批量解压各部分文件和解析每个区域的场景及场景对应的图片索引,具体代码如下:

1.批量解压

(1)安装unar包

sudo apt-get install unar

(2) 解压脚本

import os
def un_zip():
    zip_path = './scans/' # 压缩文件目录
    scences = os.listdir(zip_path)
    for scence in scences:
        zips = os.listdir(os.path.join(zip_path, scence))
        for zipp in zips:
            # 此处定义需要解压的数据类别,可以用一个数组存储
            if 'house_segmentations' in zipp or 'undistorted_color_images' in zipp or 'matterport_skybox_images' in zipp:
                print(os.path.join(zip_path, scence, zipp))
                os.system('sudo unzip {}'.format(os.path.join(zip_path, scence, zipp)))

2.获取场景、区域、区域类别、区域对应图片

(1)region2label.py

region2label = {
    'a':'bathroom',
    'b':'bedroom',
    'c':'closet',
    'd':'dining room',
    'e':'entryway_foyer_lobby',
    'f':'familyroom',
    'g':'garage',
    'h':'hallway',
    'i':'library',
    'j':'laundryroom_mudroom',
    'k':'kitchen',
    'l':'living room',
    'm':'conferenceroom',
    'n':'lounge',
    'o':'office',
    'p':'porch_terrace_deck_driveway',
    'r':'rec_game',
    's':'stairs',
    't':'toilet',
    'u':'utilityroom_toolroom ',
    'v':'tv',
    'w':'workout_gym_exercise',
    'x':'outdoor',
    'y':'balcony',
    'z':'other room',
    'B':'bar',
    'C':'classroom',
    'D':'dining booth',
    'S':'spa_sauna',
    'Z':'junk',
    '-':'no'
}

(2) 脚本

import os
from region2label import region2label

def parse_img_region():
    scenes = os.listdir('./unzip/')
    scene_region_images = {}
    for scene in scenes:
        scene_region_images[scene] = {}
        parse_source = os.path.join('./unzip', scene, 'house_segmentations', '{}.house'.format(scene))
        lines = [line.rstrip().replace('\n', '') for line in open(parse_source)]
        for line in lines:
            if 'R ' in line:
                txt = line.split('  ')
                scene_region_images[scene][txt[1].split(' ')[0]] = {}
                scene_region_images[scene][txt[1].split(' ')[0]]['label'] = region2label[txt[3]]
                scene_region_images[scene][txt[1].split(' ')[0]]['imgs'] = []
            if 'P ' in line:
                txt = line.split('  ')
                if txt[2].split(' ')[1] != '-1':
                    scene_region_images[scene][txt[2].split(' ')[1]]['imgs'].append(txt[1])
        print('{} Done!'.format(scene)) 
    f = open('scene_region_images.py', 'w+')
    f.write(scene_region_images.__str__())
    f.close()
阅读剩余
THE END