courte commited on
Commit
2e337bb
·
verified ·
1 Parent(s): d989aac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -1,22 +1,23 @@
1
- import tensorflow as tf
2
  import numpy as np
3
- from PIL import Image
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(image):
11
- image = image.resize((224, 224)) # Resize to match EfficientNetB0 input size
12
- image = np.array(image) # Convert to NumPy array
13
- image = tf.keras.applications.efficientnet.preprocess_input(image) # EfficientNetB0 normalization
 
14
  image = np.expand_dims(image, axis=0) # Add batch dimension
15
  return image
16
 
17
  # Define the prediction function
18
- def predict(image):
19
- processed_image = preprocess_image(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="image",
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."