Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +40 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
|
| 5 |
+
# Load object detection pipeline
|
| 6 |
+
detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
| 7 |
+
|
| 8 |
+
def detect_objects(image):
|
| 9 |
+
results = detector(image)
|
| 10 |
+
|
| 11 |
+
# Draw bounding boxes on the image
|
| 12 |
+
img = image.copy()
|
| 13 |
+
draw = ImageDraw.Draw(img)
|
| 14 |
+
|
| 15 |
+
for obj in results:
|
| 16 |
+
box = obj["box"]
|
| 17 |
+
label = obj["label"]
|
| 18 |
+
score = obj["score"]
|
| 19 |
+
|
| 20 |
+
# Rectangle around object
|
| 21 |
+
draw.rectangle(
|
| 22 |
+
[(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
|
| 23 |
+
outline="red", width=3
|
| 24 |
+
)
|
| 25 |
+
# Label text
|
| 26 |
+
draw.text((box["xmin"], box["ymin"] - 10), f"{label} ({score:.2f})", fill="red")
|
| 27 |
+
|
| 28 |
+
return img
|
| 29 |
+
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
gr.Markdown("## Object Detection App (with Bounding Boxes)")
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
input_img = gr.Image(type="pil", label="Upload an Image")
|
| 35 |
+
output_img = gr.Image(type="pil", label="Detected Objects")
|
| 36 |
+
|
| 37 |
+
btn = gr.Button("Detect Objects")
|
| 38 |
+
btn.click(fn=detect_objects, inputs=input_img, outputs=output_img)
|
| 39 |
+
|
| 40 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.30
|
| 2 |
+
transformers>=4.40
|
| 3 |
+
torch>=1.13
|