rajsecrets0 commited on
Commit
e1eb5aa
·
verified ·
1 Parent(s): bb62263

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+ import json
6
+
7
+ # Load class indices
8
+ with open("class_indices.json", "r") as f:
9
+ class_indices = json.load(f)
10
+
11
+ # Reverse the mapping for predictions
12
+ class_names = {v: k for k, v in class_indices.items()}
13
+
14
+ # Load the TFLite model
15
+ interpreter = tf.lite.Interpreter(model_path="model.tflite")
16
+ interpreter.allocate_tensors()
17
+
18
+ # Get input and output details
19
+ input_details = interpreter.get_input_details()
20
+ output_details = interpreter.get_output_details()
21
+
22
+ # Define the image preprocessing function
23
+ def preprocess_image(image, target_size=(224, 224)):
24
+ image = image.resize(target_size)
25
+ image = np.array(image) / 255.0 # Normalize the image
26
+ image = np.expand_dims(image, axis=0) # Add batch dimension
27
+ return image.astype(np.float32)
28
+
29
+ # Define prediction function
30
+ def predict(image):
31
+ input_data = preprocess_image(image)
32
+ interpreter.set_tensor(input_details[0]['index'], input_data)
33
+ interpreter.invoke()
34
+ output_data = interpreter.get_tensor(output_details[0]['index'])
35
+ predicted_class = np.argmax(output_data)
36
+ confidence = np.max(output_data)
37
+ return class_names[predicted_class], confidence
38
+
39
+ # Streamlit UI
40
+ st.title("🌾 Crop Disease Prediction")
41
+ st.write("Upload an image of a crop leaf, and the app will predict the disease (if any).")
42
+
43
+ uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "png", "jpeg"])
44
+
45
+ if uploaded_file is not None:
46
+ # Display the uploaded image
47
+ image = Image.open(uploaded_file)
48
+ st.image(image, caption="Uploaded Image", use_column_width=True)
49
+
50
+ st.write("Processing...")
51
+
52
+ # Perform prediction
53
+ predicted_class, confidence = predict(image)
54
+ st.write(f"**Prediction:** {predicted_class}")
55
+ st.write(f"**Confidence:** {confidence:.2f}")