Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from PIL import Image
|
|
4 |
import gradio as gr
|
5 |
from transformers import pipeline
|
6 |
import matplotlib.pyplot as plt
|
|
|
7 |
|
8 |
# Initialize the models
|
9 |
detector50 = pipeline(model="facebook/detr-resnet-50")
|
@@ -50,13 +51,37 @@ def infer(model, in_pil_img):
|
|
50 |
results = detector101(in_pil_img) if model == "detr-resnet-101" else detector50(in_pil_img)
|
51 |
return get_figure(in_pil_img, results)
|
52 |
|
|
|
|
|
|
|
|
|
|
|
53 |
# Define Gradio interface
|
54 |
with gr.Blocks() as demo:
|
55 |
gr.Markdown("## DETR Object Detection")
|
56 |
model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
input_image = gr.Image(label="Input image", type="pil")
|
58 |
output_image = gr.Image(label="Output image")
|
59 |
send_btn = gr.Button("Infer")
|
|
|
|
|
60 |
send_btn.click(fn=infer, inputs=[model, input_image], outputs=output_image)
|
61 |
|
62 |
demo.launch()
|
|
|
4 |
import gradio as gr
|
5 |
from transformers import pipeline
|
6 |
import matplotlib.pyplot as plt
|
7 |
+
import requests
|
8 |
|
9 |
# Initialize the models
|
10 |
detector50 = pipeline(model="facebook/detr-resnet-50")
|
|
|
51 |
results = detector101(in_pil_img) if model == "detr-resnet-101" else detector50(in_pil_img)
|
52 |
return get_figure(in_pil_img, results)
|
53 |
|
54 |
+
def url_to_image(url):
|
55 |
+
response = requests.get(url)
|
56 |
+
img = Image.open(io.BytesIO(response.content))
|
57 |
+
return img
|
58 |
+
|
59 |
# Define Gradio interface
|
60 |
with gr.Blocks() as demo:
|
61 |
gr.Markdown("## DETR Object Detection")
|
62 |
model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
|
63 |
+
|
64 |
+
# Example images from the internet
|
65 |
+
example_urls = [
|
66 |
+
"https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png",
|
67 |
+
"https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Golde33443.jpg/800px-Golde33443.jpg",
|
68 |
+
"https://upload.wikimedia.org/wikipedia/commons/e/e3/Brazilian_Jaguar.jpg",
|
69 |
+
"https://upload.wikimedia.org/wikipedia/commons/f/f9/Big_Sur%2C_California.jpg",
|
70 |
+
"https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif"
|
71 |
+
]
|
72 |
+
|
73 |
+
examples = gr.Examples(
|
74 |
+
examples=example_urls,
|
75 |
+
inputs=[gr.Image(type="pil")],
|
76 |
+
fn=lambda x: url_to_image(x),
|
77 |
+
label="Try these example images"
|
78 |
+
)
|
79 |
+
|
80 |
input_image = gr.Image(label="Input image", type="pil")
|
81 |
output_image = gr.Image(label="Output image")
|
82 |
send_btn = gr.Button("Infer")
|
83 |
+
|
84 |
+
# Trigger inference on button click
|
85 |
send_btn.click(fn=infer, inputs=[model, input_image], outputs=output_image)
|
86 |
|
87 |
demo.launch()
|