Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import DetrForObjectDetection,
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
-
# Load model and
|
| 6 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 7 |
-
|
| 8 |
|
| 9 |
st.title("Context-Aware Object Detection")
|
| 10 |
|
|
@@ -14,8 +15,19 @@ if uploaded_file is not None:
|
|
| 14 |
image = Image.open(uploaded_file)
|
| 15 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
inputs =
|
| 19 |
outputs = model(**inputs)
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import DetrForObjectDetection, DetrImageProcessor
|
| 3 |
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Load model and processor
|
| 7 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 8 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 9 |
|
| 10 |
st.title("Context-Aware Object Detection")
|
| 11 |
|
|
|
|
| 15 |
image = Image.open(uploaded_file)
|
| 16 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 17 |
|
| 18 |
+
# Preprocess the image and make predictions
|
| 19 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 20 |
outputs = model(**inputs)
|
| 21 |
+
|
| 22 |
+
# Extract and display bounding boxes and labels
|
| 23 |
+
logits = outputs.logits.softmax(-1)
|
| 24 |
+
boxes = outputs.pred_boxes
|
| 25 |
+
|
| 26 |
+
# Define a confidence threshold
|
| 27 |
+
threshold = 0.9
|
| 28 |
+
for logit, box in zip(logits[0], boxes[0]):
|
| 29 |
+
score, label = logit.max(0)
|
| 30 |
+
if score > threshold:
|
| 31 |
+
st.write(f"Detected object with confidence {score:.2f}")
|
| 32 |
+
|
| 33 |
+
st.write("Detection complete!")
|