Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,71 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
# Load YOLOv11 model
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
-
return
|
| 12 |
|
| 13 |
model = load_model()
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 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 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
st.
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
st.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
# Load YOLOv11 model
|
| 10 |
@st.cache_resource
|
| 11 |
def load_model():
|
| 12 |
+
return torch.hub.load('ultralytics/yolov5', 'custom', path='yolo11l.pt', force_reload=True)
|
| 13 |
|
| 14 |
model = load_model()
|
| 15 |
+
model.conf = 0.25 # confidence threshold
|
| 16 |
|
| 17 |
+
# Activity labels
|
| 18 |
+
labels = ['Normal', 'Peaking', 'Sneaking', 'Stealing']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Streamlit UI
|
| 21 |
+
st.set_page_config(page_title="Suspicious Activity Detection", layout="centered")
|
| 22 |
+
st.title("🚨 Suspicious Activity Detection with YOLOv11")
|
| 23 |
+
st.markdown("Detect **Normal**, **Peaking**, **Sneaking**, and **Stealing** behaviors in real-time from uploaded images or videos.")
|
| 24 |
+
|
| 25 |
+
# Upload section
|
| 26 |
+
file = st.file_uploader("Upload an image or a video", type=['jpg', 'jpeg', 'png', 'mp4'])
|
| 27 |
+
|
| 28 |
+
# Process predictions
|
| 29 |
+
def display_predictions(results):
|
| 30 |
+
df = results.pandas().xyxy[0]
|
| 31 |
+
activity_counts = {label: 0 for label in labels}
|
| 32 |
+
for _, row in df.iterrows():
|
| 33 |
+
label = row['name']
|
| 34 |
+
if label in activity_counts:
|
| 35 |
+
activity_counts[label] += 1
|
| 36 |
+
return activity_counts
|
| 37 |
+
|
| 38 |
+
# Handle image
|
| 39 |
+
if file is not None:
|
| 40 |
+
if file.type.startswith("image"):
|
| 41 |
+
image = Image.open(file).convert('RGB')
|
| 42 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 43 |
+
results = model(image, size=640)
|
| 44 |
+
results.render()
|
| 45 |
+
st.image(Image.fromarray(results.ims[0]), caption="Detection Result", use_column_width=True)
|
| 46 |
+
counts = display_predictions(results)
|
| 47 |
+
st.markdown("### 🔍 Detected Activities")
|
| 48 |
+
for act, cnt in counts.items():
|
| 49 |
+
if cnt > 0:
|
| 50 |
+
st.success(f"**{act}**: {cnt}")
|
| 51 |
+
|
| 52 |
+
# Handle video
|
| 53 |
+
elif file.type.startswith("video"):
|
| 54 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 55 |
+
tfile.write(file.read())
|
| 56 |
+
video_path = tfile.name
|
| 57 |
+
cap = cv2.VideoCapture(video_path)
|
| 58 |
+
stframe = st.empty()
|
| 59 |
+
st.markdown("### 📹 Processing video...")
|
| 60 |
+
|
| 61 |
+
while cap.isOpened():
|
| 62 |
+
ret, frame = cap.read()
|
| 63 |
+
if not ret:
|
| 64 |
+
break
|
| 65 |
+
results = model(frame)
|
| 66 |
+
results.render()
|
| 67 |
+
output_frame = results.ims[0]
|
| 68 |
+
stframe.image(output_frame, channels="BGR", use_column_width=True)
|
| 69 |
+
|
| 70 |
+
cap.release()
|
| 71 |
+
os.remove(video_path)
|