Beispiel #1
0
def rig_model_from_json(key, obj):
    """
    Read a rig model from a json object
    """
    rig_model = pymap.RigModel(key)
    for key, rig_camera in obj["rig_cameras"].items():
        rig_model.add_rig_camera(rig_camera_from_json(key, rig_camera))
    return rig_model
Beispiel #2
0
def compute_relative_pose(rig_id, pose_instances):
    """ Compute a rig model relatives poses given poses grouped by rig instance. """

    # Put all poses instances into some canonical frame taken as the mean of their R|t
    centered_pose_instances = []
    for instance in pose_instances:
        origin_center = np.zeros(3)
        rotation_center = np.zeros(3)
        for shot, _ in instance:
            rotation_center += shot.pose.rotation
            origin_center += shot.pose.get_origin()
        origin_center /= len(instance)
        rotation_center /= len(instance)

        centered_pose_instance = []
        for shot, rig_camera_id in instance:
            instance_pose = pygeometry.Pose(rotation_center)
            instance_pose.set_origin(origin_center)
            instance_pose_camera = shot.pose.relative_to(instance_pose)
            centered_pose_instance.append((
                instance_pose_camera,
                rig_camera_id,
                shot.camera.id,
            ))
        centered_pose_instances.append(centered_pose_instance)

    # Average canonical poses per RigCamera ID
    average_origin, average_rotation, count_poses, camera_ids = {}, {}, {}, {}
    for centered_pose_instance in centered_pose_instances:
        for pose, rig_camera_id, camera_id in centered_pose_instance:
            if rig_camera_id not in average_origin:
                average_origin[rig_camera_id] = np.zeros(3)
                average_rotation[rig_camera_id] = np.zeros(3)
                count_poses[rig_camera_id] = 0
            average_origin[rig_camera_id] += pose.get_origin()
            average_rotation[rig_camera_id] += pose.rotation
            camera_ids[rig_camera_id] = camera_id
            count_poses[rig_camera_id] += 1

    # Construct final rig_model results
    rig_model = pymap.RigModel(rig_id)
    for rig_camera_id, count in count_poses.items():
        o = average_origin[rig_camera_id] / count
        r = average_rotation[rig_camera_id] / count
        pose = pygeometry.Pose(r)
        pose.set_origin(o)
        rig_model.add_rig_camera(pymap.RigCamera(pose, rig_camera_id))
    return rig_model
    def add_rig_camera_sequence(
        self,
        cameras,
        relative_positions,
        relative_rotations,
        start,
        length,
        height,
        interval,
        position_noise=None,
        rotation_noise=None,
        gps_noise=None,
    ):
        default_noise_interval = 0.25 * interval

        instances_positions, instances_rotations = sg.generate_cameras(
            sg.samples_generator_interval(start, length, interval,
                                          default_noise_interval),
            self.generator,
            height,
        )
        sg.perturb_points(instances_positions, position_noise)
        sg.perturb_rotations(instances_rotations, rotation_noise)

        shots_ids_per_camera = []
        for rig_camera_p, rig_camera_r, camera in zip(relative_positions,
                                                      relative_rotations,
                                                      cameras):
            pose_rig_camera = pygeometry.Pose(rig_camera_r)
            pose_rig_camera.set_origin(rig_camera_p)

            rotations = []
            positions = []
            for instance_p, instance_r in zip(instances_positions,
                                              instances_rotations):
                pose_instance = pygeometry.Pose(instance_r)
                pose_instance.set_origin(instance_p)
                composed = pose_rig_camera.compose(pose_instance)
                rotations.append(composed.rotation)
                positions.append(composed.get_origin())

            self.shot_rotations.append(rotations)
            self.shot_positions.append(positions)
            shift = sum(len(s) for s in shots_ids_per_camera)
            shots_ids_per_camera.append(
                [f"Shot {shift+i:04d}" for i in range(len(positions))])
            self.cameras.append(camera)
        self.shot_ids += shots_ids_per_camera

        rig_camera_ids = []
        rig_model = pymap.RigModel(f"RigModel {len(self.rig_models)}")
        for i, (rig_camera_p, rig_camera_r) in enumerate(
                zip(relative_positions, relative_rotations)):
            pose_rig_camera = pygeometry.Pose(rig_camera_r)
            pose_rig_camera.set_origin(rig_camera_p)
            rig_camera_id = f"RigCamera {i}"
            rig_camera = pymap.RigCamera(pose_rig_camera, rig_camera_id)
            rig_model.add_rig_camera(rig_camera)
            rig_camera_ids.append(rig_camera_id)
        self.rig_models.append(rig_model)

        rig_instances = []
        for i in range(len(instances_positions)):
            instance = []
            for j in range(len(shots_ids_per_camera)):
                instance.append(
                    (shots_ids_per_camera[j][i], rig_camera_ids[j]))
            rig_instances.append(instance)
        self.rig_instances.append(rig_instances)
        self.instances_positions.append(instances_positions)
        self.instances_rotations.append(instances_rotations)

        return self
def _create_rig_model():
    rig_model = pymap.RigModel("rig_model")
    rig_camera = pymap.RigCamera()
    rig_camera.id = "rig_camera"
    rig_model.add_rig_camera(rig_camera)
    return rig_model