Spaces:
Sleeping
Sleeping
Add all files
Browse files- README.md +14 -3
- app.py +40 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,13 +1,24 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: purple
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.11.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Object Detection with DETR
|
| 3 |
+
emoji: π
|
| 4 |
colorFrom: purple
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.11.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
+
tags:
|
| 12 |
+
- object-detection
|
| 13 |
+
- vision
|
| 14 |
+
- transformers
|
| 15 |
+
- detr
|
| 16 |
+
- deep-learning
|
| 17 |
+
- gradio
|
| 18 |
+
datasets:
|
| 19 |
+
- coco
|
| 20 |
+
models:
|
| 21 |
+
- facebook/detr-resnet-50
|
| 22 |
---
|
| 23 |
|
| 24 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
|
| 5 |
+
# Load pre-trained model and image processor
|
| 6 |
+
model_name = "facebook/detr-resnet-50"
|
| 7 |
+
model = DetrForObjectDetection.from_pretrained(model_name)
|
| 8 |
+
processor = DetrImageProcessor.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Define function for object detection
|
| 11 |
+
def detect_objects(image):
|
| 12 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 13 |
+
outputs = model(**inputs)
|
| 14 |
+
|
| 15 |
+
# Get predictions
|
| 16 |
+
target_sizes = [image.size[::-1]] # (height, width)
|
| 17 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
| 18 |
+
|
| 19 |
+
# Draw bounding boxes on the image
|
| 20 |
+
draw = ImageDraw.Draw(image)
|
| 21 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 22 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 23 |
+
draw.rectangle(box, outline="red", width=3)
|
| 24 |
+
label_name = model.config.id2label[label.item()]
|
| 25 |
+
draw.text((box[0], box[1]), f"{label_name} ({score:.2f})", fill="red")
|
| 26 |
+
|
| 27 |
+
return image
|
| 28 |
+
|
| 29 |
+
# Create Gradio interface
|
| 30 |
+
interface = gr.Interface(
|
| 31 |
+
fn=detect_objects,
|
| 32 |
+
inputs=gr.Image(type="pil"),
|
| 33 |
+
outputs=gr.Image(type="pil"),
|
| 34 |
+
title="Object Detection App",
|
| 35 |
+
description="Upload an image to detect objects using the DETR model."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Launch the app
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
gradio
|
| 3 |
+
transformers
|
| 4 |
+
pillow
|
| 5 |
+
timm
|