Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gr.Image(label='Image Input', type='pil', tool='sketch', source="upload") doesn't support example input from both image and mask. #3110

Closed
Tracked by #466
xingqian2018 opened this issue Feb 1, 2023 · 1 comment · Fixed by #6169
Labels
enhancement New feature or request 🖼️ image Image component
Milestone

Comments

@xingqian2018
Copy link

I am trying to create an interface with masked image input and prepare some convenience examples, but it looks like Gradio doesn't support it.

import gradio as gr

with gr.Blocks() as demo:
    input_gallary = gr.Image(label='Image Input', type='pil', tool='sketch', source="upload")
    output_gallary = gr.Gallery(label="Image Result").style()

    gr.Examples(
        label='Examples',
        examples=[{'image' : 'image.png', 'mask' : 'mask.png'}],
        fn=lambda x:x,
        inputs=[input_gallary],
        outputs=[output_gallary],
        cache_examples=False, )

demo.launch(share=True)

Error message:

Exception has occurred: ValueError
Cannot process this value as an Image

I also try to hack through gr.Images with customized postprocess() function, but some other unrelated error shows up.

import gradio as gr

class Customized_Image_Block(gr.Image):
    def postprocess(self, y):
        from gradio import processing_utils
        from PIL import Image as _Image
        from pathlib import Path
        import numpy as np
        if y is None:
            return None
        if isinstance(y, np.ndarray):
            return processing_utils.encode_array_to_base64(y)
        elif isinstance(y, dict):
            return processing_utils.encode_url_or_file_to_base64(y['image'])
        elif isinstance(y, _Image.Image):
            return processing_utils.encode_pil_to_base64(y)
        elif isinstance(y, (str, Path)):
            return processing_utils.encode_url_or_file_to_base64(y)
        else:
            raise ValueError("Cannot process this value as an Image")

with gr.Blocks() as demo:
    input_gallary = Customized_Image_Block(label='Image Input', type='pil', tool='sketch', source="upload")
    output_gallary = gr.Gallery(label="Image Result").style()

    gr.Examples(
        label='Examples',
        examples=[{'image' : 'image.png', 'mask' : 'mask.png'}],
        fn=lambda x:x,
        inputs=[input_gallary],
        outputs=[output_gallary],
        cache_examples=False, )

demo.launch(share=True)

Error message:

Exception has occurred: AttributeError
'Dataset' object has no attribute 'headers'
@abidlabs abidlabs added the enhancement New feature or request label Feb 2, 2023
@pngwn
Copy link
Member

pngwn commented Feb 18, 2023

This is a duplicate of #2314. This isn't specific to examples, it is just that you can't load a mask for images right now.

@pngwn pngwn added the 🖼️ image Image component label Feb 22, 2023
@pngwn pngwn mentioned this issue Feb 22, 2023
20 tasks
@abidlabs abidlabs added this to the Component Cleanup milestone Jul 9, 2023
@abidlabs abidlabs modified the milestones: Component Cleanup, 4.0 Aug 10, 2023
@abidlabs abidlabs modified the milestones: 4.0, 4.0-image Oct 23, 2023
@pngwn pngwn mentioned this issue Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 🖼️ image Image component
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants