| | import streamlit as st |
| | from PIL import Image |
| | import numpy as np |
| | from ultralytics import YOLO |
| |
|
| | |
| | st.set_page_config(page_title="Suspicious Activity Detection", layout="centered") |
| |
|
| | |
| | @st.cache_resource |
| | def load_model(): |
| | return YOLO("yolo11l.pt") |
| |
|
| | model = load_model() |
| |
|
| | |
| | def classify_action(detections): |
| | """ |
| | Classify based on detected object types and confidence scores. |
| | """ |
| | action_scores = { |
| | 'Stealing': 0, |
| | 'Sneaking': 0, |
| | 'Peaking': 0, |
| | 'Normal': 0 |
| | } |
| |
|
| | objects = [d[0] for d in detections] |
| | confidences = [d[1] for d in detections] |
| |
|
| | has_person = 'person' in objects |
| | num_objects = len(objects) |
| |
|
| | |
| | if has_person: |
| | if 'handbag' in objects or 'backpack' in objects: |
| | action_scores['Stealing'] += 0.6 |
| | if 'refrigerator' in objects or 'microwave' in objects: |
| | action_scores['Stealing'] += 0.4 |
| | if max(confidences) < 0.55: |
| | action_scores['Sneaking'] += 0.6 |
| | if num_objects <= 2: |
| | action_scores['Peaking'] += 0.5 |
| |
|
| | |
| | if all(score == 0 for score in action_scores.values()): |
| | action_scores['Normal'] = 0.8 |
| |
|
| | |
| | total = sum(action_scores.values()) |
| | if total > 0: |
| | for key in action_scores: |
| | action_scores[key] /= total |
| |
|
| | return action_scores |
| |
|
| | |
| | def detect_action(image_path): |
| | results = model.predict(source=image_path, conf=0.4, iou=0.5, save=False, verbose=False) |
| | result = results[0] |
| |
|
| | detections = [ |
| | (model.names[int(cls)], float(conf)) |
| | for cls, conf in zip(result.boxes.cls, result.boxes.conf) |
| | ] |
| |
|
| | |
| | annotated_image = result.plot() |
| | action_scores = classify_action(detections) |
| |
|
| | return annotated_image, action_scores |
| |
|
| | |
| | st.title("π Suspicious Activity Detection") |
| | st.markdown("Upload an image to detect if someone is **Stealing**, **Sneaking**, **Peaking**, or just acting **Normal**.") |
| |
|
| | uploaded_file = st.file_uploader("π€ Upload an image", type=["jpg", "jpeg", "png"]) |
| |
|
| | if uploaded_file: |
| | image = Image.open(uploaded_file).convert("RGB") |
| | st.image(image, caption="Uploaded Image", use_column_width=True) |
| |
|
| | temp_path = "/tmp/uploaded.jpg" |
| | image.save(temp_path) |
| |
|
| | with st.spinner("π΅οΈ Detecting suspicious activity..."): |
| | detected_image, action_scores = detect_action(temp_path) |
| |
|
| | |
| | st.image(detected_image, caption="π Detected Image", use_column_width=True) |
| |
|
| | st.subheader("π Action Confidence Scores") |
| | for action, score in action_scores.items(): |
| | st.write(f"**{action}**: {score:.2%}") |
| |
|
| | most_likely_action = max(action_scores.items(), key=lambda x: x[1]) |
| | st.success(f"π― **Predicted Action**: {most_likely_action[0]} ({most_likely_action[1]:.2%} confidence)") |