faranbutt789 commited on
Commit
f51c85d
·
verified ·
1 Parent(s): 316921a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn as nn
4
+ import torchvision.transforms as T
5
+ import cv2
6
+ import numpy as np
7
+ import pandas as pd
8
+
9
+ # ----------------------------
10
+ # Load class names
11
+ # ----------------------------
12
+ df = pd.read_csv("signnames.csv")
13
+ df.set_index("ClassId", inplace=True)
14
+ class_ids = df.to_dict()["SignName"]
15
+ id2int = {v: i for i, (k, v) in enumerate(class_ids.items())}
16
+ int2id = {v: k for k, v in id2int.items()}
17
+
18
+ # ----------------------------
19
+ # Define Model
20
+ # ----------------------------
21
+ def conv_func(in_channels, out_channels):
22
+ return nn.Sequential(
23
+ nn.Dropout(0.2),
24
+ nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
25
+ nn.ReLU(),
26
+ nn.BatchNorm2d(out_channels),
27
+ nn.MaxPool2d(2),
28
+ )
29
+
30
+ class RoadSignClassifierModel(nn.Module):
31
+ def __init__(self, num_classes=len(id2int)):
32
+ super().__init__()
33
+ self.model = nn.Sequential(
34
+ conv_func(3, 64),
35
+ conv_func(64, 64),
36
+ conv_func(64, 128),
37
+ conv_func(128, 256),
38
+ nn.Flatten(),
39
+ nn.Linear(256 * 2 * 2, 256),
40
+ nn.Dropout(0.2),
41
+ nn.ReLU(),
42
+ nn.Linear(256, num_classes),
43
+ )
44
+
45
+ def forward(self, x):
46
+ return self.model(x)
47
+
48
+ # ----------------------------
49
+ # Load trained model
50
+ # ----------------------------
51
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
52
+ model = RoadSignClassifierModel()
53
+ model.load_state_dict(torch.load("traffic_sign_model.pth", map_location=device))
54
+ model = model.to(device)
55
+ model.eval()
56
+
57
+ # ----------------------------
58
+ # Preprocessing
59
+ # ----------------------------
60
+ val_tf = T.Compose([
61
+ T.ToPILImage(),
62
+ T.Resize(32),
63
+ T.CenterCrop(32),
64
+ T.ToTensor(),
65
+ T.Normalize(mean=[0.485, 0.456, 0.406],
66
+ std=[0.229, 0.224, 0.225])
67
+ ])
68
+
69
+ # ----------------------------
70
+ # Prediction Function
71
+ # ----------------------------
72
+ def predict(img):
73
+ # Convert from Gradio (PIL.Image) to OpenCV
74
+ img = np.array(img.convert("RGB"))
75
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
76
+ img_input = val_tf(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)).unsqueeze(0).to(device)
77
+
78
+ with torch.no_grad():
79
+ output = model(img_input)
80
+ pred_class = torch.argmax(output, dim=1).item()
81
+
82
+ return {class_ids[int2id[pred_class]]: 1.0}
83
+
84
+ # ----------------------------
85
+ # Gradio UI
86
+ # ----------------------------
87
+ demo = gr.Interface(
88
+ fn=predict,
89
+ inputs=gr.Image(type="pil"),
90
+ outputs=gr.Label(num_top_classes=1),
91
+ title="🚦 Traffic Sign Classifier",
92
+ description="Upload a traffic sign image and the model will predict its category."
93
+ )
94
+
95
+ if __name__ == "__main__":
96
+ demo.launch()