Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,104 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import cv2
|
3 |
-
import requests
|
4 |
-
import os
|
5 |
-
|
6 |
-
from ultralytics import YOLO
|
7 |
-
|
8 |
-
file_urls = [
|
9 |
-
'https://www.dropbox.com/s/b5g97xo901zb3ds/pothole_example.jpg?dl=1',
|
10 |
-
'https://www.dropbox.com/s/86uxlxxlm1iaexa/pothole_screenshot.png?dl=1',
|
11 |
-
'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
|
12 |
-
]
|
13 |
-
|
14 |
-
def download_file(url, save_name):
|
15 |
-
url = url
|
16 |
-
if not os.path.exists(save_name):
|
17 |
-
file = requests.get(url)
|
18 |
-
open(save_name, 'wb').write(file.content)
|
19 |
-
|
20 |
-
for i, url in enumerate(file_urls):
|
21 |
-
if 'mp4' in file_urls[i]:
|
22 |
-
download_file(
|
23 |
-
file_urls[i],
|
24 |
-
f"video.mp4"
|
25 |
-
)
|
26 |
-
else:
|
27 |
-
download_file(
|
28 |
-
file_urls[i],
|
29 |
-
f"image_{i}.jpg"
|
30 |
-
)
|
31 |
-
model = YOLO('best.pt')
|
32 |
-
path = [['image_0.jpg'], ['image_1.jpg']]
|
33 |
-
video_path = [['video.mp4']]
|
34 |
-
|
35 |
-
def show_preds_image(image_path):
|
36 |
-
image = cv2.imread(image_path)
|
37 |
-
outputs = model.predict(source=image_path)
|
38 |
-
results = outputs[0].cpu().numpy()
|
39 |
-
for i, det in enumerate(results.boxes.xyxy):
|
40 |
-
cv2.rectangle(
|
41 |
-
image,
|
42 |
-
(int(det[0]), int(det[1])),
|
43 |
-
(int(det[2]), int(det[3])),
|
44 |
-
color=(0, 0, 255),
|
45 |
-
thickness=2,
|
46 |
-
lineType=cv2.LINE_AA
|
47 |
-
)
|
48 |
-
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
49 |
-
|
50 |
-
inputs_image = [
|
51 |
-
gr.components.Image(type="filepath", label="Input Image"),
|
52 |
-
]
|
53 |
-
outputs_image = [
|
54 |
-
gr.components.Image(type="numpy", label="Output Image"),
|
55 |
-
]
|
56 |
-
interface_image = gr.Interface(
|
57 |
-
fn=show_preds_image,
|
58 |
-
inputs=inputs_image,
|
59 |
-
outputs=outputs_image,
|
60 |
-
title="Pothole detector",
|
61 |
-
examples=path,
|
62 |
-
cache_examples=False,
|
63 |
-
)
|
64 |
-
|
65 |
-
def show_preds_video(video_path):
|
66 |
-
cap = cv2.VideoCapture(video_path)
|
67 |
-
while(cap.isOpened()):
|
68 |
-
ret, frame = cap.read()
|
69 |
-
if ret:
|
70 |
-
frame_copy = frame.copy()
|
71 |
-
outputs = model.predict(source=frame)
|
72 |
-
results = outputs[0].cpu().numpy()
|
73 |
-
for i, det in enumerate(results.boxes.xyxy):
|
74 |
-
cv2.rectangle(
|
75 |
-
frame_copy,
|
76 |
-
(int(det[0]), int(det[1])),
|
77 |
-
(int(det[2]), int(det[3])),
|
78 |
-
color=(0, 0, 255),
|
79 |
-
thickness=2,
|
80 |
-
lineType=cv2.LINE_AA
|
81 |
-
)
|
82 |
-
yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
|
83 |
-
|
84 |
-
inputs_video = [
|
85 |
-
gr.components.Video(type="filepath", label="Input Video"),
|
86 |
-
|
87 |
-
]
|
88 |
-
outputs_video = [
|
89 |
-
gr.components.Image(type="numpy", label="Output Image"),
|
90 |
-
]
|
91 |
-
interface_video = gr.Interface(
|
92 |
-
fn=show_preds_video,
|
93 |
-
inputs=inputs_video,
|
94 |
-
outputs=outputs_video,
|
95 |
-
title="Pothole detector",
|
96 |
-
examples=video_path,
|
97 |
-
cache_examples=False,
|
98 |
-
)
|
99 |
-
|
100 |
-
gr.TabbedInterface(
|
101 |
-
[interface_image, interface_video],
|
102 |
-
tab_names=['Image inference', 'Video inference']
|
103 |
-
).queue().launch()
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|