{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "import tensorflow as tf\n", "import numpy as np\n", "from PIL import Image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Loading trained model\n", "model = tf.keras.models.load_model(\"model.h5\") " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "# Label Overview\n", "class_num = { 0:'Speed limit (20km/h)',\n", " 1:'Speed limit (30km/h)', +\n", " 2:'Speed limit (50km/h)', \n", " 3:'Speed limit (60km/h)', \n", " 4:'Speed limit (70km/h)', \n", " 5:'Speed limit (80km/h)', \n", " 6:'End of speed limit (80km/h)', \n", " 7:'Speed limit (100km/h)', \n", " 8:'Speed limit (120km/h)', \n", " 9:'No passing', \n", " 10:'No passing veh over 3.5 tons', \n", " 11:'Right-of-way at intersection', \n", " 12:'Priority road', \n", " 13:'Yield', \n", " 14:'Stop', \n", " 15:'No vehicles', \n", " 16:'Veh > 3.5 tons prohibited', \n", " 17:'No entry', \n", " 18:'General caution', \n", " 19:'Dangerous curve left', \n", " 20:'Dangerous curve right', \n", " 21:'Double curve', \n", " 22:'Bumpy road', \n", " 23:'Slippery road', \n", " 24:'Road narrows on the right', \n", " 25:'Road work', \n", " 26:'Traffic signals', \n", " 27:'Pedestrians', \n", " 28:'Children crossing', \n", " 29:'Bicycles crossing', \n", " 30:'Beware of ice/snow',\n", " 31:'Wild animals crossing', \n", " 32:'End speed + passing limits', \n", " 33:'Turn right ahead', \n", " 34:'Turn left ahead', \n", " 35:'Ahead only', \n", " 36:'Go straight or right', \n", " 37:'Go straight or left', \n", " 38:'Keep right', \n", " 39:'Keep left', \n", " 40:'Roundabout mandatory', \n", " 41:'End of no passing', \n", " 42:'End no passing veh > 3.5 tons' }\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "# Define a function to preprocess input image\n", "def preprocess_image(image):\n", " # Resize the image to the required dimensions\n", " image = image.resize((30, 30))\n", " # Convert the PIL image to a NumPy array\n", " image_array = np.array(image)\n", " # Normalize pixel values to be between 0 and 1\n", " image_array = image_array / 255.0\n", " # Add batch dimension to the image\n", " image_array = np.expand_dims(image_array, axis=0)\n", " return image_array\n", "\n", "# Define a function to make predictions\n", "def predict_class(image):\n", " # Preprocess the input image\n", " processed_image = preprocess_image(image)\n", " # Make predictions using the loaded model\n", " predictions = model.predict(processed_image)\n", " # Get the predicted class index\n", " predicted_class_index = np.argmax(predictions)\n", " # Return the predicted class label and class number\n", " return f\"{class_num[predicted_class_index]} (Class {predicted_class_index})\"\n", "\n", "# Create Gradio interface\n", "iface = gr.Interface(\n", " fn=predict_class,\n", " inputs=gr.Image(type=\"pil\", label=\"Upload an image\"),\n", " outputs=\"text\"\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Launch the Gradio interface\n", "iface.launch()\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }