-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
386 lines (279 loc) · 13 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# import cv2
# import numpy as np
# import streamlit as st
# from PIL import Image
# from ultralytics import YOLO
# from collections import deque
# import tempfile
# import os
# def plt_show(image, title=""):
# if len(image.shape) == 3:
# st.image(image, caption=title, use_column_width=True)
# def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# dim = None
# (h, w) = image.shape[:2]
# if width is None and height is None:
# return image
# if width is None:
# r = height / float(h)
# dim = (int(w * r), height)
# else:
# r = width / float(w)
# dim = (width, int(h * r))
# resized = cv2.resize(image, dim, interpolation=inter)
# return resized
# def process_image(image):
# st.image(image, caption="Original Image", use_column_width=True)
# resized_image = image_resize(image, width=275, height=180)
# plt_show(resized_image, title="Resized Image")
# gray_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
# cv2.imwrite('grayImg.jpg', gray_image)
# plt_show(gray_image, title="Grayscale Image")
# return resized_image, gray_image
# def detect_contours(image):
# ret, thresh = cv2.threshold(image, 127, 255, 0)
# contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# return contours
# def draw_and_display_contours(image, contours):
# image_with_contours = image.copy()
# cv2.drawContours(image_with_contours, contours, -1, (0, 250, 0), 1)
# plt_show(image_with_contours, title="Contours on Image")
# def additional_processing_and_display(image):
# blur = cv2.blur(image, (5, 5))
# plt_show(blur, title="Blurred Image")
# gblur = cv2.GaussianBlur(image, (5, 5), 0)
# plt_show(gblur, title="Gaussian Blurred Image")
# median = cv2.medianBlur(image, 5)
# plt_show(median, title="Median Blurred Image")
# kernel = np.ones((5, 5), np.uint8)
# erosion = cv2.erode(median, kernel, iterations=1)
# dilation = cv2.dilate(erosion, kernel, iterations=5)
# closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
# edges = cv2.Canny(dilation, 9, 220)
# plt_show(erosion, title="Erosion Image")
# plt_show(closing, title="Closing Image")
# plt_show(edges, title="Edges Image")
# def road_damage_assessment(uploaded_video):
# best_model = YOLO('model/best.pt')
# font = cv2.FONT_HERSHEY_SIMPLEX
# font_scale = 1
# text_position = (40, 80)
# font_color = (255, 255, 255)
# background_color = (0, 0, 255)
# damage_deque = deque(maxlen=20)
# # Save the uploaded video to a temporary file
# temp_video_path = os.path.join(tempfile.gettempdir(), "temp_video.mp4")
# with open(temp_video_path, "wb") as temp_video:
# temp_video.write(uploaded_video.read())
# cap = cv2.VideoCapture(temp_video_path)
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# out = cv2.VideoWriter('road_damage_assessment.avi', fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
# while cap.isOpened():
# ret, frame = cap.read()
# if ret:
# results = best_model.predict(source=frame, imgsz=640, conf=0.25)
# processed_frame = results[0].plot(boxes=False)
# percentage_damage = 0
# if results[0].masks is not None:
# total_area = 0
# masks = results[0].masks.data.cpu().numpy()
# image_area = frame.shape[0] * frame.shape[1]
# for mask in masks:
# binary_mask = (mask > 0).astype(np.uint8) * 255
# contour, _ = cv2.findContours(binary_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# total_area += cv2.contourArea(contour[0])
# percentage_damage = (total_area / image_area) * 100
# damage_deque.append(percentage_damage)
# smoothed_percentage_damage = sum(damage_deque) / len(damage_deque)
# cv2.line(processed_frame, (text_position[0], text_position[1] - 10),
# (text_position[0] + 350, text_position[1] - 10), background_color, 40)
# cv2.putText(processed_frame, f'Road Damage: {smoothed_percentage_damage:.2f}%', text_position, font, font_scale, font_color, 2, cv2.LINE_AA)
# out.write(processed_frame)
# cv2.imshow('Road Damage Assessment', processed_frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# else:
# break
# # Release resources and delete the temporary file
# cap.release()
# out.release()
# cv2.destroyAllWindows()
# os.remove(temp_video_path)
# def main():
# st.title("Image and Road Damage Assessment")
# # Image Section
# st.markdown("## Image Section")
# uploaded_file = st.file_uploader("Choose an image...", type="jpg")
# if uploaded_file is not None:
# image = Image.open(uploaded_file)
# original_image = np.array(image)
# st.image(original_image, caption="Uploaded Image", use_column_width=True)
# resized_image, gray_image = process_image(original_image)
# if gray_image is not None:
# contours = detect_contours(gray_image)
# if contours:
# draw_and_display_contours(resized_image, contours)
# st.success("Pothole Detected!")
# else:
# st.warning("No Pothole Detected!")
# additional_processing_and_display(gray_image)
# # Video Section
# st.markdown("---") # Separation between image and video sections
# st.markdown("## Video Section")
# uploaded_video = st.file_uploader("Choose a video...", type="mp4")
# if uploaded_video is not None:
# road_damage_assessment(uploaded_video)
# if __name__ == "__main__":
# main()
import cv2
import numpy as np
import streamlit as st
from PIL import Image
from ultralytics import YOLO
from collections import deque
import io
import tempfile
import os
from demand import load_data, show_demand_analysis
def plt_show(image, title=""):
if len(image.shape) == 3:
st.image(image, caption=title, use_column_width=True)
def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation=inter)
return resized
def process_image(image):
st.image(image, caption="Original Image", use_column_width=True)
resized_image = image_resize(image, width=275, height=180)
plt_show(resized_image, title="Resized Image")
gray_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
cv2.imwrite('grayImg.jpg', gray_image)
plt_show(gray_image, title="Grayscale Image")
return resized_image, gray_image
def detect_contours(image):
ret, thresh = cv2.threshold(image, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
return contours
def draw_and_display_contours(image, contours):
image_with_contours = image.copy()
cv2.drawContours(image_with_contours, contours, -1, (0, 250, 0), 1)
plt_show(image_with_contours, title="Contours on Image")
def additional_processing_and_display(image):
blur = cv2.blur(image, (5, 5))
plt_show(blur, title="Blurred Image")
gblur = cv2.GaussianBlur(image, (5, 5), 0)
plt_show(gblur, title="Gaussian Blurred Image")
median = cv2.medianBlur(image, 5)
plt_show(median, title="Median Blurred Image")
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(median, kernel, iterations=1)
dilation = cv2.dilate(erosion, kernel, iterations=5)
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
edges = cv2.Canny(dilation, 9, 220)
plt_show(erosion, title="Erosion Image")
plt_show(closing, title="Closing Image")
plt_show(edges, title="Edges Image")
def road_damage_assessment(uploaded_video):
import torch
# Set the device to CPU
device = torch.device('cpu')
# Load the YOLO model
best_model = YOLO('model/best.pt')
best_model.to(device)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
text_position = (40, 80)
font_color = (255, 255, 255)
background_color = (0, 0, 255)
damage_deque = deque(maxlen=20)
# Save the uploaded video to a temporary file
temp_video_path = os.path.join(tempfile.gettempdir(), "temp_video.mp4")
with open(temp_video_path, "wb") as temp_video:
temp_video.write(uploaded_video.read())
cap = cv2.VideoCapture(temp_video_path)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('road_damage_assessment.avi', fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
while cap.isOpened():
ret, frame = cap.read()
if ret:
results = best_model.predict(source=frame, imgsz=640, conf=0.25)
processed_frame = results[0].plot(boxes=False)
percentage_damage = 0
if results[0].masks is not None:
total_area = 0
masks = results[0].masks.data.cpu().numpy()
image_area = frame.shape[0] * frame.shape[1]
for mask in masks:
binary_mask = (mask > 0).astype(np.uint8) * 255
contour, _ = cv2.findContours(binary_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
total_area += cv2.contourArea(contour[0])
percentage_damage = (total_area / image_area) * 100
damage_deque.append(percentage_damage)
smoothed_percentage_damage = sum(damage_deque) / len(damage_deque)
cv2.line(processed_frame, (text_position[0], text_position[1] - 10),
(text_position[0] + 350, text_position[1] - 10), background_color, 40)
cv2.putText(processed_frame, f'Road Damage: {smoothed_percentage_damage:.2f}%', text_position, font, font_scale, font_color, 2, cv2.LINE_AA)
out.write(processed_frame)
cv2.imshow('Road Damage Assessment', processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release resources and delete the temporary file
cap.release()
out.release()
cv2.destroyAllWindows()
os.remove(temp_video_path)
def process_uploaded_video(uploaded_video):
temp_video_file = tempfile.NamedTemporaryFile(delete=False)
temp_video_file.write(uploaded_video.read())
temp_video_file_path = temp_video_file.name
temp_video_file.close()
road_damage_assessment(temp_video_file_path)
os.remove(temp_video_file_path)
def main():
st.set_page_config(page_title="Urban Mobility Solution", page_icon=":car:")
st.title("Image and Road Damage Assessment")
# Image Section
st.markdown("## Image Section")
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
if uploaded_file is not None:
image = Image.open(uploaded_file)
original_image = np.array(image)
st.image(original_image, caption="Uploaded Image", use_column_width=True)
resized_image, gray_image = process_image(original_image)
if gray_image is not None:
contours = detect_contours(gray_image)
if contours:
draw_and_display_contours(resized_image, contours)
st.success("Pothole Detected!")
else:
st.warning("No Pothole Detected!")
additional_processing_and_display(gray_image)
# Video Section
st.markdown("---") # Separation between image and video sections
st.markdown("## Video Section")
uploaded_video = st.file_uploader("Choose a video...", type="mp4")
st.markdown("---")
if uploaded_video is not None:
road_damage_assessment(uploaded_video)
st.sidebar.markdown("<span style='font-size:28px'>Urban Mobility Solution</span>", unsafe_allow_html=True)
st.sidebar.markdown("---") # Separation between image and demand sections
st.sidebar.markdown("## Demand Prediction")
# Sidebar menu for demand prediction navigation
demand_uploaded_file = st.sidebar.file_uploader("Upload CSV file for Demand Prediction", type=["csv"])
# Load data if file is uploaded
if demand_uploaded_file:
df, demand_model = load_data(demand_uploaded_file)
show_demand_analysis(df)
if __name__ == "__main__":
main()