-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
189 lines (167 loc) · 6.43 KB
/
app.py
File metadata and controls
189 lines (167 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
import numpy as np
from PIL import Image
import cv2
# Load the trained model
model = load_model("waste_classification_model.h5")
# Define the class labels
classes = {0: "Organic Waste (O)", 1: "Inorganic Waste (R)"}
# Add custom CSS to enhance UI design with smaller font sizes
# Update the background gradient style
import streamlit as st
st.markdown(
"""
<style>
body {
background: rgba(255, 255, 255, 0.8);
font-family: Arial, sans-serif;
color: #fff; /* Text color to contrast with the background */
}
.main {
background: linear-gradient(to right, #764BA2, #667EEA); /* Gradient applied directly */
border-radius: 15px;
padding: 20px;
margin: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.stButton button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
cursor: pointer;
font-size: 14px; /* Reduced font size */
}
.stButton button:hover {
background-color: #45a049;
}
.header {
font-family: 'Arial Black', sans-serif;
font-size: 35px; /* Reduced font size */
color: #4CAF50;
text-align: center;
}
.subheader {
font-family: 'Arial', sans-serif;
font-size: 22px; /* Reduced font size */
color: #ffffff;
text-align: center;
}
hr {
border: none;
height: 1px;
background: #ddd;
margin: 20px 0;
}
</style>
""",
unsafe_allow_html=True,
)
# Header
st.markdown('<div class="header">🌿 Waste Classification App</div>', unsafe_allow_html=True)
st.markdown('<div class="subheader">Classify waste as Organic or Inorganic</div>', unsafe_allow_html=True)
# Sidebar for mode selection
st.sidebar.title("⚙️ Input Mode")
input_mode = st.sidebar.radio("Choose input mode:", ["Upload Image", "Camera Capture"])
# Preprocess the image
def preprocess_image(image):
image = image.resize((150, 150)) # Resize to the model input size
image_array = img_to_array(image) / 255.0 # Normalize
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
return image_array
# Predict function
def predict(image):
processed_image = preprocess_image(image)
prediction = model.predict(processed_image)
predicted_class = np.argmax(prediction, axis=1)[0]
confidence = np.max(prediction)
return classes[predicted_class], confidence
# Draw bounding box and label on the image
def draw_bounding_box(image, label, confidence):
image_np = np.array(image) # Convert PIL image to numpy array
h, w, _ = image_np.shape
# Draw bounding box
x1, y1, x2, y2 = int(w * 0.1), int(h * 0.1), int(w * 0.9), int(h * 0.9)
cv2.rectangle(image_np, (x1, y1), (x2, y2), (0, 255, 0), 3) # Green box
# Add label
label_text = f"{label} ({confidence:.2f})"
font = cv2.FONT_HERSHEY_SIMPLEX
text_size = cv2.getTextSize(label_text, font, 0.7, 2)[0]
text_x = x1
text_y = y1 - 10
cv2.rectangle(image_np, (x1, y1 - text_size[1] - 10), (x1 + text_size[0], y1), (0, 255, 0), -1)
cv2.putText(image_np, label_text, (text_x, text_y), font, 0.7, (0, 0, 0), 2)
return Image.fromarray(image_np) # Convert back to PIL Image
# Main functionality
if input_mode == "Upload Image":
st.write("📤 **Upload Image**")
uploaded_file = st.file_uploader("Upload an image for classification", type=["jpg", "jpeg", "png"])
if uploaded_file:
# Display uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Predict and show result
if st.button("🔍 Classify Image"):
label, confidence = predict(image)
result_image = draw_bounding_box(image, label, confidence)
st.image(result_image, caption="Result with Bounding Box", use_column_width=True)
elif input_mode == "Camera Capture":
st.write("📸 **Capture Image**")
picture = st.camera_input("Take a picture")
if picture:
# Display captured image
image = Image.open(picture)
st.image(image, caption="Captured Image", use_column_width=True)
# Predict and show result
if st.button("🔍 Classify Image"):
label, confidence = predict(image)
result_image = draw_bounding_box(image, label, confidence)
st.image(result_image, caption="Result with Bounding Box", use_column_width=True)
# Footer
# Footer with personal links and acknowledgment
st.markdown("<hr>", unsafe_allow_html=True)
st.markdown(
"""
<div style="text-align: center; font-family: Arial, sans-serif; font-size: 14px;"> <!-- Reduced font size -->
🌟 Powered by <b style="font-size: 16px;">TensorFlow</b>, <b style="font-size: 16px;">OpenCV</b>, and <b style="font-size: 16px;">Streamlit</b><br><br>
<b style="font-size: 16px;">Made by:</b> Hitesh Kumar<br>
<a href="https://www.linkedin.com/in/hitesh-kumar-aiml/" target="_blank" style="color: #333; font-size: 16px;">LinkedIn</a> |
<a href="https://github.com/Hiteshydv001" target="_blank" style="color: #333; font-size: 16px;">GitHub</a><br><br>
🎉<b style="font-size: 16px;">Edunet-Shell Skills4Future AICTE Internship Project</b> (Nov-Dec)<br>
Focused on <b style="font-size: 16px;">Green Skills</b> & <b style="font-size: 16px;">AI</b>.
</div>
""",
unsafe_allow_html=True,
)
import streamlit as st
import requests
# Fetch the raw content of the README file
url = "https://raw.githubusercontent.com/Hiteshydv001/Waste-classification-model-cnn/main/README.md"
response = requests.get(url)
# Apply custom CSS for padding and styling
st.markdown(
"""
<style>
.markdown-container {
padding-top: 20px;
padding-left: 20px;
padding-right: 20px;
padding-bottom: 20px;
background-color: #2e2e2e; /* Dark background */
color: #ffffff; /* White text for better visibility */
border-radius: 8px;
box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.1);
font-size: 16px; /* Adjust text size */
}
</style>
""",
unsafe_allow_html=True
)
# Display the README content with custom container
st.markdown(
f'<div class="markdown-container">{response.text}</div>',
unsafe_allow_html=True
)