File size: 4,652 Bytes
b792099
 
 
 
 
 
 
 
 
 
 
 
 
cab027a
b792099
 
 
 
 
 
 
 
cab027a
b792099
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import streamlit as st
import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf
import base64
import cv2
import os
from dotenv import load_dotenv
import google.generativeai as genai
load_dotenv()
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))

header_image_path = 'What.jpg'
st.image(header_image_path, use_column_width='auto')


def get_gemini_repsonse(input,prompt):
    model=genai.GenerativeModel('gemini-pro')
    response=model.generate_content([input,prompt])
    return response.text

input_prompt= """You are an farming expert and i want some remedial and preventive information about given plant disease. give me remedial informaion for appropriate environmental condition for that particular provided disease , soil condition and what pesticides and fertilizers to use. give the information in such away that it is easy for a farmer to understand in hindi,marathi and english."""

MODEL = tf.keras.models.load_model('./potato_trained_models/1/')
TOMATO_MODEL = tf.keras.models.load_model('./tomato_trained_models/1')
PEEPER_MODEL = tf.keras.models.load_model('./pepper_trained_models/1')

class_names = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']

Tomato_classes = ['Tomato_healthy', 'Tomato_Spider_mites_Two_spotted_spider_mite', 'Tomato__Target_Spot', 'Tomato_Septoria_leaf_spot',
 'Tomato__Tomato_mosaic_virus', 'Tomato_Leaf_Mold', 'Tomato_Bacterial_spot', 'Tomato_Late_blight',
 'Tomato_Early_blight', 'Tomato__Tomato_YellowLeaf__Curl_Virus']

pepper_classes = ['pepper_bell_bacterial_spot','pepper_healthy']


st.title("Plant Disease Detection")
st.write("This application is detecting disease in three plants photato, tomato and pepper")
options = ["Select One Plant","Tomato", "Potato", "Pepper"]


selected_option = st.selectbox("Select Plant:", options)
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])

def read_file_as_image(data)->np.array:
    image = np.array(data)
    image = cv2.resize(image, (256,256))
    return image

async def potato():
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image", width=250)
        image = read_file_as_image(image)
        image_batch = np.expand_dims(image, axis=0)
        predictions = MODEL.predict(image_batch)
        predicted_class = class_names[np.argmax(predictions[0])]
        confidence = np.max(predictions[0])
        print("prediction", class_names[np.argmax(predictions)])
        st.write("Predicted Class : ", predicted_class, " Confidence Level : ", confidence)
        input=st.text_input(predicted_class,key="input")
        response=get_gemini_repsonse(input_prompt,input)
        st.subheader("The Response is")
        st.write(response)

async def tomato():
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image", width=250)
        image = read_file_as_image(image)
        image_batch = np.expand_dims(image, axis=0)
        predictions = TOMATO_MODEL.predict(image_batch)
        predicted_class = Tomato_classes[np.argmax(predictions[0])]
        confidence = np.max(predictions[0])
        print("prediction", Tomato_classes[np.argmax(predictions)])
        st.write("Predicted Class : ", predicted_class, " Confidence Level : ", confidence)
        input=st.text_input(predicted_class,key="input")
        response=get_gemini_repsonse(input_prompt,input)
        st.subheader("The Response is")
        st.write(response)


async def pepper():
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image", width=250)
        image = read_file_as_image(image)
        image_batch = np.expand_dims(image, axis=0)
        predictions = PEEPER_MODEL.predict(image_batch)
        predicted_class = pepper_classes[np.argmax(predictions[0])]
        confidence = np.max(predictions[0])
        print("prediction", pepper_classes[np.argmax(predictions)])
        st.write("Predicted Class : ", predicted_class, "Confidence Level : ", confidence)
        input=st.text_input(predicted_class,key="input")
        response=get_gemini_repsonse(input_prompt,input)
        st.subheader("The Response is")
        st.write(response)



import asyncio

if __name__ == "__main__":
     if st.button('Predict'):
    
        if selected_option == 'Potato':
            asyncio.run(potato())
        elif selected_option == 'Tomato':
            asyncio.run(tomato())
        else :
            asyncio.run(pepper())
        # else:
        #     st.write("not avalible")