File size: 1,672 Bytes
e537686
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

# Run this file using Streamlit command: streamlit run main.py

import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
import io

class CoffeeLandClassifier:
    def __init__(self, model_path, class_labels):
        self.model = tf.keras.models.load_model(model_path)
        self.class_labels = class_labels

    def run(self):
        st.title("Coffee Land Classifier")

        # Create a file uploader widget
        uploaded_image = st.file_uploader("Please upload an image", type=["jpg", "jpeg", "png"])

        if uploaded_image is not None:
            # Load and preprocess the uploaded image
            img = Image.open(uploaded_image)
            img = img.resize((64, 64))  # Resize the image to match the model's input shape
            img = np.array(img)
            img = img.astype('float32') / 255.0
            img = np.expand_dims(img, axis=0)

            # Make a prediction
            predictions = self.model.predict(img)
            class_index = np.argmax(predictions)
            predicted_class = self.class_labels[class_index]

            # Display the uploaded image
            st.image(img[0])

            # Show the prediction result
            st.write(f"Prediction: {predicted_class}")
            st.write("Class Probabilities:")
            for i, prob in enumerate(predictions[0]):
                st.write(f"{self.class_labels[i]}: {prob * 100:.2f}%")

def main():
    model_path = "model/model.h5"
    class_labels = ["Coffee Land", "Not Coffee Land"]  # Class labels
    classifier = CoffeeLandClassifier(model_path, class_labels)
    classifier.run()

if __name__ == "__main__":
    main()