| import gradio as gr |
| import tensorflow as tf |
| import numpy as np |
| from PIL import Image |
|
|
| |
| |
| model = tf.keras.models.load_model("rice_stage_model.keras") |
|
|
| |
| classes = ["flowering", "germination", "noise", "tillering"] |
|
|
| |
| CONFIDENCE_THRESHOLD = 0.6 |
|
|
| def predict_stage(image): |
| if image is None: |
| return "⚠️ Please upload an image first." |
| |
| |
| image = image.convert("RGB") |
| |
| |
| image = image.resize((224, 224)) |
|
|
| |
| img = np.array(image) / 255.0 |
| img = np.expand_dims(img, axis=0) |
|
|
| |
| predictions = model.predict(img)[0] |
|
|
| |
| predicted_index = np.argmax(predictions) |
| confidence = float(predictions[predicted_index]) |
| predicted_label = classes[predicted_index] |
|
|
| |
| if predicted_label == "noise": |
| return "⚠️ Invalid image. Please upload a clear rice crop image." |
|
|
| if confidence < CONFIDENCE_THRESHOLD: |
| return "⚠️ Model uncertain. Please upload a clearer crop photo." |
|
|
| |
| return f"🌾 Predicted Stage: {predicted_label.capitalize()} | Confidence: {confidence:.2f}" |
|
|
| |
| |
| interface = gr.Interface( |
| fn=predict_stage, |
| inputs=gr.Image(type="pil"), |
| outputs="text", |
| title="Rice Crop Growth Stage Classifier", |
| description="Upload a picture of the rice crop from the field to instantly detect its growth stage.", |
| flagging_mode="never" |
| ) |
|
|
| if __name__ == "__main__": |
| |
| interface.launch( |
| server_name="0.0.0.0", |
| server_port=7860, |
| ssr_mode=False, |
| show_error=True |
| ) |