Sayali9141 commited on
Commit
845bf66
1 Parent(s): 34fe804

Upload traffic_object_detection.py

Browse files
Files changed (1) hide show
  1. traffic_object_detection.py +118 -0
traffic_object_detection.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """traffic_object_detection.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1B7DIM9ABIA6RRhA8tL_3rcxL9M1iIP7D
8
+ """
9
+
10
+ !pip install datasets
11
+
12
+ from datasets import load_dataset
13
+
14
+ dataset = load_dataset("Sayali9141/traffic_signal_images")
15
+
16
+ next(iter(dataset['train']))
17
+
18
+ import matplotlib.pyplot as plt
19
+ from IPython.display import display
20
+ from PIL import Image
21
+
22
+ """Trying out hugging face YOLO
23
+
24
+ """
25
+
26
+ from transformers import AutoFeatureExtractor
27
+
28
+ feature_extractor = AutoFeatureExtractor.from_pretrained("hustvl/yolos-small")
29
+
30
+ from transformers import YolosForObjectDetection
31
+
32
+ model = YolosForObjectDetection.from_pretrained("hustvl/yolos-small")
33
+
34
+ """This code shows how to get image from the url"""
35
+
36
+ device = 'cuda'
37
+
38
+ model = model.to(device)
39
+
40
+ from PIL import Image
41
+ import requests
42
+ import base64
43
+ from io import BytesIO
44
+ from time import time
45
+ import matplotlib.pyplot as plt
46
+ import torch
47
+ # colors for visualization
48
+ COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
49
+ [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
50
+
51
+ def plot_results(pil_img, prob, boxes):
52
+ count=0
53
+ plt.figure(figsize=(16,10))
54
+ plt.imshow(pil_img)
55
+ ax = plt.gca()
56
+ colors = COLORS * 100
57
+ for p, (xmin, ymin, xmax, ymax), c in zip(prob, boxes.tolist(), colors):
58
+ cl = p.argmax()
59
+ if model.config.id2label[cl.item()] in ['car', 'truck'] :
60
+ ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
61
+ fill=False, color=c, linewidth=3))
62
+ text = f'{model.config.id2label[cl.item()]}: {p[cl]:0.2f}'
63
+ ax.text(xmin, ymin, text, fontsize=15,
64
+ bbox=dict(facecolor='yellow', alpha=0.5))
65
+ count+=1
66
+ plt.axis('off')
67
+ plt.show()
68
+ # print(count)
69
+ return(count)
70
+
71
+ all_counts = []
72
+ for i in range (22000, 22005):
73
+ row = dataset['train'][i]
74
+ start= time()
75
+ pixel_values = feature_extractor(row['image_url'], return_tensors="pt").pixel_values
76
+ pixel_values = pixel_values.to(device)
77
+ # pixel_values.shape
78
+ with torch.no_grad():
79
+ outputs = model(pixel_values, output_attentions=True)
80
+ probas = outputs.logits.softmax(-1)[0, :, :-1]
81
+ keep = probas.max(-1).values > 0.8
82
+ target_sizes = torch.tensor(row['image_url'].size[::-1]).unsqueeze(0)
83
+ postprocessed_outputs = feature_extractor.post_process(outputs, target_sizes)
84
+ bboxes_scaled = postprocessed_outputs[0]['boxes']
85
+ plot_results(row['image_url'], probas[keep], bboxes_scaled[keep])
86
+ count = 0
87
+
88
+ for p, boxes in zip(probas[keep], bboxes_scaled[keep]):
89
+ cl = p.argmax()
90
+ if model.config.id2label[cl.item()] in ['car', 'truck']:
91
+ count += 1
92
+ all_counts.append(count)
93
+ print(time()-start)
94
+
95
+ # def select_columns(example):
96
+ # return {key: example[key] for key in ['timestamp', 'camera_id', 'latitude', 'longitude']}
97
+
98
+ # subset_dataset = dataset['train'].map(select_columns[dataset['train']])
99
+
100
+ # data_yolo= subset_dataset.to_pandas()
101
+
102
+ # data_yolo['box_count'][22000:22004]= [x for x in all_counts]
103
+
104
+ #create interactive map
105
+ #create interactive map using latitude and longitude of counts column
106
+ # import folium
107
+ # from folium import plugins
108
+
109
+ # # Create a map object and center it to the avarage coordinates to m
110
+ # m = folium.Map(location=[df['latitude'].mean(), df['longitude'].mean()], zoom_start=10)
111
+
112
+ # # Add marker for each row in the data
113
+ # for i in range(0,len(df)):
114
+ # folium.Marker([df.iloc[i]['latitude'], df.iloc[i]['longitude']], popup=df.iloc[i]['counts']).add_to(m)
115
+
116
+ # # Display the map
117
+ # m.save('map.html')
118
+ # m