Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,43 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
-
import
|
| 5 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
image =
|
| 14 |
-
image = image.resize((299, 299)) # Resize to model input size
|
| 15 |
-
image = np.array(image) / 255.0 # Normalize pixel values
|
| 16 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 17 |
return image
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
def predict(
|
| 21 |
-
processed_image = preprocess_image(
|
| 22 |
-
predictions = model.predict(processed_image)
|
| 23 |
-
predicted_class = np.argmax(predictions, axis=1)[0] # Get class
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
iface = gr.Interface(
|
| 29 |
fn=predict,
|
| 30 |
-
inputs=
|
| 31 |
-
outputs="
|
| 32 |
-
title="Car
|
| 33 |
-
description="Upload an image of a car
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Launch the app
|
| 37 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
+
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Load the model
|
| 8 |
+
model = tf.keras.models.load_model("car_brand_classifier_final.h5", compile=False)
|
| 9 |
|
| 10 |
+
# Define image directory where car brand images are stored
|
| 11 |
+
IMAGE_DIR = "car_brands" # Make sure this folder exists with images named as class labels
|
| 12 |
|
| 13 |
+
# Preprocess image to match EfficientNetB0 input requirements
|
| 14 |
+
def preprocess_image(image):
|
| 15 |
+
image = image.resize((224, 224)) # Resize to match EfficientNetB0
|
| 16 |
+
image = np.array(image) / 255.0 # Normalize
|
|
|
|
|
|
|
| 17 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 18 |
return image
|
| 19 |
|
| 20 |
+
# Prediction function
|
| 21 |
+
def predict(image):
|
| 22 |
+
processed_image = preprocess_image(image)
|
| 23 |
+
predictions = model.predict(processed_image)
|
| 24 |
+
predicted_class = np.argmax(predictions, axis=1)[0] # Get predicted class index
|
| 25 |
+
|
| 26 |
+
# Find corresponding image for predicted class
|
| 27 |
+
matching_image_path = os.path.join(IMAGE_DIR, f"{predicted_class}.jpg")
|
| 28 |
|
| 29 |
+
if os.path.exists(matching_image_path):
|
| 30 |
+
return matching_image_path # Return image file path
|
| 31 |
+
else:
|
| 32 |
+
return "No matching image found"
|
| 33 |
|
| 34 |
+
# Gradio Interface
|
| 35 |
iface = gr.Interface(
|
| 36 |
fn=predict,
|
| 37 |
+
inputs="image",
|
| 38 |
+
outputs="image", # Change output to "image"
|
| 39 |
+
title="Car Vision",
|
| 40 |
+
description="Upload an image of a car, and get a matching brand image.",
|
| 41 |
)
|
| 42 |
|
|
|
|
| 43 |
iface.launch()
|