pranit144 commited on
Commit
c480bcd
·
verified ·
1 Parent(s): a9d47f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -82
app.py CHANGED
@@ -1,82 +1,83 @@
1
- import streamlit as st
2
- import os
3
- import tensorflow as tf
4
- from tensorflow.keras.models import load_model
5
- import cv2
6
- import numpy as np
7
-
8
- # Load the model manually using Keras
9
- model = load_model('Model/keras_model.h5', compile=False) # Load model without compilation
10
-
11
- # Labels directly added to the code
12
- labels = [
13
- "Nothing",
14
- "Zip-top cans",
15
- "Newspaper",
16
- "Old shoes",
17
- "Watercolor pen",
18
- "Disinfectant",
19
- "Battery",
20
- "Vegetable leaf",
21
- "Apple"
22
- ]
23
-
24
- # Classification dictionary (modified to skip image-based overlays)
25
- classDic = {0: None,
26
- 1: 0,
27
- 2: 0,
28
- 3: 3,
29
- 4: 3,
30
- 5: 1,
31
- 6: 1,
32
- 7: 2,
33
- 8: 2}
34
-
35
- # Streamlit page configuration
36
- st.title('Object Detection with Webcam')
37
- st.write("This application uses your webcam to detect objects in real-time.")
38
-
39
- # Start the webcam feed for Streamlit
40
- cap = cv2.VideoCapture(0) # Manually set to camera index 0 (or change to 1, 2 if necessary)
41
- if not cap.isOpened():
42
- st.error("Error: No camera found!")
43
- exit()
44
-
45
- stframe = st.empty() # Placeholder for the webcam feed
46
-
47
- # Add the 'Exit' button outside the loop so it's only displayed once
48
- exit_button = st.button('Exit', key="exit_button")
49
-
50
- # Loop to keep updating webcam feed
51
- while True:
52
- ret, img = cap.read()
53
- if not ret or img is None:
54
- st.error("Error: Failed to capture image!")
55
- continue
56
-
57
- imgResize = cv2.resize(img, (454, 340)) # Resize captured frame to a specific size
58
-
59
- # Perform prediction using the loaded model
60
- img_input = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
61
- img_input = cv2.resize(img_input, (224, 224)) # Resize to expected input size for your model
62
- img_input = img_input / 255.0 # Normalize the image
63
- prediction = model.predict(tf.convert_to_tensor(img_input[None, ...])) # Make prediction
64
- classID = prediction.argmax() # Get the predicted class ID
65
- print(f"Predicted class ID: {classID}")
66
-
67
- # Handle the class ID (can be used to trigger different actions based on class)
68
- if 0 < classID <= len(labels):
69
- st.write(f"Detected class: {labels[classID - 1]}")
70
- else:
71
- st.write(f"Invalid classID: {classID}, setting to default.")
72
-
73
- # Display the resized image in Streamlit
74
- img_resized = cv2.cvtColor(imgResize, cv2.COLOR_BGR2RGB) # Convert image to RGB
75
- stframe.image(img_resized, channels="RGB", caption="Real-time Detection", use_container_width=True)
76
-
77
- # Exit condition based on button press
78
- if exit_button:
79
- break
80
-
81
- # Release resources and close the window
82
- cap.release()
 
 
1
+ import streamlit as st
2
+ import os
3
+ !pip install tensorflow
4
+ import tensorflow as tf
5
+ from tensorflow.keras.models import load_model
6
+ import cv2
7
+ import numpy as np
8
+
9
+ # Load the model manually using Keras
10
+ model = load_model('Model/keras_model.h5', compile=False) # Load model without compilation
11
+
12
+ # Labels directly added to the code
13
+ labels = [
14
+ "Nothing",
15
+ "Zip-top cans",
16
+ "Newspaper",
17
+ "Old shoes",
18
+ "Watercolor pen",
19
+ "Disinfectant",
20
+ "Battery",
21
+ "Vegetable leaf",
22
+ "Apple"
23
+ ]
24
+
25
+ # Classification dictionary (modified to skip image-based overlays)
26
+ classDic = {0: None,
27
+ 1: 0,
28
+ 2: 0,
29
+ 3: 3,
30
+ 4: 3,
31
+ 5: 1,
32
+ 6: 1,
33
+ 7: 2,
34
+ 8: 2}
35
+
36
+ # Streamlit page configuration
37
+ st.title('Object Detection with Webcam')
38
+ st.write("This application uses your webcam to detect objects in real-time.")
39
+
40
+ # Start the webcam feed for Streamlit
41
+ cap = cv2.VideoCapture(0) # Manually set to camera index 0 (or change to 1, 2 if necessary)
42
+ if not cap.isOpened():
43
+ st.error("Error: No camera found!")
44
+ exit()
45
+
46
+ stframe = st.empty() # Placeholder for the webcam feed
47
+
48
+ # Add the 'Exit' button outside the loop so it's only displayed once
49
+ exit_button = st.button('Exit', key="exit_button")
50
+
51
+ # Loop to keep updating webcam feed
52
+ while True:
53
+ ret, img = cap.read()
54
+ if not ret or img is None:
55
+ st.error("Error: Failed to capture image!")
56
+ continue
57
+
58
+ imgResize = cv2.resize(img, (454, 340)) # Resize captured frame to a specific size
59
+
60
+ # Perform prediction using the loaded model
61
+ img_input = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
62
+ img_input = cv2.resize(img_input, (224, 224)) # Resize to expected input size for your model
63
+ img_input = img_input / 255.0 # Normalize the image
64
+ prediction = model.predict(tf.convert_to_tensor(img_input[None, ...])) # Make prediction
65
+ classID = prediction.argmax() # Get the predicted class ID
66
+ print(f"Predicted class ID: {classID}")
67
+
68
+ # Handle the class ID (can be used to trigger different actions based on class)
69
+ if 0 < classID <= len(labels):
70
+ st.write(f"Detected class: {labels[classID - 1]}")
71
+ else:
72
+ st.write(f"Invalid classID: {classID}, setting to default.")
73
+
74
+ # Display the resized image in Streamlit
75
+ img_resized = cv2.cvtColor(imgResize, cv2.COLOR_BGR2RGB) # Convert image to RGB
76
+ stframe.image(img_resized, channels="RGB", caption="Real-time Detection", use_container_width=True)
77
+
78
+ # Exit condition based on button press
79
+ if exit_button:
80
+ break
81
+
82
+ # Release resources and close the window
83
+ cap.release()