Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,23 @@
|
|
| 1 |
-
import
|
| 2 |
import numpy as np
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# Load the EfficientNetB0 model
|
| 7 |
model = tf.keras.models.load_model("car_brand_classifier_final.h5")
|
| 8 |
|
| 9 |
-
# EfficientNetB0 preprocessing function
|
| 10 |
-
def preprocess_image(
|
| 11 |
-
image =
|
| 12 |
-
image =
|
| 13 |
-
image =
|
|
|
|
| 14 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 15 |
return image
|
| 16 |
|
| 17 |
# Define the prediction function
|
| 18 |
-
def predict(
|
| 19 |
-
processed_image = preprocess_image(
|
| 20 |
predictions = model.predict(processed_image) # Model inference
|
| 21 |
predicted_class = np.argmax(predictions, axis=1)[0] # Get class with highest probability
|
| 22 |
|
|
@@ -25,7 +26,7 @@ def predict(image):
|
|
| 25 |
# Create the Gradio interface
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=predict,
|
| 28 |
-
inputs="
|
| 29 |
outputs="text",
|
| 30 |
title="Car Brand Classifier",
|
| 31 |
description="Upload an image of a car to classify its brand."
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
# Load the EfficientNetB0 model
|
| 7 |
model = tf.keras.models.load_model("car_brand_classifier_final.h5")
|
| 8 |
|
| 9 |
+
# EfficientNetB0 preprocessing function using OpenCV
|
| 10 |
+
def preprocess_image(image_path):
|
| 11 |
+
image = cv2.imread(image_path) # Load image using OpenCV
|
| 12 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
|
| 13 |
+
image = cv2.resize(image, (224, 224)) # Resize to (224, 224)
|
| 14 |
+
image = image.astype(np.float32) / 255.0 # Normalize pixel values (0-1)
|
| 15 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 16 |
return image
|
| 17 |
|
| 18 |
# Define the prediction function
|
| 19 |
+
def predict(image_path):
|
| 20 |
+
processed_image = preprocess_image(image_path) # Preprocess image
|
| 21 |
predictions = model.predict(processed_image) # Model inference
|
| 22 |
predicted_class = np.argmax(predictions, axis=1)[0] # Get class with highest probability
|
| 23 |
|
|
|
|
| 26 |
# Create the Gradio interface
|
| 27 |
iface = gr.Interface(
|
| 28 |
fn=predict,
|
| 29 |
+
inputs=gr.components.Image(type="filepath", label="Upload Car Image"),
|
| 30 |
outputs="text",
|
| 31 |
title="Car Brand Classifier",
|
| 32 |
description="Upload an image of a car to classify its brand."
|