File size: 2,656 Bytes
bad10c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e1cd5d
 
94ccad9
 
 
 
 
 
 
 
 
 
 
 
 
6e1cd5d
 
 
 
 
 
 
 
 
 
 
 
 
94ccad9
6e1cd5d
 
 
 
 
bad10c1
 
 
 
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
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.")

    # File uploader for image input
    uploaded_file = st.file_uploader("Upload your own image...", type=["jpg", "jpeg", "png"])

    if uploaded_file is not None:
        # Load the uploaded image for display
        img = image.load_img(uploaded_file, target_size=(224, 224))
        st.image(img, caption='Uploaded Image', width=224)
        
        # Classify the uploaded image
        result, probabilities = classify_image(img)
        st.write(f'Predicted mosquito species: **{result}**')
        st.write(f'Prediction probabilities: {probabilities}')

    # 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}')

# Run the app
if __name__ == "__main__":
    main()