courte commited on
Commit
57cee7f
·
verified ·
1 Parent(s): 2505f06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -23
app.py CHANGED
@@ -1,37 +1,43 @@
1
- import cv2
2
- import numpy as np
3
  import tensorflow as tf
4
- import gradio as gr
5
  from PIL import Image
 
 
 
 
 
6
 
7
- # Load the EfficientNetB0 model
8
- model = tf.keras.models.load_model("car_brand_classifier_final.h5")
9
 
10
- # EfficientNetB0 preprocessing function using OpenCV
11
- # Define the preprocessing function
12
- def preprocess_image(image_path):
13
- image = Image.open(image_path) # Open image from path
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
- # Define the prediction function
20
- def predict(image_path):
21
- processed_image = preprocess_image(image_path) # Preprocess image
22
- predictions = model.predict(processed_image) # Model inference
23
- predicted_class = np.argmax(predictions, axis=1)[0] # Get class with highest probability
 
 
 
24
 
25
- return f"Predicted class: {predicted_class}"
 
 
 
26
 
27
- # Create the Gradio interface
28
  iface = gr.Interface(
29
  fn=predict,
30
- inputs=gr.components.Image(type="filepath", label="Upload Car Image"),
31
- outputs="text",
32
- title="Car Brand Classifier",
33
- description="Upload an image of a car to classify its brand."
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()