Beispiel #1
0
    def __init__(self, config):
        CameraModule.__init__(self, config)
        self.bvh_tree = None

        self.rotations = []
        self.translations = []

        self.var_rot, self.var_translation = 0.0, 0.0
        self.check_pose_novelty_rot = self.config.get_bool(
            "check_pose_novelty_rot", False)
        self.check_pose_novelty_translation = self.config.get_bool(
            "check_pose_novelty_translation", False)

        self.min_var_diff_rot = self.config.get_float("min_var_diff_rot",
                                                      sys.float_info.min)
        if self.min_var_diff_rot == -1.0:
            self.min_var_diff_rot = sys.float_info.min

        self.min_var_diff_translation = self.config.get_float(
            "min_var_diff_translation", sys.float_info.min)
        if self.min_var_diff_translation == -1.0:
            self.min_var_diff_translation = sys.float_info.min

        self.cam_pose_collection = ItemCollection(
            self._sample_cam_poses,
            self.config.get_raw_dict("default_cam_param", {}))
 def __init__(self, config):
     CameraModule.__init__(self, config)
     # A dict specifying the length of parameters that require more than one argument. If not specified, 1 is assumed.
     self.number_of_arguments_per_parameter = {
         "location": 3,
         "rotation/value": 3
     }
 def __init__(self, config):
     CameraModule.__init__(self, config)
     # A dict specifying the length of parameters that require more than one argument. If not specified, 1 is assumed.
     self.number_of_arguments_per_parameter = {
         "location": 3,
         "rotation/value": 3
     }
     self.cam_pose_collection = ItemCollection(self._add_cam_pose, self.config.get_raw_dict("default_cam_param", {}))
    def __init__(self, config):
        CameraModule.__init__(self, config)

        self.rotation_ranges = [
            self.config.get_list("rotation_range_x", [90, 90]),
            self.config.get_list("rotation_range_y", [0, 0]),
            self.config.get_list("rotation_range_z", [])
        ]
        self.sqrt_number_of_rays = self.config.get_int("sqrt_number_of_rays",
                                                       10)

        self.proximity_checks = self.config.get_raw_dict(
            "proximity_checks", [])

        self.bvh_tree = None
