Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,66 @@
|
|
1 |
-
# app.py
|
2 |
import streamlit as st
|
3 |
-
from PIL import Image
|
4 |
import torch
|
5 |
-
|
6 |
-
import
|
7 |
-
from
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
model.eval()
|
12 |
|
13 |
-
#
|
14 |
-
LABELS_URL = 'https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt'
|
15 |
-
labels = requests.get(LABELS_URL).text.split('\n')
|
16 |
-
|
17 |
-
# Streamlit UI
|
18 |
-
st.set_page_config(page_title="π― Object Detector", layout="centered")
|
19 |
st.title("π― AI Object Detection App")
|
20 |
-
st.markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
|
|
23 |
|
24 |
-
|
|
|
|
|
|
|
25 |
image = Image.open(uploaded_file).convert("RGB")
|
26 |
-
st.image(image, caption="
|
27 |
-
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
with st.spinner(
|
33 |
-
outputs = model(
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import torch
|
3 |
+
from torchvision.models.detection import fasterrcnn_resnet50_fpn
|
4 |
+
from torchvision.transforms import functional as F
|
5 |
+
from PIL import Image, ImageDraw
|
6 |
+
import io
|
7 |
|
8 |
+
# Set page config
|
9 |
+
st.set_page_config(page_title="Object Detection App", layout="centered")
|
|
|
10 |
|
11 |
+
# Title and description
|
|
|
|
|
|
|
|
|
|
|
12 |
st.title("π― AI Object Detection App")
|
13 |
+
st.markdown("Upload an image, and let AI detect objects with bounding boxes using a pretrained Faster R-CNN model.")
|
14 |
+
|
15 |
+
# Load model
|
16 |
+
@st.cache_resource
|
17 |
+
def load_model():
|
18 |
+
model = fasterrcnn_resnet50_fpn(pretrained=True)
|
19 |
+
model.eval()
|
20 |
+
return model
|
21 |
+
|
22 |
+
model = load_model()
|
23 |
|
24 |
+
# Upload image
|
25 |
+
uploaded_file = st.file_uploader("π· Upload Image", type=["jpg", "jpeg", "png"])
|
26 |
|
27 |
+
# Confidence threshold slider
|
28 |
+
conf_thresh = st.slider("π Confidence Threshold", min_value=0.1, max_value=1.0, value=0.5, step=0.05)
|
29 |
+
|
30 |
+
if uploaded_file is not None:
|
31 |
image = Image.open(uploaded_file).convert("RGB")
|
32 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
33 |
+
|
34 |
+
# Convert image to tensor
|
35 |
+
image_tensor = F.to_tensor(image).unsqueeze(0)
|
36 |
+
|
37 |
+
# Run detection
|
38 |
+
with st.spinner("Detecting objects..."):
|
39 |
+
outputs = model(image_tensor)
|
40 |
+
boxes = outputs[0]["boxes"]
|
41 |
+
labels = outputs[0]["labels"]
|
42 |
+
scores = outputs[0]["scores"]
|
43 |
+
|
44 |
+
# Filter boxes by confidence threshold
|
45 |
+
selected_indices = [i for i, score in enumerate(scores) if score >= conf_thresh]
|
46 |
+
draw = ImageDraw.Draw(image)
|
47 |
+
|
48 |
+
for i in selected_indices:
|
49 |
+
box = boxes[i].tolist()
|
50 |
+
label = labels[i].item()
|
51 |
+
score = scores[i].item()
|
52 |
+
draw.rectangle(box, outline="red", width=3)
|
53 |
+
draw.text((box[0], box[1]), f"{label}:{score:.2f}", fill="white")
|
54 |
+
|
55 |
+
st.image(image, caption="π§ Detected Image", use_column_width=True)
|
56 |
+
|
57 |
+
# Download button
|
58 |
+
buf = io.BytesIO()
|
59 |
+
image.save(buf, format="PNG")
|
60 |
+
byte_im = buf.getvalue()
|
61 |
+
st.download_button(
|
62 |
+
label="π₯ Download Detected Image",
|
63 |
+
data=byte_im,
|
64 |
+
file_name="detected.png",
|
65 |
+
mime="image/png"
|
66 |
+
)
|