Spaces:
Sleeping
Sleeping
streamlit app
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Run this file using Streamlit command: streamlit run main.py
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
class CoffeeLandClassifier:
|
| 11 |
+
def __init__(self, model_path, class_labels):
|
| 12 |
+
self.model = tf.keras.models.load_model(model_path)
|
| 13 |
+
self.class_labels = class_labels
|
| 14 |
+
|
| 15 |
+
def run(self):
|
| 16 |
+
st.title("Coffee Land Classifier")
|
| 17 |
+
|
| 18 |
+
# Create a file uploader widget
|
| 19 |
+
uploaded_image = st.file_uploader("Please upload an image", type=["jpg", "jpeg", "png"])
|
| 20 |
+
|
| 21 |
+
if uploaded_image is not None:
|
| 22 |
+
# Load and preprocess the uploaded image
|
| 23 |
+
img = Image.open(uploaded_image)
|
| 24 |
+
img = img.resize((64, 64)) # Resize the image to match the model's input shape
|
| 25 |
+
img = np.array(img)
|
| 26 |
+
img = img.astype('float32') / 255.0
|
| 27 |
+
img = np.expand_dims(img, axis=0)
|
| 28 |
+
|
| 29 |
+
# Make a prediction
|
| 30 |
+
predictions = self.model.predict(img)
|
| 31 |
+
class_index = np.argmax(predictions)
|
| 32 |
+
predicted_class = self.class_labels[class_index]
|
| 33 |
+
|
| 34 |
+
# Display the uploaded image
|
| 35 |
+
st.image(img[0])
|
| 36 |
+
|
| 37 |
+
# Show the prediction result
|
| 38 |
+
st.write(f"Prediction: {predicted_class}")
|
| 39 |
+
st.write("Class Probabilities:")
|
| 40 |
+
for i, prob in enumerate(predictions[0]):
|
| 41 |
+
st.write(f"{self.class_labels[i]}: {prob * 100:.2f}%")
|
| 42 |
+
|
| 43 |
+
def main():
|
| 44 |
+
model_path = "model/model.h5"
|
| 45 |
+
class_labels = ["Coffee Land", "Not Coffee Land"] # Class labels
|
| 46 |
+
classifier = CoffeeLandClassifier(model_path, class_labels)
|
| 47 |
+
classifier.run()
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
main()
|