Example #1
0
def get_scene_capture_and_texture(owner, observer):
    """
    Adds a SceneCapture2DComponent to some parent camera object so we can capture the pixels for this camera view.
    Then captures the image, renders it on the render target of the scene capture and returns the image as a numpy
    array.

    :param uobject owner: The owner camera/scene-capture actor to which the new SceneCapture2DComponent
        needs to be attached or for which the render target has to be created and/or returned.
    :param uobject observer: The MLObserver uobject.
    :return: numpy array containing the pixel values (0-255) of the captured image.
    :rtype: np.ndarray
    """

    texture = None  # the texture object to use for getting the image

    # look for first SceneCapture2DComponent
    scene_captures = owner.get_actor_components_by_type(
        SceneCaptureComponent2D)
    if len(scene_captures) > 0:
        scene_capture = scene_captures[0]
        texture = scene_capture.TextureTarget
        #ue.log("DEBUG: get_scene_capture_and_texture -> found a scene_capture; texture={}".format(texture))
    # then CameraComponent
    else:
        cameras = owner.get_actor_components_by_type(CameraComponent)
        if len(cameras) > 0:
            camera = cameras[0]
            scene_capture = get_child_component(camera,
                                                SceneCaptureComponent2D)
            if scene_capture:
                texture = scene_capture.TextureTarget
                #ue.log("DEBUG: get_scene_capture_and_texture -> found a camera with scene_capture comp; texture={}".format(texture))
            else:
                scene_capture = owner.add_actor_component(
                    SceneCaptureComponent2D, "MaRLEnE_SceneCapture", camera)
                scene_capture.bCaptureEveryFrame = False
                scene_capture.bCaptureOnMovement = False
                #ue.log("DEBUG: get_scene_capture_and_texture -> found a camera w/o scene_capture comp -> added it; texture={}".format(
                #    texture))
        # error -> return nothing
        else:
            raise RuntimeError(
                "Observer {} has bScreenCapture set to true, but its owner does not possess either a "
                "Camera or a SceneCapture2D!".format(observer.get_name()))

    if not texture:
        # use MLObserver's width/height settings
        texture = scene_capture.TextureTarget =\
            ue.create_transient_texture_render_target2d(observer.Width or 84, observer.Height or 84)
        #ue.log("DEBUG: scene capture is created in get_scene_image texture={} will return texture {}".format(scene_capture.TextureTarget, texture))

    return scene_capture, texture
Example #2
0
def get_scene_capture_and_texture(parent, obs_name, width=84, height=84):
    """
    Adds a SceneCapture2DComponent to some parent camera object so we can capture the pixels for this camera view.
    Then captures the image, renders it on the render target of the scene capture and returns the image as a numpy array.

    :param uobject parent: The parent camera/scene-capture actor/component to which the new SceneCapture2DComponent needs to be attached
    or for which the render target has to be created and/or returned
    :param str obs_name: The name of the observer component.
    :param int width: The width (in px) to use for the render target.
    :param int height: The height (in px) to use for the render target.
    :return: numpy array containing the pixel values (0-255) of the captured image
    :rtype: np.ndarray
    """

    texture = None  # the texture object to use for getting the image

    if parent.is_a(SceneCaptureComponent2D):
        scene_capture = parent
        texture = parent.TextureTarget
    elif parent.is_a(CameraComponent):
        scene_capture = get_child_component(parent, SceneCaptureComponent2D)
        if scene_capture:
            texture = scene_capture.TextureTarget
        else:
            scene_capture = parent.get_owner().add_actor_component(
                SceneCaptureComponent2D, "Engine2LearnScreenCapture", parent)
            scene_capture.bCaptureEveryFrame = False
            scene_capture.bCaptureOnMovement = False
    # error -> return nothing
    else:
        raise RuntimeError(
            "Observer {} has bScreenCapture set to true, but is not a child of either a Camera or a SceneCapture2D!"
            .format(obs_name))

    if not texture:
        # TODO: setup camera transform and options (greyscale, etc..)
        texture = scene_capture.TextureTarget = ue.create_transient_texture_render_target2d(
            width, height)
        ue.log("DEBUG: scene capture is created in get_scene_image texture={}".
               format(scene_capture.TextureTarget))

    return scene_capture, texture
Example #3
0
 def __init__(self):
     self.what_i_am_seeing = ue.create_transient_texture_render_target2d(512, 512)