prithivMLmods commited on
Commit
fcf74cf
·
verified ·
1 Parent(s): 4853a1d

Create traffic_density.py

Browse files
Files changed (1) hide show
  1. traffic_density.py +43 -0
traffic_density.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ from transformers import AutoImageProcessor
4
+ from transformers import SiglipForImageClassification
5
+ from transformers.image_utils import load_image
6
+ from PIL import Image
7
+ import torch
8
+
9
+ # Load model and processor
10
+ model_name = "prithivMLmods/Traffic-Density-Classification"
11
+ model = SiglipForImageClassification.from_pretrained(model_name)
12
+ processor = AutoImageProcessor.from_pretrained(model_name)
13
+
14
+ @spaces.GPU
15
+ def traffic_density_classification(image):
16
+ """Predicts traffic density category for an image."""
17
+ image = Image.fromarray(image).convert("RGB")
18
+ inputs = processor(images=image, return_tensors="pt")
19
+
20
+ with torch.no_grad():
21
+ outputs = model(**inputs)
22
+ logits = outputs.logits
23
+ probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
24
+
25
+ labels = {
26
+ "0": "high-traffic", "1": "low-traffic", "2": "medium-traffic", "3": "no-traffic"
27
+ }
28
+ predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
29
+
30
+ return predictions
31
+
32
+ # Create Gradio interface
33
+ iface = gr.Interface(
34
+ fn=traffic_density_classification,
35
+ inputs=gr.Image(type="numpy"),
36
+ outputs=gr.Label(label="Prediction Scores"),
37
+ title="Traffic Density Classification",
38
+ description="Upload an image to classify it into one of the 4 traffic density categories."
39
+ )
40
+
41
+ # Launch the app
42
+ if __name__ == "__main__":
43
+ iface.launch()