ajoy0071998 commited on
Commit
f8e6541
·
verified ·
1 Parent(s): c7bb3af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -111
app.py CHANGED
@@ -1,111 +1,111 @@
1
- import streamlit as st
2
- import numpy as np
3
- import torch
4
- import cv2
5
- from tensorflow.keras.models import load_model
6
- from tensorflow.keras.preprocessing import image as keras_image
7
- from PIL import Image
8
-
9
- # Load the pre-trained MobileNetV2 model from the .h5 file
10
- @st.cache_resource
11
- def load_mobilenet_model():
12
- return load_model('mobilenetv2_classifier.h5') # Replace with your .h5 file path
13
-
14
- mobilenet_model = load_mobilenet_model()
15
-
16
- # Load the YOLOv5 model (pre-trained)
17
- @st.cache_resource
18
- def load_yolov5_model():
19
- return torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
20
-
21
- yolov5_model = load_yolov5_model()
22
-
23
- # Function to preprocess and predict image using MobileNetV2 with class messages
24
- def predict_image(model, img, target_size=(224, 224)):
25
- st.image(img, caption="Uploaded Image", use_column_width=True)
26
- img = img.resize(target_size)
27
- img_array = keras_image.img_to_array(img)
28
- img_array = np.expand_dims(img_array, axis=0)
29
- img_array /= 255.0 # Normalize to match the training preprocessing
30
- predictions = model.predict(img_array)[0]
31
-
32
- # Get indices sorted by prediction confidence (in descending order)
33
- sorted_indices = np.argsort(predictions)[::-1]
34
- primary_class_index = sorted_indices[0]
35
- primary_confidence = predictions[primary_class_index]
36
-
37
- # Check if the primary predicted label is 1 and has a confidence below 0.7
38
- if primary_class_index != 1 and primary_confidence < 0.7:
39
- secondary_class_index = sorted_indices[1]
40
- secondary_confidence = predictions[secondary_class_index]
41
- return secondary_class_index, secondary_confidence
42
-
43
- return primary_class_index, primary_confidence
44
-
45
- # Map class indices to specific messages
46
- class_messages = {
47
- 0: "Face is not visible",
48
- 1: "Looking at the system",
49
- 2: "Looking down",
50
- 3: "Looking left or right",
51
- 4: "Loking up"
52
- }
53
-
54
- # Function to count people using YOLOv5
55
- def count_people(img):
56
- img_array = np.array(img)
57
- results = yolov5_model(img_array)
58
- detections = results.pandas().xyxy[0]
59
- person_detections = detections[detections['name'] == 'person']
60
- num_people = len(person_detections)
61
- return num_people, results
62
-
63
- # Function to detect communication devices using YOLOv5
64
- def detect_device(img):
65
- img_array = np.array(img)
66
- results = yolov5_model(img_array)
67
- detections = results.pandas().xyxy[0]
68
- device_detections = detections[detections['name'].str.contains('phone|tablet|computer', case=False, na=False)]
69
- return device_detections, results
70
-
71
- # Streamlit UI setup
72
- st.title("Image Analysis App")
73
-
74
- # Upload image
75
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
76
-
77
- if uploaded_file is not None:
78
- img = Image.open(uploaded_file)
79
-
80
- # Display the uploaded image
81
- st.image(img, caption="Uploaded Image", use_column_width=True)
82
-
83
- # Run classification with MobileNetV2
84
- st.subheader("Image Classification")
85
- predicted_index, confidence = predict_image(mobilenet_model, img)
86
-
87
- if predicted_index in class_messages:
88
- st.write(f"Prediction: {class_messages[predicted_index]}")
89
- st.write(f"Confidence score: {confidence:.2f}")
90
- else:
91
- st.write("Prediction not recognized.")
92
-
93
- # Run person counting with YOLOv5
94
- st.subheader("People Counting")
95
- num_people, people_results = count_people(img)
96
- st.write(f"Number of people detected: {num_people}")
97
- people_results.show() # Show image with bounding boxes
98
-
99
- # Run device detection with YOLOv5
100
- st.subheader("Communication Device Detection")
101
- device_detections, device_results = detect_device(img)
102
-
103
- if not device_detections.empty:
104
- st.write("Communication devices detected:")
105
- st.write(device_detections[['name', 'confidence']])
106
- device_results.show() # Show image with bounding boxes
107
- else:
108
- st.write("No communication devices detected in the image.")
109
- else:
110
- st.write("Please upload an image to analyze.")
111
-
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import torch
4
+ import cv2
5
+ from tensorflow.keras.models import load_model
6
+ from tensorflow.keras.preprocessing import image as keras_image
7
+ from PIL import Image
8
+
9
+ # Load the pre-trained MobileNetV2 model from the .h5 file
10
+ @st.cache_resource
11
+ def load_mobilenet_model():
12
+ return load_model('mobilenetv2_classifier.h5') # Replace with your .h5 file path
13
+
14
+ mobilenet_model = load_mobilenet_model()
15
+
16
+ # Load the YOLOv5 model (pre-trained)
17
+ @st.cache_resource
18
+ def load_yolov5_model():
19
+ return torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
20
+
21
+ yolov5_model = load_yolov5_model()
22
+
23
+ # Function to preprocess and predict image using MobileNetV2 with class messages
24
+ def predict_image(model, img, target_size=(224, 224)):
25
+ st.image(img, caption="Uploaded Image", use_column_width=True)
26
+ img = img.resize(target_size)
27
+ img_array = keras_image.img_to_array(img)
28
+ img_array = np.expand_dims(img_array, axis=0)
29
+ img_array /= 255.0 # Normalize to match the training preprocessing
30
+ predictions = model.predict(img_array)[0]
31
+
32
+ # Get indices sorted by prediction confidence (in descending order)
33
+ sorted_indices = np.argsort(predictions)[::-1]
34
+ primary_class_index = sorted_indices[0]
35
+ primary_confidence = predictions[primary_class_index]
36
+
37
+ # Check if the primary predicted label is 1 and has a confidence below 0.7
38
+ if primary_class_index != 1 and primary_confidence < 0.7:
39
+ secondary_class_index = sorted_indices[1]
40
+ secondary_confidence = predictions[secondary_class_index]
41
+ return secondary_class_index, secondary_confidence
42
+
43
+ return primary_class_index, primary_confidence
44
+
45
+ # Map class indices to specific messages
46
+ class_messages = {
47
+ 0: "Face is not visible",
48
+ 1: "Looking at the system",
49
+ 2: "Looking down",
50
+ 3: "Looking left or right",
51
+ 4: "Looking up"
52
+ }
53
+
54
+ # Function to count people using YOLOv5
55
+ def count_people(img):
56
+ img_array = np.array(img)
57
+ results = yolov5_model(img_array)
58
+ detections = results.pandas().xyxy[0]
59
+ person_detections = detections[detections['name'] == 'person']
60
+ num_people = len(person_detections)
61
+ return num_people, results
62
+
63
+ # Function to detect communication devices using YOLOv5
64
+ def detect_device(img):
65
+ img_array = np.array(img)
66
+ results = yolov5_model(img_array)
67
+ detections = results.pandas().xyxy[0]
68
+ device_detections = detections[detections['name'].str.contains('phone|tablet|computer', case=False, na=False)]
69
+ return device_detections, results
70
+
71
+ # Streamlit UI setup
72
+ st.title("Image Analysis App")
73
+
74
+ # Upload image
75
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
76
+
77
+ if uploaded_file is not None:
78
+ img = Image.open(uploaded_file)
79
+
80
+ # Display the uploaded image
81
+ st.image(img, caption="Uploaded Image", use_column_width=True)
82
+
83
+ # Run classification with MobileNetV2
84
+ st.subheader("Image Classification")
85
+ predicted_index, confidence = predict_image(mobilenet_model, img)
86
+
87
+ if predicted_index in class_messages:
88
+ st.write(f"Prediction: {class_messages[predicted_index]}")
89
+ st.write(f"Confidence score: {confidence:.2f}")
90
+ else:
91
+ st.write("Prediction not recognized.")
92
+
93
+ # Run person counting with YOLOv5
94
+ st.subheader("People Counting")
95
+ num_people, people_results = count_people(img)
96
+ st.write(f"Number of people detected: {num_people}")
97
+ people_results.show() # Show image with bounding boxes
98
+
99
+ # Run device detection with YOLOv5
100
+ st.subheader("Communication Device Detection")
101
+ device_detections, device_results = detect_device(img)
102
+
103
+ if not device_detections.empty:
104
+ st.write("Communication devices detected:")
105
+ st.write(device_detections[['name', 'confidence']])
106
+ device_results.show() # Show image with bounding boxes
107
+ else:
108
+ st.write("No communication devices detected in the image.")
109
+ else:
110
+ st.write("Please upload an image to analyze.")
111
+