Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras.preprocessing import image | |
| from tensorflow.keras.applications.efficientnet import preprocess_input | |
| from PIL import Image | |
| model = load_model("src/best_efficientnet_model.keras") | |
| class_names = [ | |
| 'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum', 'carrot', | |
| 'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant', 'garlic', 'ginger', | |
| 'grapes', 'jalepeno', 'kiwi', 'lemon', 'lettuce', 'mango', 'onion', 'orange', | |
| 'paprika', 'pear', 'peas', 'pineapple', 'pomegranate', 'potato', 'raddish', | |
| 'soy beans', 'spinach', 'sweetcorn', 'sweetpotato', 'tomato', 'turnip', 'watermelon' | |
| ] | |
| st.title("Fruit and Vegetable Recognition") | |
| st.markdown("Upload an image of a fruit or vegetable. The model will predict its class.") | |
| uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| img = Image.open(uploaded_file).convert("RGB") | |
| st.image(img, caption="Uploaded Image", use_container_width=True) | |
| img = img.resize((300, 300)) | |
| img_array = image.img_to_array(img) | |
| img_array = np.expand_dims(img_array, axis=0) | |
| img_array = preprocess_input(img_array) | |
| predictions = model.predict(img_array) | |
| predicted_index = np.argmax(predictions) | |
| predicted_label = class_names[predicted_index] | |
| confidence = predictions[0][predicted_index] * 100 | |
| st.markdown(f"### Prediction: **{predicted_label}**") | |