File size: 2,921 Bytes
b42b45f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from keras.models import load_model
from fastapi import FastAPI
import gradio as gr
import requests
import numpy as np
from PIL import Image
from tensorflow import keras
from io import BytesIO
import uvicorn
from pyModel import Item



app = FastAPI()



@app.post('/post')
def newPost(str):
    input_image = url_to_img(str)
    processed_image = process(input_image)
    crop = crop_predict(processed_image)
    disease = disease_predict(processed_image, crop)
    return [crop,disease]
  

def url_to_img(image_url):

    # Load the image from the URL
    response = requests.get(image_url)
    input_image = Image.open(BytesIO(response.content))
    return input_image


def process(input_image):

    # Preprocess the image to match model's input dimensions
    input_image = input_image.resize((224, 224))  # Adjust the dimensions
    input_image = np.array(input_image) / 255.0  # Normalize pixel values to [0, 1]
    processed_image = np.expand_dims(input_image, axis=0)  # Add batch dimension
    return processed_image

def crop_predict(processed_image):

    # Load the trained model
    model = keras.models.load_model('./crop_model.h5')
    # Make predictions
    predictions = model.predict(processed_image)
    # Process the predictions (e.g., get class labels)
    class_labels = ["Corn", "Potato", "Rice", "Wheat"] 
    predicted_class = np.argmax(predictions, axis=1)
    predicted_label = class_labels[predicted_class[0]]
    return predicted_label

def disease_predict(processed_image, crop):

    if crop == "Potato":
        # Load the trained model
        model = keras.models.load_model('./potato_model.h5')
        # Process the predictions (e.g., get class labels)
        class_labels = ["Potato_Early_Blight", "Potato_Healthy", "Potato_Late_Blight"] 
    
    elif crop == "Corn":
        # Load the trained model
        model = keras.models.load_model('./corn_model.h5')
        # Process the predictions (e.g., get class labels)
        class_labels = ["Corn_Common_Rust", "Corn_Gray_Leaf_Spot", "Corn_Healthy", "Corn_Northern_Leaf_Blight"] 

    elif crop == "Rice":
        # Load the trained model
        model = keras.models.load_model('./rice_model.h5')
        # Process the predictions (e.g., get class labels)
        class_labels = ["Rice_Brown_Spot", "Rice_Healthy", "Rice_Leaf_Blast", "Rice_Neck_Blast"] 

    elif crop == "Wheat":
        # Load the trained model
        model = keras.models.load_model('./wheat_model.h5')
        # Process the predictions (e.g., get class labels)
        class_labels = ["Wheat_Brown_Rust", "Wheat_Healthy", "Wheat_Yellow_Rust"]

    # Make predictions
    predictions = model.predict(processed_image)
    predicted_class = np.argmax(predictions, axis=1)
    predicted_label = class_labels[predicted_class[0]]
    return predicted_label


# iface = gr.Interface(fn = newPost , inputs=['text'] , outputs=['text'])

# iface.launch(share=True)