SRUTHI123 commited on
Commit
2702a98
·
verified ·
1 Parent(s): 57dfbfe

Upload 3 files

Browse files
Files changed (1) hide show
  1. app.py +70 -27
app.py CHANGED
@@ -1,35 +1,78 @@
1
  import streamlit as st
2
- from ultralytics import YOLO
3
- import numpy as np
4
- import cv2
5
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- st.title("🔍 Suspicious Activity Detection with YOLOv11")
 
 
 
 
 
 
 
 
8
 
9
- # Load the model
10
- @st.cache_resource
11
- def load_model():
12
- return YOLO("yolo11l.pt")
 
 
 
 
 
13
 
14
- model = load_model()
 
15
 
16
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
17
 
18
- if uploaded_file:
 
 
 
 
 
 
 
19
  image = Image.open(uploaded_file)
20
- st.image(image, caption="Uploaded Image", use_column_width=True)
21
-
22
- if st.button("Detect Activity"):
23
- img_array = np.array(image.convert("RGB"))[..., ::-1] # Convert to BGR
24
- results = model.predict(img_array)
25
-
26
- for r in results:
27
- plotted = r.plot()
28
- st.image(plotted, caption="Detections", use_column_width=True)
29
-
30
- st.subheader("Detected Objects:")
31
- for box in r.boxes:
32
- conf = float(box.conf[0])
33
- cls = int(box.cls[0])
34
- cls_name = model.names[cls]
35
- st.write(f"- {cls_name} (Confidence: {conf:.2f})")
 
 
 
 
 
 
1
  import streamlit as st
 
 
 
2
  from PIL import Image
3
+ import cv2
4
+ import numpy as np
5
+ from ultralytics import YOLO
6
+ import os
7
+
8
+ # Model Initialization
9
+ model = YOLO('/content/yolo11l.pt') # Adjust the path to your YOLOv11 model
10
+
11
+ # Function to detect actions in images
12
+ def detect_action(image_path):
13
+ results = model.predict(source=image_path, conf=0.25, save=False)
14
+ result = results[0]
15
+ detections = [
16
+ (model.names[int(box.cls[0])], float(box.conf[0])) for box in result.boxes
17
+ ]
18
+
19
+ # Classify action based on detections
20
+ action_scores = classify_action(detections)
21
+
22
+ return result.plot(), action_scores
23
 
24
+ def classify_action(detections):
25
+ detected_objects = [d[0] for d in detections]
26
+
27
+ action_scores = {
28
+ 'Stealing': 0.0,
29
+ 'Sneaking': 0.0,
30
+ 'Peaking': 0.0,
31
+ 'Normal': 0.0
32
+ }
33
 
34
+ if 'person' in detected_objects:
35
+ if any(obj in detected_objects for obj in ['backpack', 'handbag', 'suitcase']):
36
+ action_scores['Stealing'] += 0.4
37
+ if 'refrigerator' in detected_objects:
38
+ action_scores['Stealing'] += 0.3
39
+ if [conf for obj, conf in detections if obj == 'person'][0] < 0.6:
40
+ action_scores['Sneaking'] += 0.5
41
+ if len(detected_objects) <= 2:
42
+ action_scores['Peaking'] += 0.5
43
 
44
+ if not any(score > 0.3 for score in action_scores.values()):
45
+ action_scores['Normal'] = 0.4
46
 
47
+ return action_scores
48
 
49
+ # Streamlit UI
50
+ st.title('Suspicious Activity Detection')
51
+ st.write('Upload an image to detect suspicious activities.')
52
+
53
+ # File uploader
54
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
55
+ if uploaded_file is not None:
56
+ # Read the image
57
  image = Image.open(uploaded_file)
58
+ st.image(image, caption='Uploaded Image', use_column_width=True)
59
+
60
+ # Save the uploaded file for processing
61
+ img_path = "/tmp/uploaded_image.jpg"
62
+ image.save(img_path)
63
+
64
+ # Predict and display results
65
+ st.write("Detecting action...")
66
+ detected_image, action_scores = detect_action(img_path)
67
+
68
+ st.image(detected_image, caption='Detected Image', use_column_width=True)
69
+
70
+ # Display action scores
71
+ st.write("Action Probability Scores:")
72
+ for action, score in action_scores.items():
73
+ st.write(f"{action}: {score:.2%}")
74
+
75
+ # Predict and display the most likely action
76
+ predicted_action = max(action_scores.items(), key=lambda x: x[1])
77
+ st.write(f"Predicted Action: {predicted_action[0]} ({predicted_action[1]:.2%} confidence)")
78
+