Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,89 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
# Load YOLOv11 model
|
| 10 |
@st.cache_resource
|
| 11 |
def load_model():
|
| 12 |
-
return
|
| 13 |
|
| 14 |
model = load_model()
|
| 15 |
-
model.conf = 0.25 # confidence threshold
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
|
| 5 |
+
# Set up Streamlit page
|
| 6 |
+
st.set_page_config(page_title="Suspicious Activity Detection", layout="centered")
|
| 7 |
|
| 8 |
# Load YOLOv11 model
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
+
return YOLO("yolo11l (1).pt") # Ensure model filename matches
|
| 12 |
|
| 13 |
model = load_model()
|
|
|
|
| 14 |
|
| 15 |
+
# ------------------ Improved Action Classification Logic ------------------
|
| 16 |
+
def classify_action(detections):
|
| 17 |
+
"""
|
| 18 |
+
Classify activity based on object types, count, and confidence.
|
| 19 |
+
"""
|
| 20 |
+
action_scores = {'Stealing': 0, 'Sneaking': 0, 'Peaking': 0, 'Normal': 0}
|
| 21 |
+
objects = [d[0] for d in detections]
|
| 22 |
+
confidences = [d[1] for d in detections]
|
| 23 |
|
| 24 |
+
has_person = 'person' in objects
|
| 25 |
+
has_bag = any(obj in objects for obj in ['handbag', 'backpack'])
|
| 26 |
+
few_objects = len(set(objects)) <= 2
|
| 27 |
+
mostly_person = objects.count('person') >= len(objects) * 0.6 if objects else False
|
| 28 |
+
max_conf = max(confidences) if confidences else 0.0
|
| 29 |
+
|
| 30 |
+
# Decision tree for classification
|
| 31 |
+
if has_person:
|
| 32 |
+
if has_bag and len(objects) >= 3:
|
| 33 |
+
action_scores['Stealing'] += 1.0
|
| 34 |
+
elif max_conf < 0.55 and few_objects:
|
| 35 |
+
action_scores['Sneaking'] += 1.0
|
| 36 |
+
elif mostly_person and few_objects and max_conf >= 0.55:
|
| 37 |
+
action_scores['Peaking'] += 1.0
|
| 38 |
+
else:
|
| 39 |
+
action_scores['Normal'] += 1.0
|
| 40 |
+
else:
|
| 41 |
+
action_scores['Normal'] += 1.0
|
| 42 |
+
|
| 43 |
+
# Normalize scores
|
| 44 |
+
total = sum(action_scores.values())
|
| 45 |
+
if total > 0:
|
| 46 |
+
for k in action_scores:
|
| 47 |
+
action_scores[k] /= total
|
| 48 |
+
|
| 49 |
+
return action_scores
|
| 50 |
+
|
| 51 |
+
# ------------------ Detection Function ------------------
|
| 52 |
+
def detect_action(image_path):
|
| 53 |
+
results = model.predict(source=image_path, conf=0.35, iou=0.5, save=False, verbose=False)
|
| 54 |
+
result = results[0]
|
| 55 |
+
|
| 56 |
+
detections = [
|
| 57 |
+
(model.names[int(cls)], float(conf))
|
| 58 |
+
for cls, conf in zip(result.boxes.cls, result.boxes.conf)
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
annotated_image = result.plot()
|
| 62 |
+
action_scores = classify_action(detections)
|
| 63 |
+
|
| 64 |
+
return annotated_image, action_scores
|
| 65 |
+
|
| 66 |
+
# ------------------ Streamlit UI ------------------
|
| 67 |
+
st.title("🛡️ Suspicious Activity Detection")
|
| 68 |
+
st.markdown("Upload an image to detect if someone is **Stealing**, **Sneaking**, **Peaking**, or acting **Normal**.")
|
| 69 |
+
|
| 70 |
+
uploaded_file = st.file_uploader("📤 Upload an image", type=["jpg", "jpeg", "png"])
|
| 71 |
+
|
| 72 |
+
if uploaded_file:
|
| 73 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 74 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 75 |
+
|
| 76 |
+
temp_path = "/tmp/uploaded.jpg"
|
| 77 |
+
image.save(temp_path)
|
| 78 |
+
|
| 79 |
+
with st.spinner("🔍 Detecting suspicious activity..."):
|
| 80 |
+
detected_image, action_scores = detect_action(temp_path)
|
| 81 |
+
|
| 82 |
+
st.image(detected_image, caption="🔍 Detection Results", use_column_width=True)
|
| 83 |
+
|
| 84 |
+
st.subheader("📊 Action Confidence Scores")
|
| 85 |
+
for action, score in action_scores.items():
|
| 86 |
+
st.write(f"**{action}**: {score:.2%}")
|
| 87 |
+
|
| 88 |
+
top_action = max(action_scores.items(), key=lambda x: x[1])
|
| 89 |
+
st.success(f"🎯 **Predicted Action:** {top_action[0]} ({top_action[1]:.2%} confidence)")
|