SRUTHI123 commited on
Commit
848d7db
·
verified ·
1 Parent(s): ebdc3f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -32
app.py CHANGED
@@ -5,47 +5,42 @@ from ultralytics import YOLO
5
 
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.pt")
12
 
13
  model = load_model()
14
 
15
- # Improved Action Classification Logic
16
- def classify_action(detections, boxes):
 
 
 
 
17
  action_scores = {'Stealing': 0, 'Sneaking': 0, 'Peaking': 0, 'Normal': 0}
18
  objects = [d[0] for d in detections]
19
  confidences = [d[1] for d in detections]
20
 
21
  has_person = 'person' in objects
22
  has_bag = any(obj in objects for obj in ['backpack', 'handbag'])
23
- low_conf = max(confidences) < 0.5 if confidences else True
24
  few_objects = len(set(objects)) <= 2
25
- mostly_person = objects.count('person') > len(objects) * 0.6
26
-
27
- # Analyze bounding box heights (sneaking = crouched or hidden)
28
- person_heights = [
29
- y2 - y1 for (cls, conf), (x1, y1, x2, y2) in zip(detections, boxes)
30
- if cls == 'person'
31
- ]
32
- avg_height = np.mean(person_heights) if person_heights else 0
33
- sneaking_posture = avg_height < 0.4 # Normalized threshold (assuming 0–1 scale)
34
 
35
  if has_person:
36
  if has_bag:
37
- action_scores['Stealing'] += 0.6
38
- if 'refrigerator' in objects or 'microwave' in objects:
39
- action_scores['Stealing'] += 0.4
40
- if low_conf and sneaking_posture:
41
  action_scores['Sneaking'] += 0.9
42
- elif few_objects and mostly_person:
43
- action_scores['Peaking'] += 0.7
44
  else:
45
  action_scores['Normal'] += 0.8
46
  else:
47
  action_scores['Normal'] += 1.0
48
 
 
49
  total = sum(action_scores.values())
50
  if total > 0:
51
  for k in action_scores:
@@ -53,25 +48,26 @@ def classify_action(detections, boxes):
53
 
54
  return action_scores
55
 
56
- # Detection Function
57
  def detect_action(image_path):
58
  results = model.predict(source=image_path, conf=0.4, iou=0.5, save=False, verbose=False)
59
  result = results[0]
 
60
  detections = [
61
  (model.names[int(cls)], float(conf))
62
  for cls, conf in zip(result.boxes.cls, result.boxes.conf)
63
  ]
64
- boxes = result.boxes.xyxy.cpu().numpy() / result.orig_shape[0] # Normalize height
65
 
66
  annotated_image = result.plot()
67
- action_scores = classify_action(detections, boxes)
 
68
  return annotated_image, action_scores
69
 
70
- # Streamlit UI
71
  st.title("🛡️ Suspicious Activity Detection")
72
- st.markdown("Upload an image to detect: **Stealing**, **Sneaking**, **Peaking**, or **Normal** behavior.")
73
 
74
- uploaded_file = st.file_uploader("📤 Upload Image", type=["jpg", "jpeg", "png"])
75
 
76
  if uploaded_file:
77
  image = Image.open(uploaded_file).convert("RGB")
@@ -80,13 +76,14 @@ if uploaded_file:
80
  temp_path = "/tmp/uploaded.jpg"
81
  image.save(temp_path)
82
 
83
- with st.spinner("🔍 Analyzing..."):
84
  detected_image, action_scores = detect_action(temp_path)
85
 
86
- st.image(detected_image, caption="Detection Result", use_column_width=True)
87
- st.subheader("📊 Action Scores")
 
88
  for action, score in action_scores.items():
89
  st.write(f"**{action}**: {score:.2%}")
90
-
91
- final_action = max(action_scores.items(), key=lambda x: x[1])
92
- st.success(f"🎯 **Predicted Action:** {final_action[0]} ({final_action[1]:.2%})")
 
5
 
6
  st.set_page_config(page_title="Suspicious Activity Detection", layout="centered")
7
 
8
+ # Load the YOLOv11 model
9
  @st.cache_resource
10
  def load_model():
11
+ return YOLO("yolo11l.pt") # Ensure your model file is present in the root directory
12
 
13
  model = load_model()
14
 
15
+ # ------------------ Intelligent Action Classification Logic ------------------
16
+ def classify_action(detections):
17
+ """
18
+ Classifies the activity based on detected object types and confidence scores.
19
+ Adjusted to better separate 'Sneaking' from 'Peaking'.
20
+ """
21
  action_scores = {'Stealing': 0, 'Sneaking': 0, 'Peaking': 0, 'Normal': 0}
22
  objects = [d[0] for d in detections]
23
  confidences = [d[1] for d in detections]
24
 
25
  has_person = 'person' in objects
26
  has_bag = any(obj in objects for obj in ['backpack', 'handbag'])
27
+ low_conf = max(confidences) < 0.6 if confidences else True
28
  few_objects = len(set(objects)) <= 2
29
+ mostly_person = objects.count('person') >= len(objects) * 0.6
 
 
 
 
 
 
 
 
30
 
31
  if has_person:
32
  if has_bag:
33
+ action_scores['Stealing'] += 0.7
34
+ if low_conf and few_objects:
 
 
35
  action_scores['Sneaking'] += 0.9
36
+ elif mostly_person and few_objects:
37
+ action_scores['Peaking'] += 0.6
38
  else:
39
  action_scores['Normal'] += 0.8
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:
 
48
 
49
  return action_scores
50
 
51
+ # ------------------ Detection Function ------------------
52
  def detect_action(image_path):
53
  results = model.predict(source=image_path, conf=0.4, 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")
 
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)")