| import os |
| import shutil |
| import glob |
| from tqdm import tqdm |
| import cv2 |
| from natsort import natsorted |
| import json |
| import math |
|
|
| def config_setup(): |
| config = {} |
| config["img_width_for_resize"] = 1024 |
| config["input_scenes"] = ["Hospital", "Office_Room_1", "Office_Room_2", "Parking_Lot"] |
| config["output_folders"] = ["src/dataset/mp3d/", "src/dataset/pano/", "src/dataset/s2d3d/"] |
| config["output_RGB_rules"] = ["image/", "test/img/pano_", "test/img/camera_"] |
| config["output_json_rules"] = ["label/", "test/label_cor/pano_", "test/label_cor/camera_"] |
|
|
| |
| config["except_list"] = ["017_Hospital", |
| "044_Hospital", |
| "049_Hospital", |
| "012_Office_Room_2", |
| "034_Office_Room_2", |
| "008_Office_Room_1", |
| "010_Office_Room_1", |
| "014_Office_Room_1", |
| "017_Office_Room_1", |
| "025_Office_Room_1", |
| "037_Office_Room_1" |
| ] |
| return config |
|
|
| def xyz2uv(xyz): |
|
|
| normXZ = math.sqrt( math.pow(xyz[0], 2) + math.pow(xyz[2], 2) ) |
| if normXZ < 0.000001: |
| normXZ = 0.000001 |
|
|
| normXYZ = math.sqrt(math.pow(xyz[0], 2) + |
| math.pow(xyz[1], 2) + |
| math.pow(xyz[2], 2) ) |
|
|
| v = math.asin(xyz[1] / normXYZ) |
| u = math.asin(xyz[0] / normXZ) |
|
|
| if xyz[2] > 0 and u > 0: |
| u = math.pi - u |
| elif xyz[2] > 0 and u < 0: |
| u = -math.pi - u |
|
|
| uv = (u, v) |
|
|
| return uv |
|
|
| def uv2coords(uv): |
|
|
| coordsX = uv[0] / (2 * math.pi) + 0.5 |
| coordsY = -uv[1] / math.pi + 0.5 |
|
|
| coords = (coordsX, coordsY) |
|
|
| return coords |
|
|
| def write_json2txt(input_json, output_txt, img_width): |
| output_list = [] |
| with open(input_json) as f: |
| dict_json = json.load(f) |
|
|
| for point in dict_json["layoutPoints"]["points"]: |
| layout_up = uv2coords(xyz2uv([point["xyz"][0], point["xyz"][1] + dict_json["layoutHeight"] - dict_json["cameraHeight"], point["xyz"][2]])) |
| layout_down = uv2coords(xyz2uv([point["xyz"][0], point["xyz"][1] - dict_json["cameraHeight"], point["xyz"][2]])) |
| output_list.append(" ".join([str(int(layout_up[0]*img_width)), str(int(layout_up[1]*img_width/2))]) + "\n") |
| output_list.append(" ".join([str(int(layout_down[0]*img_width)), str(int(layout_down[1]*img_width/2))]) + "\n") |
|
|
| with open(output_txt, "a") as t: |
| t.writelines(output_list) |
| return 0 |
|
|
| def main(): |
| config = config_setup() |
| |
| for input_scene in config["input_scenes"]: |
| img_files = natsorted(glob.glob(input_scene+"/RGB_mh_aligned/*_equi_rgb_aligned.png")) |
| json_files = natsorted(glob.glob(input_scene+"/layout/*_equi_layout.json")) |
|
|
| for img_file, json_file in zip(img_files, json_files): |
| idx = img_file.split(".")[0].split("/")[-1].split(input_scene)[0] |
|
|
| if idx + "_" + input_scene in config["except_list"]: |
| continue |
| |
| else: |
| for output_folder, output_RGB_rule, output_json_rule in zip(config["output_folders"], config["output_RGB_rules"], config["output_json_rules"]): |
| os.makedirs(output_folder+output_RGB_rule.split("/")[:-1], exist_ok=True) |
| os.makedirs(output_folder+output_json_rule.split("/")[:-1], exist_ok=True) |
| output_file = output_folder+output_RGB_rule+idx+"_"+input_scene+"_equi_rgb_aligned.png" |
| output_txt = output_folder+output_json_rule+idx+"_"+input_scene+"_equi_layout.txt" |
| |
| img = cv2.resize(cv2.imread(img_file), (config["img_width_for_resize"], int(config["img_width_for_resize"]/2))) |
| cv2.imwrite(output_file, img) |
|
|
| img_width = img.shape[1] |
| write_json2txt(json_file, output_txt, img_width) |
|
|
| if __name__ == "__main__": |
| main() |