hustvl / Matte-Anything

[Image and Vision Computing (Vol.147 Jul. '24)] Interactive Natural Image Matting with Segment Anything Models
MIT License
465 stars 33 forks source link

Demo cannot work #23

Open ning22 opened 2 months ago

ning22 commented 2 months ago

image

No matter where I clicked after dropping an image, Error occurred! Do you have detailed instructions to run the demo?

f-chen165 commented 1 month ago

升级gradio包到4.39.0;使用下面的代码替换matte_anything.py中对应的部分 with gr.Blocks() as demo: gr.Markdown( """

Matte Anything🐒 !

        """
    )
    with gr.Row(equal_height=True):    #.style(equal_height=True):
        with gr.Column():
            # input image
            original_image = gr.State(value="numpy")   # store original image without points, default None
            input_image = gr.Image(type="numpy", label="Input Image")                             
            # prompt (point or text)
            # Point Input
            with gr.Tab(label='Point Input') as Tab1:
                with gr.Column():
                    selected_points = gr.State([])      # store points
                    radio = gr.Radio(['foreground_point', 'background_point'], label='Point Labels')
                    with gr.Row():
                        undo_button = gr.Button('Remove Point')
                        undo_all_button = gr.Button('Remove All  Points')
            # Foreground Text Input
            with gr.Tab(label='Foreground Text Input') as Tab2:
                with gr.Blocks():
                    gr.Markdown("Foreground Text Input")
                    fg_caption = gr.Textbox(lines=1, placeholder="the girl in the middle", label="foreground input text")                   

            # Save Config
            with gr.Tab(label='Save Config') as Tab3:
                with gr.Blocks():
                    gr.Markdown("save name")
                    save_dir = gr.Textbox(lines=1, placeholder="the girl in the middle", label="Give a name of your demo. It will be saved in ```your_demos/your_name.pny```")

            # run button
            button = gr.Button("Start!")

            # Trimap Settings
            with gr.Tab(label='Trimap Settings'):
                gr.Markdown("Trimap Settings")
                erode_kernel_size = gr.Slider(minimum=1, maximum=30, step=1, value=10, label="erode_kernel_size")
                dilate_kernel_size = gr.Slider(minimum=1, maximum=30, step=1, value=10, label="dilate_kernel_size")

            # Input Text Settings
            with gr.Tab(label='Input Text Settings'):
                gr.Markdown("Input Text Settings")
                fg_box_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, value=0.25, label="foreground_box_threshold")
                fg_text_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.001, value=0.25, label="foreground_text_threshold")

            # Transparency Settings
            with gr.Tab(label='Transparency Settings'):
                gr.Markdown("Transparency Settings")
                tr_caption = gr.Textbox(lines=1, placeholder="glass.lens.crystal.diamond.bubble.bulb.web.grid", label="transparency input text")
                tr_box_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.005, value=0.5, label="transparency_box_threshold")
                tr_text_threshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.005, value=0.25, label="transparency_text_threshold")

        with gr.Column():

            # show the image with mask
            with gr.Tab(label='SAM Mask'):
                mask = gr.Image(type='numpy')
            # with gr.Tab(label='Trimap'):
            #     trimap = gr.Image(type='numpy')
            with gr.Tab(label='Alpha Matte'):
                alpha = gr.Image(type='numpy')
            # show only mask
            with gr.Tab(label='Foreground by SAM Mask'):
                foreground_by_sam_mask = gr.Image(type='numpy')
            with gr.Tab(label='Refined by ViTMatte'):
                refined_by_vitmatte = gr.Image(type='numpy')
            # with gr.Tab(label='Transparency Detection'):
            #     transparency = gr.Image(type='numpy')
            with gr.Tab(label='New Background 1'):
                new_bg_1 = gr.Image(type='numpy')
            with gr.Tab(label='New Background 2'):
                new_bg_2 = gr.Image(type='numpy')
            with gr.Tab(label='New Background 3'):
                new_bg_3 = gr.Image(type='numpy')

    input_image.upload(
        store_img,
        [input_image],
        [original_image, selected_points]
    )
    input_image.select(
        get_point,
        [input_image, selected_points, radio],
        [input_image],
    )
    undo_button.click(
        undo_points,
        [original_image, selected_points],
        [input_image]
    )
    undo_all_button.click(
        undo_all_points,
        [original_image, selected_points],
        [input_image]
    )
    Tab1.select(
        clear_fg_caption,
        [fg_caption],
        [fg_caption]
    )
    Tab2.select(
        undo_all_points,
        [original_image, selected_points],
        [input_image]
    )

    button.click(run_inference, inputs=[original_image, selected_points, erode_kernel_size, dilate_kernel_size, fg_box_threshold, fg_text_threshold, fg_caption, tr_box_threshold, tr_text_threshold, \
                                        save_dir, tr_caption],  outputs=[mask, alpha, foreground_by_sam_mask, refined_by_vitmatte, new_bg_1, new_bg_2, new_bg_3])

    with gr.Row():
        with gr.Column():
            background_image = gr.State(value=None)

demo.launch()