Being able to choose between one input or another in Gradio
Hello, I made an object detection model and I managed to display predictions in Gradio using a webcam or uploading a picture, but in separated gradio apps, using as input:input_1 = gr.inputs.Image(type='pil', label="Original Image", source="upload")orinput_2 = gr.inputs.Image(type='pil', label="Original Image", source="webcam")I’d like to have both options in the same app, so users would be able to choose between uploading a pictureormaking a picture using the webcam. I tried to pass both inputs as a list:inputs = [input_1, input_2]When I try to make the prediction the output image shows an error message. I tried setting the optional property in the inputs as True, but it still shows the error.The interface goes like this:input = gr.inputs.Image(type='pil', label="Original Image", source="upload", optional=True)
input_2 = gr.inputs.Image(type='pil', label="Original Image", source="webcam", optional=True)
inputs = [input, input_2]
outputs = gr.outputs.Image(type="pil", label="Output Image")
title = "Object detection with Yolov5"

gr.Interface(predict,
             inputs, 
             outputs, 
             title=title,
             theme="huggingface").launch(enable_queue=True)And the predict function goes like this:def predict(im, size=640):
    g = (size / max(im.size))  # gain
    im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS)  # resize

    results = model(im)  # inference
    results.render()  # updates results.imgs with boxes and labels
    return Image.fromarray(results.imgs[0])error gradio2248×1125 129 KBIs there a way to make this or I should just keep the inputs as separated apps?Thanks in advance
2022-05-20T01:01:34Z
I think the issue lies in how you’re using yourpredictfunction. Gradio passes each input in order to yourpredictfunction, so right now if you havedef predict(im, size=640):and your inputs areinputs = [input, input_2]theninputis being passed intopredictasim, andinput_2is being passed in assize. In other words, the uploaded image is being set toim, and the webcam image (even if it’sNone) is being set tosize. You should likely rename those parameters to be something like:def predict(uploaded_image, webcam_image):and then you can write some logic to decide which of the variables to use in that function. Alternatively, you could usea tabbed interface!
2022-05-20T09:04:16Z
Solved! Thank you