Beispiel #5
0
    def run(self):
        """ Load BOP data """

        bop_dataset_path = self.config.get_string("bop_dataset_path")
        scene_id = self.config.get_int("scene_id", -1)
        obj_ids = self.config.get_list("obj_ids", [])
        split = self.config.get_string("split", "test")
        model_type = self.config.get_string("model_type", "")
        cam_type = self.config.get_string("cam_type", "")
        scale = 0.001 if self.config.get_bool("mm2m", False) else 1
        datasets_path = os.path.dirname(bop_dataset_path)
        dataset = os.path.basename(bop_dataset_path)

        print("bob: {}, dataset_path: {}".format(bop_dataset_path,
                                                 datasets_path))
        print("dataset: {}".format(dataset))

        try:
            from bop_toolkit_lib import dataset_params, inout
        except ImportError as error:
            print(
                'ERROR: Please download the bop_toolkit package and add it to sys_paths in config!'
            )
            print('https://github.com/thodan/bop_toolkit')
            raise error

        model_p = dataset_params.get_model_params(
            datasets_path,
            dataset,
            model_type=model_type if model_type else None)
        cam_p = dataset_params.get_camera_params(
            datasets_path, dataset, cam_type=cam_type if cam_type else None)
        bpy.data.scenes["Scene"]["num_labels"] = len(model_p['obj_ids'])

        try:
            split_p = dataset_params.get_split_params(datasets_path,
                                                      dataset,
                                                      split=split)
        except ValueError:
            raise Exception(
                "Wrong path or {} split does not exist in {}.".format(
                    split, dataset))

        bpy.context.scene.world["category_id"] = 0
        bpy.context.scene.render.resolution_x = self.config.get_int(
            "resolution_x", split_p['im_size'][0])
        bpy.context.scene.render.resolution_y = self.config.get_int(
            "resolution_y", split_p['im_size'][1])

        # Collect camera and camera object
        cam_ob = bpy.context.scene.camera
        cam = cam_ob.data
        cam['loaded_resolution'] = bpy.context.scene.render.resolution_x, bpy.context.scene.render.resolution_y
        cam['loaded_intrinsics'] = cam_p[
            'K']  # load default intrinsics from camera.json

        config = Config({})
        camera_module = CameraModule(config)
        camera_module._set_cam_intrinsics(cam, config)

        #only load all/selected objects here, use other modules for setting poses, e.g. camera.CameraSampler / object.ObjectPoseSampler
        if scene_id == -1:
            obj_ids = obj_ids if obj_ids else model_p['obj_ids']
            for obj_id in obj_ids:
                self._load_mesh(obj_id, model_p, scale=scale)
        # replicate scene: load scene objects, object poses, camera intrinsics and camera poses
        else:
            sc_gt = inout.load_scene_gt(
                split_p['scene_gt_tpath'].format(**{'scene_id': scene_id}))
            sc_camera = inout.load_json(
                split_p['scene_camera_tpath'].format(**{'scene_id': scene_id}))

            for i, (cam_id, insts) in enumerate(sc_gt.items()):

                cam_K, cam_H_m2c_ref = self._get_ref_cam_extrinsics_intrinsics(
                    sc_camera, cam_id, insts, scale)

                if i == 0:
                    # define world = first camera
                    cam_H_m2w_ref = cam_H_m2c_ref.copy()

                    # load scene objects
                    for inst in insts:
                        cur_obj = self._load_mesh(inst['obj_id'], model_p)
                        self.set_object_pose(cur_obj, inst, scale)

                cam_H_c2w = self._compute_camera_to_world_trafo(
                    cam_H_m2w_ref, cam_H_m2c_ref)

                #set camera intrinsics and extrinsics
                config = Config({
                    "cam2world_matrix": list(cam_H_c2w.flatten()),
                    "camK": list(cam_K.flatten())
                })
                camera_module._set_cam_intrinsics(cam, config)
                camera_module._set_cam_extrinsics(cam_ob, config)

                # Store new cam pose as next frame
                frame_id = bpy.context.scene.frame_end
                camera_module._insert_key_frames(cam, cam_ob, frame_id)
                bpy.context.scene.frame_end = frame_id + 1
Beispiel #6
0
 def __init__(self, config):
     CameraModule.__init__(self, config)
     self.bvh_tree = None
     self.cam_pose_collection = ItemCollection(
         self._sample_cam_poses,
         self.config.get_raw_dict("default_cam_param", {}))
