Spaces:
Sleeping
Sleeping
Clement Vachet commited on
Commit ·
3a4ff58
1
Parent(s): 2ccf6ca
Use gradio blocks for user interface
Browse files7 unmodified lines89101112131415165 unmodified lines22232425262717 unmodified lines454647484950517 unmodified lines596061626364656667686970717273747576777879807 unmodified linesfrom transformers import pipelineimport matplotlib.pyplot as pltimport iomodel_pipeline = pipeline(model="facebook/detr-resnet-50")COLORS = [[0.000, 0.447, 0.741],5 unmodified lines]def get_output_figure(pil_img, results, threshold):plt.figure(figsize=(16, 10))plt.imshow(pil_img)17 unmodified lines#@spaces.GPUdef detect(image, threshold=0.9):results = model_pipeline(image)print(results)7 unmodified linesreturn output_pil_imgwith gr.Blocks() as demo:gr.Markdown("# Object detection with DETR on COCO dataset")gr.Markdown("""This application uses a DETR (DEtection TRansformers) model to detect objects on images.This version was trained using the COCO dataset.You can load an image and see the predictions for the objects detected.""")gr.Interface(fn=detect,inputs=[gr.Image(label="Input image", type="pil"), \gr.Slider(0, 1.0, value=0.9, label='Threshold')],outputs=[gr.Image(label="Output prediction", type="pil")],examples=[['samples/savanna.jpg']],)demo.launch(show_error=True)7 unmodified lines8910111213141516175 unmodified lines232425262728293031323317 unmodified lines515253545556575859607 unmodified lines68697071727374757677787980818283848586878889909192939495969798991001011021031041051061077 unmodified linesfrom transformers import pipelineimport matplotlib.pyplot as pltimport ioimport oslist_models = ["facebook/detr-resnet-50"]list_models_simple = [os.path.basename(model) for model in list_models]COLORS = [[0.000, 0.447, 0.741],5 unmodified lines]def load_pipeline(model):model_pipeline = pipeline(model=model)return model_pipelinedef get_output_figure(pil_img, results, threshold):plt.figure(figsize=(16, 10))plt.imshow(pil_img)17 unmodified lines#@spaces.GPUdef detect(image, model_id, threshold=0.9):print("model:", list_models[model_id])model_pipeline = load_pipeline(list_models[model_id])results = model_pipeline(image)print(results)7 unmodified linesreturn output_pil_imgdef demo():with gr.Blocks(theme="base") as demo:gr.Markdown("# Object detection on COCO dataset")gr.Markdown("""This application uses transformer-based models to detect objects on images.This version was trained using the COCO dataset.You can load an image and see the predictions for the objects detected.""")with gr.Row():model_id = gr.Radio(list_models, \label="Detection models", value=list_models[0], type="index", info="Choose your detection model")with gr.Row():threshold = gr.Slider(0, 1.0, value=0.9, label='Detection threshold', info="Choose your detection threshold")with gr.Row():input_image = gr.Image(label="Input image", type="pil")output_image = gr.Image(label="Output image", type="pil")with gr.Row():submit_btn = gr.Button("Submit")clear_button = gr.ClearButton()gr.Examples(['samples/savanna.jpg'], inputs=input_image)submit_btn.click(fn=detect, inputs=[input_image, model_id, threshold], outputs=[output_image])clear_button.click(lambda: [None, None], \inputs=None, \outputs=[input_image, output_image], \queue=False)demo.queue().launch(debug=True)if __name__ == "__main__":demo()
app.py+49-22