Spaces:
Sleeping
Sleeping
import numpy as np | |
import streamlit as st | |
from tensorflow.keras.preprocessing import image | |
from tensorflow.keras.models import load_model | |
from tensorflow.keras.applications.resnet50 import preprocess_input | |
# Load the trained model | |
model_path = 'my_cnn.h5' # or '/content/my_model.keras' | |
model = load_model(model_path) | |
# Preprocess the image | |
def preprocess_image(img): | |
img_array = image.img_to_array(img) | |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension | |
img_array = preprocess_input(img_array) # Ensure correct preprocessing for ResNet50 | |
return img_array | |
# Make predictions and map to class labels | |
def classify_image(img): | |
img_array = preprocess_image(img) | |
predictions = model.predict(img_array) | |
predicted_class = np.argmax(predictions, axis=1) # Get the index of the highest probability | |
class_labels = {0: 'Aedes Aegypti', 1: 'Anopheles Stephensi', 2: 'Culex Quinquefasciatus'} | |
species = class_labels.get(predicted_class[0], "Unknown") | |
return species, predictions | |
# Streamlit application | |
def main(): | |
st.title("Mosquito Species Classification") | |
st.write("Upload a mosquito image or select an example image to classify its species.") | |
# Example images | |
example_images = { | |
"Aedes Aegypti": "Aedes_aegypti_1_0_832.jpg", | |
"Anopheles Stephensi": "Anopheles_stephensi_1_0_364.jpg", | |
"Culex Quinquefasciatus": "Culex_quinquefasciatus_1_0_1307.jpg", | |
} | |
# Select an example image | |
selected_example = st.selectbox("Or select an example image:", list(example_images.keys())) | |
if selected_example: | |
img_path = example_images[selected_example] | |
img = image.load_img(img_path, target_size=(224, 224)) | |
st.image(img, caption=f'Selected Example Image: {selected_example}', width=224) | |
# Classify the example image | |
result, probabilities = classify_image(img) | |
st.write(f'Predicted mosquito species: **{result}**') | |
st.write(f'Prediction probabilities: {probabilities}') | |
# File uploader for image input | |
uploaded_file = st.file_uploader("Or upload your own image...", type=["jpg", "jpeg", "png"]) | |
if uploaded_file is not None: | |
# Load the image for display | |
img = image.load_img(uploaded_file, target_size=(224, 224)) | |
st.image(img, caption='Uploaded Image', width=224) | |
# Classify the image | |
result, probabilities = classify_image(img) | |
st.write(f'Predicted mosquito species: **{result}**') | |
st.write(f'Prediction probabilities: {probabilities}') | |
# Run the app | |
if __name__ == "__main__": | |
main() | |