Skip to content
Snippets Groups Projects
Commit 13702a97 authored by Danylo Mazurak's avatar Danylo Mazurak
Browse files

added scripts for fruitbin dataset preprocessing

parent 32249172
No related branches found
No related tags found
No related merge requests found
import json
import os
bbox_path = f'/home/Downloads/Datasets/BOP_format/test'
output = "/home/Downloads/Datasets/BOP_format/test/test_bboxes/"
os.makedirs(output, exist_ok=True)
output_file = f"{output}gt_all_fruits_fruitbin_pbr_fruitbin_bop_test.json"
directories = [d for d in os.listdir(bbox_path) if os.path.isdir(os.path.join(bbox_path, d))]
directories = sorted(directories)
updated_data = {}
obj_id = 1
for scene_id, folder in enumerate(directories):
file_path = os.path.join(bbox_path, folder, "scene_gt_info.json")
if os.path.exists(file_path):
with open(file_path, 'r') as file:
bbox_data = json.load(file)
for key, values in bbox_data.items():
updated_key = f"{scene_id}/{key}"
updated_data[updated_key] = [{"bbox": val["bbox_visib"], "obj_id": obj_id} for val in values]
obj_id += 1
with open(output_file, 'w') as outfile:
json.dump(updated_data, outfile, indent=2)
print(f"The file was successfully created: {output_file}")
import re
import os
def convert_numbers(input_file, output_file, scene):
os.makedirs(output_file, exist_ok=True)
with open(input_file, 'r') as infile, open(output_file + "/keyframe.txt", 'a') as outfile:
for line in infile:
number = int(line.strip().split('.')[0])
outfile.write(f"000{scene}/{number}\n")
"""
Possible scenarios:
_world_occ_07.txt, _world_occ_05.txt, _world_occ_03.txt, _world_occ_01.txt,
_camera_occ_07.txt, _camera_occ_05.txt, _camera_occ_03.txt, _camera_occ_01.txt
"""
scenario = "_world_occ_07.txt"
scenario1 = scenario.split('.')[0]
objects = ["apple2", "apricot", "banana1", "kiwi1", "lemon2", "orange2", "peach1", "pear2"]
cleaned_objects = [re.sub(r'\d$', '', item) for item in objects]
for i, object in enumerate(objects):
fruit = objects[i]
scene = i
convert_numbers(f'/home/Downloads/Datasets/FruitBin/{object}/Splitting/splitting_occ_01_worlds/test_{fruit}{scenario}',
f'/home/Downloads/Datasets/BOP_format/image_sets', scene)
print(f"Data for scene {scene} ({object}) is ready")
import json
import os
path = '/home/Downloads/Datasets/BOP_format/test'
selected_images = []
directories = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
directories = sorted(directories)
for scene_id, fruit_folder in enumerate(directories):
print(scene_id)
fruit_path = os.path.join(path, fruit_folder)
print(fruit_path)
if os.path.isdir(fruit_path):
scene_gt_path = os.path.join(fruit_path, 'scene_gt.json')
if os.path.isfile(scene_gt_path):
with open(scene_gt_path, 'r') as file:
scene_gt_data = json.load(file)
for im_id, objects in scene_gt_data.items():
for obj in objects:
selected_images.append({
"im_id": int(im_id),
"inst_count": 1,
"obj_id": obj["obj_id"],
"scene_id": scene_id
})
output_path = os.path.join('/home/Downloads/Datasets/BOP_format/test_targets_bop19.json')
with open(output_path, 'w') as outfile:
json.dump(selected_images, outfile, indent=4)
print(f'File "test_targets_bop19.json" has been created with {len(selected_images)} entries.')
import os
import shutil
import numpy as np
import json
import re
import argparse
def parse_args():
parser = argparse.ArgumentParser(description="Preprocess data for different scenarios and categories.")
parser.add_argument('--src_directory', type=str, required=True, help='Source directory path.')
parser.add_argument('--dst_directory', type=str, required=True, help='Destination directory path.')
parser.add_argument('--scenario', type=str, default='_world_occ_07.txt', help='Scenario file.')
parser.add_argument('--categories', type=str, nargs='+', default=['train', 'test'], help='Data categories.')
return parser.parse_args()
def splitting(splitting_path, scenario, category, folder):
file_ids = []
fruit = re.sub(r'\d+$', '', folder)
split_file = os.path.join(splitting_path, f"{category}_{fruit}{scenario}")
if os.path.exists(split_file):
with open(split_file, 'r') as f:
lines = f.readlines()
for line in lines:
line = line.strip().split('.')[0]
if line:
file_ids.append(line)
return file_ids
def copy_specific_folder(dst_directory, src_directory, old_folder, new_folder, category, scenario):
os.makedirs(dst_directory, exist_ok=True)
folders_in_src = [d for d in os.listdir(src_directory) if os.path.isdir(os.path.join(src_directory, d))]
for folder in folders_in_src:
if category == "train":
new_folder_path = os.path.join(dst_directory, "train_pbr", folder)
else:
new_folder_path = os.path.join(dst_directory, category, folder)
os.makedirs(new_folder_path, exist_ok=True)
splitting_path = os.path.join(src_directory, folder, "Splitting")
file_names_to_copy = splitting(splitting_path, scenario, category, folder)
src_subfolder = os.path.join(src_directory, folder)
dst_subfolder = os.path.join(new_folder_path, new_folder)
os.makedirs(dst_subfolder, exist_ok=True)
for file_name in file_names_to_copy:
src_file_path = os.path.join(src_subfolder, old_folder, file_name + ".png")
if os.path.exists(src_file_path):
dst_file_path = os.path.join(dst_subfolder, file_name + ".png")
shutil.copy(src_file_path, dst_file_path)
new_file_name = f"{int(file_name):06}.png"
new_dst_file_path = os.path.join(dst_subfolder, new_file_name)
os.rename(dst_file_path, new_dst_file_path)
if not os.path.exists(os.path.join(new_folder_path, "scene_gt.json")):
process_directory(new_folder_path, src_directory, file_names_to_copy, folder)
def process_directory(new_folder_path, src_subfolder, file_names_to_copy, folder):
matrix = [543.25272224, 0., 320.25, 0., 724.33696299, 240.33333333, 0., 0., 1.]
obj_ids = {"apple2": 1, "apricot": 2, "banana1": 3, "kiwi1": 4, "lemon2": 5, "orange2": 6, "peach1": 7, "pear2": 8}
pose_transformed_path = os.path.join(src_subfolder, folder, "Pose_transformed")
all_objects_data = {}
if os.path.exists(pose_transformed_path):
for filename in os.listdir(pose_transformed_path):
if filename.endswith('.npy'):
id_file = filename.split(".")[0]
if id_file not in file_names_to_copy:
continue
file_path = os.path.join(pose_transformed_path, filename)
data = np.load(file_path)
cam_R_m2c = np.concatenate((data[0, :3], data[1, :3], data[2, :3]), axis=0).tolist()
cam_t_m2c = np.array(
(float(data[0, 3]) * 1000.0, float(data[1, 3]) * 1000.0, float(data[2, 3]) * 1000.0)).tolist()
all_objects_data[id_file] = {
"cam_R_m2c": cam_R_m2c,
"cam_t_m2c": cam_t_m2c,
"obj_id": obj_ids[folder]
}
path_to_scene_gt_json = os.path.join(new_folder_path, "scene_gt.json")
with open(path_to_scene_gt_json, 'w') as json_file:
json_file.write('{\n')
total_items = len(all_objects_data)
for idx, (id_file, object_data) in enumerate(all_objects_data.items()):
object_data_list = [object_data]
object_data_str = json.dumps(object_data_list, separators=(',', ':'))
json_file.write(f' "{id_file}": {object_data_str}')
if idx < total_items - 1:
json_file.write(',\n')
json_file.write('\n}')
bbox_path = os.path.join(src_subfolder, folder, "Bbox_resized")
all_bbox_data = {}
for filename in os.listdir(bbox_path):
if filename.endswith('.txt'):
id_file = filename.split(".")[0]
if id_file not in file_names_to_copy:
continue
file_path = os.path.join(bbox_path, filename)
bbox_data = np.loadtxt(file_path).reshape(-1)
top_left_x, top_left_y, bottom_right_x, bottom_right_y = bbox_data
width = bottom_right_x - top_left_x
height = bottom_right_y - top_left_y
all_bbox_data[id_file] = {
"bbox_visib": [int(top_left_x), int(top_left_y), int(width), int(height)]
}
path_to_scene_gt_info_json = os.path.join(new_folder_path, "scene_gt_info.json")
with open(path_to_scene_gt_info_json, 'w') as json_file:
json_file.write('{\n')
total_count = len(all_bbox_data)
for idx, (id_file, bbox_info) in enumerate(all_bbox_data.items()):
bbox_info_list = [bbox_info]
json_file.write(f' "{id_file}": {json.dumps(bbox_info_list, separators=(",", ":"))}')
if idx < total_count - 1:
json_file.write(',\n')
else:
json_file.write('\n')
json_file.write('}')
all_camera_data = {str(id_file): {"cam_K": matrix, "depth_scale": 0.1} for id_file in all_objects_data.keys()}
path_to_scene_camera_json = os.path.join(new_folder_path, "scene_camera.json")
with open(path_to_scene_camera_json, 'w') as json_file:
json_file.write('{\n')
for idx, (id_file, camera_info) in enumerate(all_camera_data.items()):
camera_info_str = json.dumps(camera_info, separators=(',', ':'))
json_file.write(f' "{id_file}": {camera_info_str}')
if idx < len(all_camera_data) - 1:
json_file.write(',\n')
else:
json_file.write('\n')
json_file.write('}')
return {"objects_data": all_objects_data, "bbox_data": all_bbox_data, "camera_data": all_camera_data}
if __name__ == "__main__":
args = parse_args()
src_directory = args.src_directory
dst_directory = args.dst_directory
scenario = args.scenario
categories = args.categories
for category in categories:
copy_specific_folder(dst_directory, src_directory, "RGB_resized", "rgb", category, scenario)
print(f"RGB files for the {category}ing dataset have been copied")
print(f".json gt files for the {category}ing dataset have been copied")
copy_specific_folder(dst_directory, src_directory, "Instance_Mask_resized", "mask_visib", category, scenario)
print(f"Masks for the {category}ing dataset have been copied")
copy_specific_folder(dst_directory, src_directory, "Depth_resized", "depth", category, scenario)
print(f"Depth files for the {category}ing dataset have been copied")
print(f"The {category}ing dataset is ready\n")
import os
def adjust_bbox(input_dir, output_dir):
for filename in os.listdir(input_dir):
if filename.endswith(".txt"):
input_file_path = os.path.join(input_dir, filename)
output_file_path = os.path.join(output_dir, filename)
with open(input_file_path, 'r') as f:
coords = f.readline().split()
x1, y1, x2, y2 = map(float, coords)
x1_new = x1 * (1 / 2)
y1_new = y1 * (2 / 3)
x2_new = x2 * (1 / 2)
y2_new = y2 * (2 / 3)
with open(output_file_path, 'w') as out_f:
out_f.write(f"{x1_new} {y1_new} {x2_new} {y2_new}\n")
input_dir = '/home/Downloads/Datasets/FruitBin/banana1/Bbox'
output_dir = '/home/Downloads/Datasets/FruitBin/banana1/Bbox_resized'
adjust_bbox(input_dir, output_dir)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment