Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
model = load_model('model-v1.h5')
|
| 8 |
+
|
| 9 |
+
# Define class names (replace with your actual class names)
|
| 10 |
+
class_names = [ "Speed limit (20 km/h)",
|
| 11 |
+
"Speed limit (30 km/h)",
|
| 12 |
+
"Speed limit (50 km/h)",
|
| 13 |
+
"Speed limit (60 km/h)",
|
| 14 |
+
"Speed limit (70 km/h)",
|
| 15 |
+
"Speed limit (80 km/h)",
|
| 16 |
+
"End of speed limit (80 km/h)",
|
| 17 |
+
"Speed limit (100 km/h)",
|
| 18 |
+
"Speed limit (120 km/h)",
|
| 19 |
+
"No overtaking",
|
| 20 |
+
"No overtaking vehicles over 3.5 metric tons",
|
| 21 |
+
"Priority at next intersection",
|
| 22 |
+
"Yield",
|
| 23 |
+
"Stop",
|
| 24 |
+
"No entry",
|
| 25 |
+
"No vehicles",
|
| 26 |
+
"No vehicles except motorcycles",
|
| 27 |
+
"Only motorcycles allowed",
|
| 28 |
+
"End of no vehicles",
|
| 29 |
+
"Restriction ends",
|
| 30 |
+
"Dangerous curve to the left",
|
| 31 |
+
"Dangerous curve to the right",
|
| 32 |
+
"Double curve",
|
| 33 |
+
"Bumpy road",
|
| 34 |
+
"Slippery road",
|
| 35 |
+
"Road narrows on the right",
|
| 36 |
+
"Roadwork",
|
| 37 |
+
"Traffic signals",
|
| 38 |
+
"Pedestrian crossing",
|
| 39 |
+
"School zone",
|
| 40 |
+
"Cycle path",
|
| 41 |
+
"Parking",
|
| 42 |
+
"No parking",
|
| 43 |
+
"No stopping",
|
| 44 |
+
"No pedestrians",
|
| 45 |
+
"Wild animals crossing",
|
| 46 |
+
"End of all speed and passing limits",
|
| 47 |
+
"Turn right ahead",
|
| 48 |
+
"Turn left ahead",
|
| 49 |
+
"Ahead only",
|
| 50 |
+
"Go straight or right",
|
| 51 |
+
"Go straight or left",
|
| 52 |
+
"Keep right"] # Update this with your classes
|
| 53 |
+
|
| 54 |
+
# Preprocessing function
|
| 55 |
+
def preprocess_image(image):
|
| 56 |
+
image = image.resize((30, 30)) # Resize to model's input size
|
| 57 |
+
image_array = img_to_array(image) / 255.0 # Normalize to [0, 1]
|
| 58 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 59 |
+
return image_array
|
| 60 |
+
|
| 61 |
+
# Prediction function
|
| 62 |
+
def classify_image(image):
|
| 63 |
+
# Preprocess the image
|
| 64 |
+
preprocessed_image = preprocess_image(image)
|
| 65 |
+
|
| 66 |
+
# Make prediction
|
| 67 |
+
predictions = model.predict(preprocessed_image)
|
| 68 |
+
|
| 69 |
+
# Get the class with the highest probability
|
| 70 |
+
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 71 |
+
|
| 72 |
+
# Get the class name
|
| 73 |
+
class_name = class_names[predicted_class]
|
| 74 |
+
|
| 75 |
+
return f"Predicted Class: {class_name}"
|
| 76 |
+
|
| 77 |
+
# Create Gradio interface
|
| 78 |
+
interface = gr.Interface(
|
| 79 |
+
fn=classify_image, # Function to handle predictions
|
| 80 |
+
inputs=gr.inputs.Image(type="pil"), # Accept image input
|
| 81 |
+
outputs="text", # Display output as text
|
| 82 |
+
title="Image Classification",
|
| 83 |
+
description="Upload an image to classify it into one of the predefined classes."
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Launch the app
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
interface.launch()
|