Anshul3878 commited on
Commit
688c4eb
·
verified ·
1 Parent(s): 77a3cc9

Upload 5 files

Browse files
Plant_Disease_Prediction_CNN_Image_Classifier.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
main.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ working_dir = os.path.dirname(os.path.abspath(__file__))
11
+ model_path = f"{working_dir}/trained_model/plant_disease_prediction_model.h5"
12
+ # Load 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
+
19
+ # Function to Load and Preprocess the Image using Pillow
20
+ def load_and_preprocess_image(image_path, target_size=(224, 224)):
21
+ # Load the image
22
+ img = Image.open(image_path)
23
+ # Resize the image
24
+ img = img.resize(target_size)
25
+ # Convert the image to a numpy array
26
+ img_array = np.array(img)
27
+ # Add batch dimension
28
+ img_array = np.expand_dims(img_array, axis=0)
29
+ # Scale the image values to [0, 1]
30
+ img_array = img_array.astype('float32') / 255.
31
+ return img_array
32
+
33
+
34
+ # Function to Predict the Class of an Image
35
+ def predict_image_class(model, image_path, class_indices):
36
+ preprocessed_img = load_and_preprocess_image(image_path)
37
+ predictions = model.predict(preprocessed_img)
38
+ predicted_class_index = np.argmax(predictions, axis=1)[0]
39
+ predicted_class_name = class_indices[str(predicted_class_index)]
40
+ return predicted_class_name
41
+
42
+
43
+ # Streamlit App
44
+ st.title('Plant Disease Classifier')
45
+
46
+ uploaded_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
47
+
48
+ if uploaded_image is not None:
49
+ image = Image.open(uploaded_image)
50
+ col1, col2 = st.columns(2)
51
+
52
+ with col1:
53
+ resized_img = image.resize((150, 150))
54
+ st.image(resized_img)
55
+
56
+ with col2:
57
+ if st.button('Classify'):
58
+ # Preprocess the uploaded image and predict the class
59
+ prediction = predict_image_class(model, uploaded_image, class_indices)
60
+ st.success(f'Prediction: {str(prediction)}')
plant_disease_prediction_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b0d196c3af57c9db66072e031922fedef1a0980f2ff5d859ec203f72a31f0646
3
+ size 573706416
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ numpy==1.26.3
2
+ streamlit==1.30.0
3
+ tensorflow==2.15.0.post1
trained_model_link.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ trained plant disease prediction model link: https://drive.google.com/file/d/1rKh-IElSdHTqax7XdfSdZTn-r8T_qWPf/view?usp=drive_link