kiki7555 commited on
Commit
551812e
1 Parent(s): c4c9963

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from PIL import Image
4
+ import numpy as np
5
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
6
+
7
+ # Load the car brand classifier model
8
+ model_path = "car_brand_classifier_finetuned.keras"
9
+ model = tf.keras.models.load_model(model_path)
10
+
11
+ labels = ['Hyundai', 'Lexus', 'Mazda', 'Mercedes', 'Opel', 'Skoda', 'Toyota', 'Volkswagen']
12
+
13
+ # Define function for car brand classification with data augmentation
14
+ def preprocess_image(image):
15
+ image = Image.fromarray(image.astype('uint8'), 'RGB')
16
+ image = image.resize((224, 224))
17
+ image = np.array(image)
18
+ image = image / 255.0 # Normalize pixel values
19
+ return image
20
+
21
+
22
+ # Prediction function
23
+ def predict_car_brand(image):
24
+ image = preprocess_image(image)
25
+ prediction = model.predict(np.expand_dims(image, axis=0))
26
+ predicted_class = labels[np.argmax(prediction)]
27
+ confidence = np.round(np.max(prediction) * 100, 2)
28
+ result = f"Label: {predicted_class}, Confidence: {confidence}%"
29
+ return result
30
+
31
+ # Create Gradio interface
32
+ input_image = gr.Image()
33
+ output_text = gr.Textbox(label="Car Brand")
34
+ interface = gr.Interface(fn=predict_car_brand,
35
+ inputs=input_image,
36
+ outputs=output_text,
37
+ description="A car brand classifier using transfer learning and fine-tuning with EfficientNetB0.",
38
+ theme="default")
39
+
40
+ if __name__ == "__main__":
41
+ interface.launch()