SRUTHI123 commited on
Commit
9b48943
·
verified ·
1 Parent(s): ea7249f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -80
app.py CHANGED
@@ -1,80 +1,80 @@
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
-
10
- model = YOLO('C:\\Users\\SRUTHI\\Desktop\\python-app\\yolo11l.pt')
11
-
12
- # Adjust the path to your YOLOv11 model
13
- # Function to detect actions in images
14
- def detect_action(image_path):
15
- results = model.predict(source=image_path, conf=0.25, save=False)
16
- result = results[0]
17
- detections = [
18
- (model.names[int(box.cls[0])], float(box.conf[0])) for box in result.boxes
19
- ]
20
-
21
- # Classify action based on detections
22
- action_scores = classify_action(detections)
23
-
24
- return result.plot(), action_scores
25
-
26
- def classify_action(detections):
27
- detected_objects = [d[0] for d in detections]
28
-
29
- action_scores = {
30
- 'Stealing': 0.0,
31
- 'Sneaking': 0.0,
32
- 'Peaking': 0.0,
33
- 'Normal': 0.0
34
- }
35
-
36
- if 'person' in detected_objects:
37
- if any(obj in detected_objects for obj in ['backpack', 'handbag', 'suitcase']):
38
- action_scores['Stealing'] += 0.4
39
- if 'refrigerator' in detected_objects:
40
- action_scores['Stealing'] += 0.3
41
- if [conf for obj, conf in detections if obj == 'person'][0] < 0.6:
42
- action_scores['Sneaking'] += 0.5
43
- if len(detected_objects) <= 2:
44
- action_scores['Peaking'] += 0.5
45
-
46
- if not any(score > 0.3 for score in action_scores.values()):
47
- action_scores['Normal'] = 0.4
48
-
49
- return action_scores
50
-
51
- # Streamlit UI
52
- st.title('Suspicious Activity Detection')
53
- st.write('Upload an image to detect suspicious activities.')
54
-
55
- # File uploader
56
- uploaded_file = st.file_uploader("Choose an image...", type="jpg")
57
- if uploaded_file is not None:
58
- # Read the image
59
- image = Image.open(uploaded_file)
60
- st.image(image, caption='Uploaded Image', use_column_width=True)
61
-
62
- # Save the uploaded file for processing
63
- img_path = "/tmp/uploaded_image.jpg"
64
- image.save(img_path)
65
-
66
- # Predict and display results
67
- st.write("Detecting action...")
68
- detected_image, action_scores = detect_action(img_path)
69
-
70
- st.image(detected_image, caption='Detected Image', use_column_width=True)
71
-
72
- # Display action scores
73
- st.write("Action Probability Scores:")
74
- for action, score in action_scores.items():
75
- st.write(f"{action}: {score:.2%}")
76
-
77
- # Predict and display the most likely action
78
- predicted_action = max(action_scores.items(), key=lambda x: x[1])
79
- st.write(f"Predicted Action: {predicted_action[0]} ({predicted_action[1]:.2%} confidence)")
80
-
 
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
+
10
+ model = YOLO(https://colab.research.google.com/drive/1IAyIjPN1J_9s5MhJ0uCsccxfcA0WfXl2?authuser=1#scrollTo=bA29p0kfmhlP)
11
+
12
+ # Adjust the path to your YOLOv11 model
13
+ # Function to detect actions in images
14
+ def detect_action(image_path):
15
+ results = model.predict(source=image_path, conf=0.25, save=False)
16
+ result = results[0]
17
+ detections = [
18
+ (model.names[int(box.cls[0])], float(box.conf[0])) for box in result.boxes
19
+ ]
20
+
21
+ # Classify action based on detections
22
+ action_scores = classify_action(detections)
23
+
24
+ return result.plot(), action_scores
25
+
26
+ def classify_action(detections):
27
+ detected_objects = [d[0] for d in detections]
28
+
29
+ action_scores = {
30
+ 'Stealing': 0.0,
31
+ 'Sneaking': 0.0,
32
+ 'Peaking': 0.0,
33
+ 'Normal': 0.0
34
+ }
35
+
36
+ if 'person' in detected_objects:
37
+ if any(obj in detected_objects for obj in ['backpack', 'handbag', 'suitcase']):
38
+ action_scores['Stealing'] += 0.4
39
+ if 'refrigerator' in detected_objects:
40
+ action_scores['Stealing'] += 0.3
41
+ if [conf for obj, conf in detections if obj == 'person'][0] < 0.6:
42
+ action_scores['Sneaking'] += 0.5
43
+ if len(detected_objects) <= 2:
44
+ action_scores['Peaking'] += 0.5
45
+
46
+ if not any(score > 0.3 for score in action_scores.values()):
47
+ action_scores['Normal'] = 0.4
48
+
49
+ return action_scores
50
+
51
+ # Streamlit UI
52
+ st.title('Suspicious Activity Detection')
53
+ st.write('Upload an image to detect suspicious activities.')
54
+
55
+ # File uploader
56
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
57
+ if uploaded_file is not None:
58
+ # Read the image
59
+ image = Image.open(uploaded_file)
60
+ st.image(image, caption='Uploaded Image', use_column_width=True)
61
+
62
+ # Save the uploaded file for processing
63
+ img_path = "/tmp/uploaded_image.jpg"
64
+ image.save(img_path)
65
+
66
+ # Predict and display results
67
+ st.write("Detecting action...")
68
+ detected_image, action_scores = detect_action(img_path)
69
+
70
+ st.image(detected_image, caption='Detected Image', use_column_width=True)
71
+
72
+ # Display action scores
73
+ st.write("Action Probability Scores:")
74
+ for action, score in action_scores.items():
75
+ st.write(f"{action}: {score:.2%}")
76
+
77
+ # Predict and display the most likely action
78
+ predicted_action = max(action_scores.items(), key=lambda x: x[1])
79
+ st.write(f"Predicted Action: {predicted_action[0]} ({predicted_action[1]:.2%} confidence)")
80
+