|
import blenderproc as bproc |
|
import argparse |
|
import numpy as np |
|
|
|
from glob import glob |
|
import random |
|
from datetime import datetime |
|
random.seed(datetime.now().timestamp()) |
|
|
|
import sys |
|
import math |
|
|
|
|
|
parser = argparse.ArgumentParser() |
|
parser.add_argument('obj1', default="apple", help="Name of object 1") |
|
parser.add_argument('obj2', default="banana", help="Name of object 2") |
|
parser.add_argument('bg_path', default="background/autumn_park_2k.hdr", help="Path to the background hdr image") |
|
parser.add_argument('output_dir', default="output/above_below/", help="Path to where the final files, will be saved") |
|
parser.add_argument('pair_index', default=-1, help="Non-Random Pair index") |
|
parser.add_argument('debug', default=0) |
|
args = parser.parse_args() |
|
|
|
pair_index = int(args.pair_index) |
|
|
|
|
|
|
|
obj_list = ["apple", "banana", "bicycle", "book", "chair", "laptop", "person", "tv"] |
|
big_list = ['bicycle', 'person'] |
|
medium_list = ['chair', 'tv'] |
|
tiny_list = ['apple', 'banana'] |
|
small_list = ['book', 'laptop'] |
|
|
|
coco_objects = ['airplane', 'apple', 'backpack', 'banana', 'baseball_bat', 'baseball_glove', 'bear', 'bed', 'bench', 'bicycle', 'bird', 'boat', 'book', 'bottle', 'bowl', 'broccoli', 'bus', 'cake', 'car', 'carrot', 'cat', 'cell_phone', 'chair', 'clock', 'couch', 'cow', 'cup', 'dining_table', 'dog', 'donut', 'elephant', 'fire_hydrant', 'fork', 'frisbee', 'giraffe', 'hair_drier', 'handbag', 'horse', 'hot_dog', 'keyboard', 'kite', 'knife', 'laptop', 'microwave', 'motorcycle', 'mouse', 'orange', 'oven', 'parking_meter', 'person', 'pizza', 'potted_plant', 'refrigerator', 'remote', 'sandwich', 'scissors', 'sheep', 'sink', 'skateboard', 'skis', 'snowboard', 'spoon', 'sports_ball', 'stop_sign', 'suitcase', 'surfboard', 'teddy_bear', 'tennis_racket', 'tie', 'toaster', 'toilet', 'toothbrush', 'traffic_light', 'train', 'truck', 'tv', 'umbrella', 'vase', 'wine_glass', 'zebra'] |
|
wide_objects = ['airplane', 'bear', 'bus', 'bicycle','car', 'cat', 'couch', 'cup', 'cow', 'desk', 'dining_table', 'train', 'pig', 'giraffe', 'truck', 'tv', 'suitcase', 'motorcycle', 'horse'] |
|
|
|
|
|
def get_obj_scale(obj_name): |
|
return 1.0 |
|
|
|
def retrieve2obj(obj1="apple", obj2="banana", index=-1, |
|
asset_path="assets/blender_assets/", |
|
noncoco_asset_path = "assets/blender_assets_non_coco/"): |
|
|
|
if obj1 in coco_objects: |
|
obj1_list = sorted(glob(f'{asset_path}/{obj1}/*.blend')) |
|
else: |
|
obj1_list = sorted(glob(f'{noncoco_asset_path}/{obj1}/*.blend')) |
|
if obj2 in coco_objects: |
|
obj2_list = sorted(glob(f'{asset_path}/{obj2}/*.blend')) |
|
else: |
|
obj2_list = sorted(glob(f'{noncoco_asset_path}/{obj2}/*.blend')) |
|
|
|
if index == -1: |
|
obj1_path = random.choice(obj1_list) |
|
obj2_path = random.choice(obj2_list) |
|
else: |
|
obj1_path = obj1_list[index] |
|
obj2_path = obj2_list[index] |
|
return obj1_path, obj2_path, f'{obj1}_{obj2}' |
|
|
|
def retrieve_floor(bgpath): |
|
if 'white' in bgpath: |
|
floor_path = "floor/floor_white.blend" |
|
elif 'autumn' in bgpath: |
|
floor_path = "floor/floor_grass_brass.blend" |
|
elif 'studio' in bgpath: |
|
floor_path = 'floor/floor_wood.blend' |
|
return floor_path |
|
|
|
def get_obj_cam_coords(left, right, cam): |
|
""" |
|
cam +x, left -y , right +y |
|
cam -x, left +y, right -y |
|
cam +y, left +x, right -x |
|
cam -y, left -x, right +x |
|
""" |
|
positions = list() |
|
positions.append((cam, 0, 0, -left, 0, right)) |
|
|
|
return random.choice(positions) |
|
|
|
bproc.init() |
|
|
|
|
|
|
|
|
|
|
|
|
|
haven_hdri_path = args.bg_path |
|
|
|
for i in range(1): |
|
|
|
path1, path2, output_name = retrieve2obj(args.obj1, args.obj2, index=pair_index) |
|
|
|
bproc.world.set_world_background_hdr_img(haven_hdri_path, rotation_euler=[0.0, 0.0, random.uniform(-np.pi, np.pi)]) |
|
|
|
|
|
print('Generating', output_name) |
|
|
|
offset = 0.0 |
|
if args.obj2 in big_list : |
|
offset = 0.5 |
|
|
|
offset = random.uniform(0.3, 0.5) |
|
|
|
r = random.uniform(5.25, 6.25) |
|
|
|
cam_x, cam_y, left_x, left_y, right_x, right_y = get_obj_cam_coords(random.uniform(1.0, 1.5), random.uniform(1.0, 1.5), r) |
|
if args.obj1 in wide_objects: |
|
left_y = left_y - random.uniform(0.15, 0.25) |
|
if args.obj2 in wide_objects: |
|
right_y = right_y + random.uniform(0.15, 0.25) |
|
|
|
|
|
obj1_scale = get_obj_scale(args.obj1) |
|
obj2_scale = get_obj_scale(args.obj2) |
|
|
|
obj1 = bproc.loader.load_blend(path1, obj_types= ['armature','mesh', 'empty', 'hair'] ) |
|
poi1 = bproc.object.compute_poi(bproc.filter.all_with_type(obj1, bproc.types.MeshObject)) |
|
obj1 = bproc.object.merge_objects(obj1) |
|
obj1.set_scale([obj1_scale, obj1_scale, obj1_scale]) |
|
|
|
obj1.set_location([left_x, left_y, 0]) |
|
obj1.set_rotation_euler([0, 0, random.uniform(-np.pi/8, np.pi/8)]) |
|
|
|
obj2 = bproc.loader.load_blend(path2, obj_types= ['armature', 'mesh', 'empty', 'hair']) |
|
poi2 = bproc.object.compute_poi(bproc.filter.all_with_type(obj2, bproc.types.MeshObject)) |
|
obj2 = bproc.object.merge_objects(obj2) |
|
obj2.set_scale([obj2_scale, obj2_scale, obj2_scale]) |
|
obj2.set_location([right_x, right_y, 0]) |
|
|
|
floor = bproc.loader.load_blend(retrieve_floor(args.bg_path), obj_types= ['armature','mesh', 'empty', 'hair'] ) |
|
floor = bproc.object.merge_objects(floor) |
|
floor.set_location([0,0,0]) |
|
|
|
|
|
obj2.set_rotation_euler([0, 0, random.uniform(-np.pi/8, np.pi/8)]) |
|
|
|
|
|
light = bproc.types.Light() |
|
light.set_type("POINT") |
|
light.set_location([0, random.uniform(-5.0, 5.0), 5]) |
|
light.set_energy(3000) |
|
|
|
poi = (np.array([left_x, left_y, 0]) + np.array([right_x, right_y, 0])) / 2 |
|
poi[2] = poi[2] + offset |
|
|
|
|
|
|
|
|
|
|
|
|
|
bproc.camera.set_resolution(1024, 1024) |
|
|
|
location = bproc.sampler.part_sphere([cam_x, cam_y, 1.25], radius=0.25, part_sphere_dir_vector=[1, 0, 0], mode="SURFACE") |
|
|
|
rotation_matrix = bproc.camera.rotation_from_forward_vec(poi - location) |
|
|
|
cam2world_matrix = bproc.math.build_transformation_mat(location, rotation_matrix) |
|
bproc.camera.add_camera_pose(cam2world_matrix) |
|
|
|
|
|
|
|
bproc.renderer.set_max_amount_of_samples(24) |
|
data = bproc.renderer.render() |
|
|
|
|
|
bproc.writer.write_hdf5(args.output_dir + output_name, data, append_to_existing_output=True) |
|
print(path1, path2) |
|
|
|
|