Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,34 @@
|
|
|
|
|
| 1 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
import requests
|
| 5 |
|
| 6 |
-
|
| 7 |
-
image = Image.open(requests.get(url, stream=True).raw)
|
| 8 |
-
|
| 9 |
-
# you can specify the revision tag if you don't want the timm dependency
|
| 10 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 11 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
import requests
|
| 6 |
|
| 7 |
+
# Load the DETR model and processor
|
|
|
|
|
|
|
|
|
|
| 8 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 9 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 10 |
|
| 11 |
+
st.title("DETR Object Detection with ResNet-50")
|
| 12 |
+
st.write("Upload an image and let the DETR model detect objects in it.")
|
| 13 |
+
|
| 14 |
+
# File uploader in Streamlit
|
| 15 |
+
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
|
| 16 |
+
|
| 17 |
+
if uploaded_file is not None:
|
| 18 |
+
# Load and display the image
|
| 19 |
+
image = Image.open(uploaded_file)
|
| 20 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 21 |
+
|
| 22 |
+
# Process the image and perform object detection
|
| 23 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
|
| 26 |
+
# Post-process the results to get bounding boxes and labels with confidence > 0.9
|
| 27 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 28 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
|
|
|
| 29 |
|
| 30 |
+
# Display results
|
| 31 |
+
st.write("Detected objects:")
|
| 32 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 33 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 34 |
+
st.write(f"{model.config.id2label[label.item()]}: {round(score.item(), 3)} at location {box}")
|
|
|