Beispiel #7
0
    def run(self):
        """ Load BOP data """

        datasets_path = os.path.dirname(self.bop_dataset_path)
        dataset = os.path.basename(self.bop_dataset_path)

        print("bob: {}, dataset_path: {}".format(self.bop_dataset_path,
                                                 datasets_path))
        print("dataset: {}".format(dataset))

        try:
            from bop_toolkit_lib import dataset_params, inout
        except ImportError as error:
            print(
                'ERROR: Please download the bop_toolkit package and add it to sys_paths in config!'
            )
            print('https://github.com/thodan/bop_toolkit')
            raise error

        model_p = dataset_params.get_model_params(
            datasets_path,
            dataset,
            model_type=self.model_type if self.model_type else None)
        cam_p = dataset_params.get_camera_params(
            datasets_path,
            dataset,
            cam_type=self.cam_type if self.cam_type else None)
        bpy.data.scenes["Scene"]["num_labels"] = len(model_p['obj_ids'])

        try:
            split_p = dataset_params.get_split_params(datasets_path,
                                                      dataset,
                                                      split=self.split)
        except ValueError:
            raise Exception(
                "Wrong path or {} split does not exist in {}.".format(
                    self.split, dataset))

        bpy.context.scene.world["category_id"] = 0
        bpy.context.scene.render.resolution_x = split_p['im_size'][0]
        bpy.context.scene.render.resolution_y = split_p['im_size'][1]

        # Collect camera and camera object
        cam_ob = bpy.context.scene.camera
        cam = cam_ob.data
        cam['loaded_resolution'] = split_p['im_size'][0], split_p['im_size'][1]
        # load default intrinsics from camera.json
        cam['loaded_intrinsics'] = cam_p['K']

        config = Config({})
        camera_module = CameraModule(config)
        camera_module._set_cam_intrinsics(cam, config)

        loaded_objects = []

        # only load all/selected objects here, use other modules for setting poses
        # e.g. camera.CameraSampler / object.ObjectPoseSampler
        if self.scene_id == -1:
            obj_ids = self.obj_ids if self.obj_ids else model_p['obj_ids']
            # if sampling is enabled
            if self.sample_objects:
                loaded_ids = {}
                loaded_amount = 0
                if self.obj_instances_limit != -1 and len(
                        obj_ids
                ) * self.obj_instances_limit < self.num_of_objs_to_sample:
                    raise RuntimeError(
                        "{}'s {} split contains {} objects, {} object where requested to sample with "
                        "an instances limit of {}. Raise the limit amount or decrease the requested "
                        "amount of objects.".format(self.bop_dataset_path,
                                                    self.split, len(obj_ids),
                                                    self.num_of_objs_to_sample,
                                                    self.obj_instances_limit))
                while loaded_amount != self.num_of_objs_to_sample:
                    random_id = choice(obj_ids)
                    if random_id not in loaded_ids.keys():
                        loaded_ids.update({random_id: 0})
                    # if there is no limit or if there is one, but it is not reached for this particular object
                    if self.obj_instances_limit == -1 or loaded_ids[
                            random_id] < self.obj_instances_limit:
                        cur_obj = self._load_mesh(random_id,
                                                  model_p,
                                                  scale=self.scale)
                        loaded_ids[random_id] += 1
                        loaded_amount += 1
                        loaded_objects.append(cur_obj)
                    else:
                        print(
                            "ID {} was loaded {} times with limit of {}. Total loaded amount {} while {} are "
                            "being requested".format(
                                random_id, loaded_ids[random_id],
                                self.obj_instances_limit, loaded_amount,
                                self.num_of_objs_to_sample))
            else:
                for obj_id in obj_ids:
                    cur_obj = self._load_mesh(obj_id,
                                              model_p,
                                              scale=self.scale)
                    loaded_objects.append(cur_obj)
            self._set_properties(loaded_objects)

        # replicate scene: load scene objects, object poses, camera intrinsics and camera poses
        else:
            sc_gt = inout.load_scene_gt(split_p['scene_gt_tpath'].format(
                **{'scene_id': self.scene_id}))
            sc_camera = inout.load_json(split_p['scene_camera_tpath'].format(
                **{'scene_id': self.scene_id}))
            for i, (cam_id, insts) in enumerate(sc_gt.items()):
                cam_K, cam_H_m2c_ref = self._get_ref_cam_extrinsics_intrinsics(
                    sc_camera, cam_id, insts, self.scale)

                if i == 0:
                    # define world = first camera
                    cam_H_m2w_ref = cam_H_m2c_ref.copy()

                    cur_objs = []
                    # load scene objects and set their poses
                    for inst in insts:
                        cur_objs.append(
                            self._load_mesh(inst['obj_id'],
                                            model_p,
                                            scale=self.scale))
                        self.set_object_pose(cur_objs[-1], inst, self.scale)

                cam_H_c2w = self._compute_camera_to_world_trafo(
                    cam_H_m2w_ref, cam_H_m2c_ref)
                #set camera intrinsics and extrinsics
                config = Config({
                    "cam2world_matrix": list(cam_H_c2w.flatten()),
                    "cam_K": list(cam_K.flatten())
                })
                camera_module._set_cam_intrinsics(cam, config)
                camera_module._set_cam_extrinsics(cam_ob, config)

                # Store new cam pose as next frame
                frame_id = bpy.context.scene.frame_end
                # Copy object poses to next key frame (to be sure)
                for cur_obj in cur_objs:
                    self._insert_key_frames(cur_obj, frame_id)
                camera_module._insert_key_frames(cam, cam_ob, frame_id)
                bpy.context.scene.frame_end = frame_id + 1
