When I click an example in the Example component, I want this action to trigger a function store_example_img(img)
. This function stores the selected image in gr.State()
, so that I can obtain the original image in the following process. But I can’t obtain the selected example image anyway, store_example_img
always gets None
. My code shows the following (key code):
original_image = gr.State(value=None, interactive=True) # store original image without points, default None
input_image = gr.Image(type="numpy")
example = gr.Examples(
examples=[os.path.join(os.path.dirname(__file__), "./images/53960-scaled.jpg"),
os.path.join(os.path.dirname(__file__), "./images/2388455-scaled.jpg"),
os.path.join(os.path.dirname(__file__), "./images/1.jpg"),
os.path.join(os.path.dirname(__file__), "./images/2.jpg"),
os.path.join(os.path.dirname(__file__), "./images/3.jpg"),
os.path.join(os.path.dirname(__file__), "./images/4.jpg"),
os.path.join(os.path.dirname(__file__), "./images/5.jpg"),
os.path.join(os.path.dirname(__file__), "./images/6.jpg"),
os.path.join(os.path.dirname(__file__), "./images/7.jpg"),
os.path.join(os.path.dirname(__file__), "./images/8.jpg"),
],
inputs=input_image,
outputs=output_image,
fn=store_example_img,
run_on_click=True,
)
def store_example_img(img):
return img
I’ve tried a lot of other ways, such as getting image via input_image.value
in store_example_img
, but still None. I am sure that store_example_img
worked when the Example is clicked. I suspect fn
is finished running before the image enters the input_image
.