import gradio as gr import tensorflow as tf import numpy as np from PIL import Image model_path = "fungi-model_transferlearning_xception.keras" model = tf.keras.models.load_model(model_path) def predict_fungi(image): # Preprocess image print(type(image)) image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale image = np.array(image) image = np.expand_dims(image, axis=0) # same as image[None, ...] prediction = model.predict(image) # Convert the probabilities to rounded values prediction = np.round(prediction, 2) # Separate the probabilities for each class p_ediblemushroomsporocarp = prediction[0][0] p_ediblesporocarp = prediction[0][1] p_poisonousmushroomsporocarp = prediction[0][2] p_poisonoussporocarp = prediction[0][2] return {'Edible Mushroom Sporocarp': p_ediblemushroomsporocarp, 'Edible Sporocarp': p_ediblesporocarp, 'Poisonous Mushroom Sporocarp': p_poisonousmushroomsporocarp, 'Poisonous Sporocarp': p_poisonoussporocarp} input_image = gr.Image() iface = gr.Interface( fn=predict_fungi, inputs=input_image, outputs=gr.Label(), examples=["images/ce (1).jpg", "images/ce (2).jpg", "images/ncvc (1).png", "images/ncvc (5).png", "images/nncv (2).png", "images/nncv (4).png", "images/cv (2).png", "images/cv (4).png"], title="Is it edible?", description="'sporocarp' is a broad term encompassing any spore-producing structure in various organisms, while 'mushroom sporocarp' specifically refers to the spore-producing structures of fungi that are typically visible and recognizable as mushrooms.") iface.launch()