Beispiel #8
0
    def run(self):
        bop_dataset_path = self.config.get_string("bop_dataset_path")
        scene_id = self.config.get_int("scene_id")
        split = self.config.get_string("split", "test")
        model_type = self.config.get_string("model_type", "")
        mm2m = 0.001 if self.config.get_bool("mm2m") else 1

        datasets_path = os.path.dirname(bop_dataset_path)
        dataset = os.path.basename(bop_dataset_path)
        print("bob: {}, dataset_path: {}".format(bop_dataset_path,
                                                 datasets_path))
        print("dataset: {}".format(dataset))

        model_p = dataset_params.get_model_params(
            datasets_path,
            dataset,
            model_type=model_type if model_type else None)
        camera_p = dataset_params.get_camera_params(datasets_path, dataset)

        try:
            split_p = dataset_params.get_split_params(datasets_path,
                                                      dataset,
                                                      split=split)
        except ValueError:
            raise Exception(
                "Wrong path or {} split does not exist in {}.".format(
                    split, dataset))

        sc_gt = inout.load_scene_gt(
            split_p['scene_gt_tpath'].format(**{'scene_id': scene_id}))
        sc_camera = inout.load_json(
            split_p['scene_camera_tpath'].format(**{'scene_id': scene_id}))

        bpy.context.scene.render.resolution_x = self.config.get_int(
            "resolution_x", split_p['im_size'][0])
        bpy.context.scene.render.resolution_y = self.config.get_int(
            "resolution_y", split_p['im_size'][1])
        #bpy.context.scene.render.pixel_aspect_x = self.config.get_float("pixel_aspect_x", 1) #split_p['im_size'][0] / split_p['im_size'][1])

        cm = CameraModule(self.config)

        for i, (cam_id, insts) in enumerate(sc_gt.items()):

            cam_K = np.array(sc_camera[str(cam_id)]['cam_K']).reshape(3, 3)

            cam_H_m2c_ref = np.eye(4)
            cam_H_m2c_ref[:3, :3] = np.array(insts[0]['cam_R_m2c']).reshape(
                3, 3)
            cam_H_m2c_ref[:3, 3] = np.array(
                insts[0]['cam_t_m2c']).reshape(3) * mm2m

            if i == 0:
                # define world = first camera
                cam_H_m2w_ref = cam_H_m2c_ref.copy()

                for inst in insts:

                    bpy.ops.import_mesh.ply(
                        filepath=model_p['model_tpath'].format(
                            **{'obj_id': inst['obj_id']}))

                    cam_H_m2c = np.eye(4)
                    cam_H_m2c[:3, :3] = np.array(inst['cam_R_m2c']).reshape(
                        3, 3)
                    cam_H_m2c[:3, 3] = np.array(
                        inst['cam_t_m2c']).reshape(3) * mm2m

                    # world = camera @ i=0
                    cam_H_m2w = cam_H_m2c
                    print('-----------------------------')
                    print("Model: {}".format(cam_H_m2w))
                    print('-----------------------------')

                    cur_obj = bpy.context.selected_objects[-1]
                    cur_obj.matrix_world = Matrix(cam_H_m2w)
                    cur_obj.scale = Vector((mm2m, mm2m, mm2m))

                    mat = self._load_materials(cur_obj)
                    self._link_col_node(mat)

            cam_H_c2w = np.dot(cam_H_m2w_ref, np.linalg.inv(cam_H_m2c_ref))

            print('-----------------------------')
            print("Cam: {}".format(cam_H_c2w))
            print('-----------------------------')

            config = {"location": [0, 0, 0], "rotation": list([0, 0, 0])}
            cm._add_cam_pose(Config(config), Matrix(cam_H_c2w), cam_K)
    def run(self):

        bop_dataset_path = self.config.get_string("bop_dataset_path")
        scene_id = self.config.get_int("scene_id", -1)
        obj_ids = self.config.get_list("obj_ids", [])
        split = self.config.get_string("split", "test")
        model_type = self.config.get_string("model_type", "")
        cam_type = self.config.get_string("cam_type", "")
        mm2m = 0.001 if self.config.get_bool("mm2m", False) else 1
        datasets_path = os.path.dirname(bop_dataset_path)
        dataset = os.path.basename(bop_dataset_path)

        print("bob: {}, dataset_path: {}".format(bop_dataset_path,
                                                 datasets_path))
        print("dataset: {}".format(dataset))

        try:
            from bop_toolkit_lib import dataset_params, inout
        except ImportError as error:
            print(
                'ERROR: Please download the bop_toolkit package and add it to sys_paths in config!'
            )
            print('https://github.com/thodan/bop_toolkit')
            raise error

        model_p = dataset_params.get_model_params(
            datasets_path,
            dataset,
            model_type=model_type if model_type else None)
        cam_p = dataset_params.get_camera_params(
            datasets_path, dataset, cam_type=cam_type if cam_type else None)
        bpy.data.scenes["Scene"]["num_labels"] = len(model_p['obj_ids'])

        try:
            split_p = dataset_params.get_split_params(datasets_path,
                                                      dataset,
                                                      split=split)
        except ValueError:
            raise Exception(
                "Wrong path or {} split does not exist in {}.".format(
                    split, dataset))

        bpy.context.scene.world["category_id"] = 0
        bpy.context.scene.render.resolution_x = self.config.get_int(
            "resolution_x", split_p['im_size'][0])
        bpy.context.scene.render.resolution_y = self.config.get_int(
            "resolution_y", split_p['im_size'][1])

        # Collect camera and camera object
        cam_ob = bpy.context.scene.camera
        cam = cam_ob.data
        cam['loaded_resolution'] = bpy.context.scene.render.resolution_x, bpy.context.scene.render.resolution_y
        cam['loaded_intrinsics'] = cam_p[
            'K']  # load default intrinsics from camera.json

        #only load all/selected objects here, use other modules for setting poses, e.g. camera.CameraSampler / object.ObjectPoseSampler
        if scene_id == -1:
            obj_ids = obj_ids if obj_ids else model_p['obj_ids']
            for obj_id in obj_ids:
                self._load_mesh(obj_id, model_p, mm2m=mm2m)
        # replicate scene: load scene objects, object poses, camera intrinsics and camera poses
        else:
            sc_gt = inout.load_scene_gt(
                split_p['scene_gt_tpath'].format(**{'scene_id': scene_id}))
            sc_camera = inout.load_json(
                split_p['scene_camera_tpath'].format(**{'scene_id': scene_id}))

            cm = CameraModule(self.config)

            for i, (cam_id, insts) in enumerate(sc_gt.items()):

                cam_K = np.array(sc_camera[str(cam_id)]['cam_K']).reshape(3, 3)

                cam_H_m2c_ref = np.eye(4)
                cam_H_m2c_ref[:3, :3] = np.array(
                    insts[0]['cam_R_m2c']).reshape(3, 3)
                cam_H_m2c_ref[:3, 3] = np.array(
                    insts[0]['cam_t_m2c']).reshape(3) * mm2m

                if i == 0:
                    # define world = first camera
                    cam_H_m2w_ref = cam_H_m2c_ref.copy()

                    for inst in insts:
                        cur_obj = self._load_mesh(inst['obj_id'],
                                                  model_p,
                                                  mm2m=mm2m)

                        cam_H_m2c = np.eye(4)
                        cam_H_m2c[:3, :3] = np.array(
                            inst['cam_R_m2c']).reshape(3, 3)
                        cam_H_m2c[:3, 3] = np.array(
                            inst['cam_t_m2c']).reshape(3) * mm2m

                        # world = camera @ i=0
                        cam_H_m2w = cam_H_m2c
                        print('-----------------------------')
                        print("Model: {}".format(cam_H_m2w))
                        print('-----------------------------')

                        cur_obj.matrix_world = Matrix(cam_H_m2w)

                cam_H_c2w = np.dot(cam_H_m2w_ref, np.linalg.inv(cam_H_m2c_ref))

                print('-----------------------------')
                print("Cam: {}".format(cam_H_c2w))
                print('-----------------------------')

                config = {"location": [0, 0, 0], "rotation": list([0, 0, 0])}
                cm._add_cam_pose(Config(config), Matrix(cam_H_c2w), cam_K)
 def __init__(self, config):
     CameraModule.__init__(self, config, False)
     self.location_poly = config.get_list("cam_poses/location_poly")
     self.look_at_poly = config.get_list("cam_poses/look_at_poly")
     self.intri_config = Config(config.get_raw_dict('intrinsics'))