Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,50 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import streamlit as st
|
3 |
-
from tensorflow.keras.preprocessing import image
|
4 |
-
from tensorflow.keras.models import load_model
|
5 |
-
from tensorflow.keras.applications.resnet50 import preprocess_input
|
6 |
-
import matplotlib.pyplot as plt
|
7 |
-
|
8 |
-
# Load the trained model
|
9 |
-
model_path = '
|
10 |
-
model = load_model(model_path)
|
11 |
-
|
12 |
-
# Preprocess the image
|
13 |
-
def preprocess_image(img):
|
14 |
-
img_array = image.img_to_array(img)
|
15 |
-
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
16 |
-
img_array = preprocess_input(img_array) # Ensure correct preprocessing for ResNet50
|
17 |
-
return img_array
|
18 |
-
|
19 |
-
# Make predictions and map to class labels
|
20 |
-
def classify_image(img):
|
21 |
-
img_array = preprocess_image(img)
|
22 |
-
predictions = model.predict(img_array)
|
23 |
-
predicted_class = np.argmax(predictions, axis=1) # Get the index of the highest probability
|
24 |
-
|
25 |
-
class_labels = {0: 'Aedes Aegypti', 1: 'Anopheles Stephensi', 2: 'Culex Quinquefasciatus'}
|
26 |
-
species = class_labels.get(predicted_class[0], "Unknown")
|
27 |
-
|
28 |
-
return species, predictions
|
29 |
-
|
30 |
-
# Streamlit application
|
31 |
-
def main():
|
32 |
-
st.title("Mosquito Species Classification")
|
33 |
-
st.write("Upload a mosquito image to classify its species.")
|
34 |
-
|
35 |
-
# File uploader for image input
|
36 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
37 |
-
|
38 |
-
if uploaded_file is not None:
|
39 |
-
# Load the image for display
|
40 |
-
img = image.load_img(uploaded_file, target_size=(224, 224))
|
41 |
-
st.image(img, caption='Uploaded Image', use_column_width=True)
|
42 |
-
|
43 |
-
# Classify the image
|
44 |
-
result, probabilities = classify_image(img)
|
45 |
-
st.write(f'Predicted mosquito species: **{result}**')
|
46 |
-
st.write(f'Prediction probabilities: {probabilities}')
|
47 |
-
|
48 |
-
# Run the app
|
49 |
-
if __name__ == "__main__":
|
50 |
-
main()
|
|
|
1 |
+
import numpy as np
|
2 |
+
import streamlit as st
|
3 |
+
from tensorflow.keras.preprocessing import image
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
|
8 |
+
# Load the trained model
|
9 |
+
model_path = 'my_cnn.h5' # or '/content/my_model.keras'
|
10 |
+
model = load_model(model_path)
|
11 |
+
|
12 |
+
# Preprocess the image
|
13 |
+
def preprocess_image(img):
|
14 |
+
img_array = image.img_to_array(img)
|
15 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
16 |
+
img_array = preprocess_input(img_array) # Ensure correct preprocessing for ResNet50
|
17 |
+
return img_array
|
18 |
+
|
19 |
+
# Make predictions and map to class labels
|
20 |
+
def classify_image(img):
|
21 |
+
img_array = preprocess_image(img)
|
22 |
+
predictions = model.predict(img_array)
|
23 |
+
predicted_class = np.argmax(predictions, axis=1) # Get the index of the highest probability
|
24 |
+
|
25 |
+
class_labels = {0: 'Aedes Aegypti', 1: 'Anopheles Stephensi', 2: 'Culex Quinquefasciatus'}
|
26 |
+
species = class_labels.get(predicted_class[0], "Unknown")
|
27 |
+
|
28 |
+
return species, predictions
|
29 |
+
|
30 |
+
# Streamlit application
|
31 |
+
def main():
|
32 |
+
st.title("Mosquito Species Classification")
|
33 |
+
st.write("Upload a mosquito image to classify its species.")
|
34 |
+
|
35 |
+
# File uploader for image input
|
36 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
37 |
+
|
38 |
+
if uploaded_file is not None:
|
39 |
+
# Load the image for display
|
40 |
+
img = image.load_img(uploaded_file, target_size=(224, 224))
|
41 |
+
st.image(img, caption='Uploaded Image', use_column_width=True)
|
42 |
+
|
43 |
+
# Classify the image
|
44 |
+
result, probabilities = classify_image(img)
|
45 |
+
st.write(f'Predicted mosquito species: **{result}**')
|
46 |
+
st.write(f'Prediction probabilities: {probabilities}')
|
47 |
+
|
48 |
+
# Run the app
|
49 |
+
if __name__ == "__main__":
|
50 |
+
main()
|