import numpy as np import gradio as gr import tensorflow as tf #version 2.13.0 import keras #version import numpy as np import cv2 import tensorflow as tf import h5py def analyse(img,plant_type): import json # Load label_disease.json with open('data/label_disease.json', 'r') as f: label_disease = json.load(f) # Load plant_label_disease.json with open('data/plant_label_disease.json', 'r') as f: plant_label_disease = json.load(f) print(label_disease,label_disease["34"]) HEIGHT = 256 WIDTH = 256 modelArchitecturePath ='model/model_architecture.h5' modelWeightsPath = 'model/model_weights.h5' dnn_model = keras.models.load_model(modelArchitecturePath,compile=False) dnn_model.load_weights(modelWeightsPath) process_img = cv2.resize(img, (HEIGHT, WIDTH),interpolation = cv2.INTER_LINEAR) process_img = process_img/(255) process_img = np.expand_dims(process_img, axis=0) y_pred = dnn_model.predict(process_img) y_pred = y_pred[0] print("y pred",y_pred) id = np.argmax(y_pred) p_id = plant_label_disease[plant_type.lower()][0] for disease in plant_label_disease[plant_type.lower()]: if y_pred[disease] > y_pred[p_id]: p_id = disease overall_predicted_id = id overall_predicted_name = label_disease[str(overall_predicted_id)] overall_predicted_acc = y_pred[overall_predicted_id] plant_predicted_id =pid plant_predicted_name = label_disease[str(plant_predicted_id)] plant_predicted_acc = y_pred[plant_predicted_id] return plant_predicted_id, plant_predicted_name, plant_predicted_acc, overall_predicted_id, overall_predicted_name, overall_predicted_acc demo = gr.Interface(analyse, [gr.Image(),gr.Radio(["Apple","Blueberry","Cherry","Corn","Grape","Orange","Peach","Pepper","Potato","Raspberry","Soybean","Squash","Strawberry","Tomato"])], ["number","text","number","number","text","number"], ) demo.launch(share=True,show_error=True)