KevinJuanCarlos23 commited on
Commit
213a63f
·
verified ·
1 Parent(s): ee4d3d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py CHANGED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ from tensorflow.keras.models import load_model
5
+
6
+ model_path = "vehicle_classification_model.keras"
7
+ try:
8
+ model = load_model(model_path)
9
+ except OSError as e:
10
+ print("Error loading model:", e)
11
+
12
+ class_labels = ['Auto Rickshaw', 'Bike', 'Car', 'Motorcycle', 'Plane', 'Ship', 'Train']
13
+
14
+ def predict_vehicle(img):
15
+ img = img.resize((224, 224))
16
+ img = np.array(img) / 255.0
17
+ img = np.expand_dims(img, axis=0)
18
+
19
+ predictions = model.predict(img)
20
+ predicted_class = np.argmax(predictions, axis=1)[0]
21
+ confidence = np.max(predictions, axis=1)[0]
22
+ predicted_label = class_labels[predicted_class]
23
+
24
+ return {"vehicle_type": predicted_label, "confidence": float(confidence)}
25
+
26
+ gr.Interface(
27
+ fn=predict_vehicle,
28
+ inputs=gr.Image(type="pil", label="Upload Image"),
29
+ outputs=gr.JSON(label="Prediction Result"),
30
+ title="Vehicle Classification System",
31
+ description="Upload an image of a vehicle and the model will predict its type.",
32
+ live=True
33
+ ).launch()