import gradio as gr import tensorflow as tf import os class_names = ['apples', 'artichokes', 'asparagus', 'aubergine', 'avocados', 'banana', 'bell peppers', 'broccoli', 'carrots', 'celery', 'courgette', 'garlic', 'ginger', 'grapefruit', 'grapes', 'kiwi', 'mangos', 'mushrooms', 'onions', 'orange', 'passion fruit', 'peas', 'pineapple', 'potatoes', 'salad', 'spinach', 'strawberry', 'sweet potatos', 'tomato', 'watermelon'] model = tf.keras.models.load_model("fruits_and_vegetables_model.h5") def load_and_predict_img(inp, img_shape=224): img_4d=inp.reshape(-1,224,224,3) # reshape our image for passing to the model prediction = model.predict(img_4d) # predicting using the model pred_labels_and_probs = {class_names[i]: float(prediction[0][i]) for i in range(len(class_names))} return_string = f"We believe this is a {class_names[tf.argmax(prediction[0])]}, we are {round(float(prediction[0][tf.argmax(prediction[0])]),2)}% confident in our prediction" return return_string, pred_labels_and_probs # Create examples list from "examples/" directory example_list = [["examples/" + example] for example in os.listdir("examples")] fruits_and_veggies = gr.Interface( fn=load_and_predict_img, inputs=gr.Image(shape=(224,224), label="Upload an image"), outputs=[gr.Textbox(label="Result"), gr.Label(num_top_classes=3, label="Top 3 Predictions")], title="Fruits and Vegetable Identifier", description="""Take photos of your fruits and vegetables to identify them. Here is a list of fruits and veggies that this application can recognize... Apples, Artichokes, Asparagus, Aubergine, Avocados, Banana, Bell Pepper, Broccoli, Carrot, Celery, Courgette, Garlic, Ginger, Grapefruit, Grapes, Kiwi, Mangos, Mushrooms, Onions, Oranges, Passion Fruit, Peas, Pineapple, Potato, Salad, Spinach, Strawberry, Sweet Potato, Tomato, Watermelon""", allow_flagging="never", examples=example_list ) fruits_and_veggies.launch()