harshpatel080503 commited on
Commit
4a1d692
·
verified ·
1 Parent(s): b6547ab

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +58 -0
main.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from PIL import Image
4
+
5
+ import numpy as np
6
+ import tensorflow as tf
7
+ import streamlit as st
8
+
9
+ working_dir = os.path.dirname(os.path.abspath(__file__))
10
+ model_path = f"{working_dir}/model/plant_disease_prediction_model.h5"
11
+
12
+ # Loading the pre-trained model
13
+ model = tf.keras.models.load_model(model_path)
14
+
15
+ # Loading the class names
16
+ class_indices = json.load(open(f"{working_dir}/class_indices.json"))
17
+
18
+ # Function to Load and Preprocess the Image using Pillow
19
+ def load_and_preprocess_image(image_path, target_size=(224, 224)):
20
+ # Load the image
21
+ img = Image.open(image_path)
22
+ # Resize the image
23
+ img = img.resize(target_size)
24
+ # Convert the image to a numpy array
25
+ img_array = np.array(img)
26
+ # Add batch dimension
27
+ img_array = np.expand_dims(img_array, axis=0)
28
+ # Scale the image values to [0, 1]
29
+ img_array = img_array.astype('float32') / 255.
30
+ return img_array
31
+
32
+
33
+ # Function to Predict the Class of an Image
34
+ def predict_image_class(model, image_path, class_indices):
35
+ preprocessed_img = load_and_preprocess_image(image_path)
36
+ predictions = model.predict(preprocessed_img)
37
+ predicted_class_index = np.argmax(predictions, axis=1)[0]
38
+ predicted_class_name = class_indices[str(predicted_class_index)]
39
+ return predicted_class_name
40
+
41
+ # Streamlit App
42
+ st.title('Plant Disease Classification')
43
+
44
+ uploaded_image = st.file_uploader("Upload an Plant Image....",type=["jpg","jpeg","png"])
45
+
46
+ if uploaded_image is not None:
47
+ image = Image.open(uploaded_image)
48
+ col1,col2 = st.columns(2)
49
+
50
+ with col1:
51
+ resized_img = image.resize((150,150))
52
+ st.image(resized_img)
53
+
54
+ with col2:
55
+ if st.button("Classify"):
56
+ # Preprocess the uploaded image and predict the class
57
+ prediction = predict_image_class(model,uploaded_image,class_indices)
58
+ st.success(f'Prediction: {str(prediction)}')