Spaces:
Build error
Build error
haydenbanz
commited on
Commit
•
08d4e3b
1
Parent(s):
6053740
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,16 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
import torch
|
4 |
from PIL import Image
|
5 |
-
|
6 |
|
7 |
-
|
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 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
|
15 |
# Preprocess the image
|
16 |
inputs = processor(images=image, return_tensors="pt")
|
@@ -22,16 +22,48 @@ def predict(inputs):
|
|
22 |
target_sizes = torch.tensor([image.size[::-1]])
|
23 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
|
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
3 |
import torch
|
4 |
from PIL import Image
|
5 |
+
import requests
|
6 |
|
7 |
+
st.set_page_config(page_title="SnapSpot", page_icon="📸", layout="wide", initial_sidebar_state="collapsed")
|
|
|
|
|
8 |
|
9 |
+
# Function to perform object detection
|
10 |
+
def detect_objects(image):
|
11 |
+
# Load DETR model and processor
|
12 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
13 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
14 |
|
15 |
# Preprocess the image
|
16 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
22 |
target_sizes = torch.tensor([image.size[::-1]])
|
23 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
24 |
|
25 |
+
return results
|
26 |
+
|
27 |
+
# Main Streamlit app
|
28 |
+
def main():
|
29 |
+
st.title("SnapSpot")
|
30 |
+
st.markdown(
|
31 |
+
"""
|
32 |
+
<style>
|
33 |
+
.reportview-container {
|
34 |
+
background: #0e1117;
|
35 |
+
color: #f0f6fc;
|
36 |
+
}
|
37 |
+
.st-bq {
|
38 |
+
background-color: #0e1117;
|
39 |
+
}
|
40 |
+
.st-bm {
|
41 |
+
padding-top: 2rem;
|
42 |
+
}
|
43 |
+
</style>
|
44 |
+
""",
|
45 |
+
unsafe_allow_html=True,
|
46 |
+
)
|
47 |
+
|
48 |
+
# Upload image
|
49 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
50 |
+
|
51 |
+
if uploaded_image is not None:
|
52 |
+
# Display uploaded image
|
53 |
+
image = Image.open(uploaded_image)
|
54 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
55 |
|
56 |
+
# Perform object detection
|
57 |
+
results = detect_objects(image)
|
58 |
|
59 |
+
# Display detection results
|
60 |
+
st.subheader("Detection Results:")
|
61 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
62 |
+
box = [round(i, 2) for i in box.tolist()]
|
63 |
+
st.write(
|
64 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
65 |
+
f"{round(score.item(), 3)} at location {box}"
|
66 |
+
)
|
67 |
|
68 |
+
if __name__ == "__main__":
|
69 |
+
main()
|