HarshitJoshi commited on
Commit
951117d
·
verified ·
1 Parent(s): 8b74f10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -25
app.py CHANGED
@@ -2,32 +2,24 @@ import gradio as gr
2
  import cv2
3
  import requests
4
  import os
5
-
6
  from ultralytics import YOLO
 
7
  file_urls = [
8
- 'https://www.dropbox.com/scl/fi/kqd1z6wby1212c6ndodb3/Pol_20_jpg.rf.133c835b66958a7d48c12deeda31a719.jpg?rlkey=uqgvs2cwvahnmju15fv1zgorg&st=snv2yvtk&dl=0',
9
- 'https://www.dropbox.com/scl/fi/39aakapeh2y5ztk94rsyu/11e-a347-3f2d_jpg.rf.c66e5aeb57ee2ed660fdf0162156127d.jpg?rlkey=xoi3iw45vksgiejycau2ha7fh&st=etiawigv&dl=0',
10
- 'https://www.dropbox.com/scl/fi/8f08ehy53vsemw164g8n7/Recording2024-06-26184319.mp4?rlkey=pnmov906ttodl0cm92rpvc5ta&st=2twc9pjn&dl=0'
11
  ]
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
 
32
  model = YOLO('modelbest.pt')
33
  path = [['image_0.jpg'], ['image_1.jpg']]
@@ -47,12 +39,12 @@ def show_preds_image(image_path):
47
  lineType=cv2.LINE_AA
48
  )
49
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
50
-
51
  inputs_image = [
52
- gr.components.Image(type="filepath", label="Input Image"),
53
  ]
54
  outputs_image = [
55
- gr.components.Image(type="numpy", label="Output Image"),
56
  ]
57
 
58
  interface_image = gr.Interface(
@@ -64,10 +56,9 @@ interface_image = gr.Interface(
64
  cache_examples=False,
65
  )
66
 
67
-
68
  def show_preds_video(video_path):
69
  cap = cv2.VideoCapture(video_path)
70
- while(cap.isOpened()):
71
  ret, frame = cap.read()
72
  if ret:
73
  frame_copy = frame.copy()
@@ -83,13 +74,12 @@ def show_preds_video(video_path):
83
  lineType=cv2.LINE_AA
84
  )
85
  yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
86
-
87
  inputs_video = [
88
- gr.components.Video(type="filepath", label="Input Video"),
89
-
90
  ]
91
  outputs_video = [
92
- gr.components.Image(type="numpy", label="Output Image"),
93
  ]
94
  interface_video = gr.Interface(
95
  fn=show_preds_video,
@@ -103,4 +93,111 @@ interface_video = gr.Interface(
103
  gr.TabbedInterface(
104
  [interface_image, interface_video],
105
  tab_names=['Image inference', 'Video inference']
106
- ).queue().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import cv2
3
  import requests
4
  import os
 
5
  from ultralytics import YOLO
6
+
7
  file_urls = [
8
+ # 'https://www.dropbox.com/s/b5g97xo901zb3ds/pothole_example.jpg?dl=1',
9
+ # 'https://www.dropbox.com/s/86uxlxxlm1iaexa/pothole_screenshot.png?dl=1',
10
+ # 'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
11
  ]
12
 
 
13
  def download_file(url, save_name):
 
14
  if not os.path.exists(save_name):
15
  file = requests.get(url)
16
  open(save_name, 'wb').write(file.content)
17
+
18
  for i, url in enumerate(file_urls):
19
  if 'mp4' in file_urls[i]:
20
+ download_file(file_urls[i], f"video.mp4")
 
 
 
21
  else:
22
+ download_file(file_urls[i], f"image_{i}.jpg")
 
 
 
23
 
24
  model = YOLO('modelbest.pt')
25
  path = [['image_0.jpg'], ['image_1.jpg']]
 
39
  lineType=cv2.LINE_AA
40
  )
41
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
42
+
43
  inputs_image = [
44
+ gr.Image(type="filepath", label="Input Image"),
45
  ]
46
  outputs_image = [
47
+ gr.Image(type="numpy", label="Output Image"),
48
  ]
49
 
50
  interface_image = gr.Interface(
 
56
  cache_examples=False,
57
  )
58
 
 
59
  def show_preds_video(video_path):
60
  cap = cv2.VideoCapture(video_path)
61
+ while cap.isOpened():
62
  ret, frame = cap.read()
63
  if ret:
64
  frame_copy = frame.copy()
 
74
  lineType=cv2.LINE_AA
75
  )
76
  yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
77
+
78
  inputs_video = [
79
+ gr.Video(format="mp4", label="Input Video"),
 
80
  ]
81
  outputs_video = [
82
+ gr.Image(type="numpy", label="Output Image"),
83
  ]
84
  interface_video = gr.Interface(
85
  fn=show_preds_video,
 
93
  gr.TabbedInterface(
94
  [interface_image, interface_video],
95
  tab_names=['Image inference', 'Video inference']
96
+ ).queue().launch()
97
+
98
+ # import gradio as gr
99
+ # import cv2
100
+ # import requests
101
+ # import os
102
+
103
+ # from ultralytics import YOLO
104
+ # file_urls = [
105
+ # 'https://www.dropbox.com/scl/fi/kqd1z6wby1212c6ndodb3/Pol_20_jpg.rf.133c835b66958a7d48c12deeda31a719.jpg?rlkey=uqgvs2cwvahnmju15fv1zgorg&st=snv2yvtk&dl=0',
106
+ # 'https://www.dropbox.com/scl/fi/39aakapeh2y5ztk94rsyu/11e-a347-3f2d_jpg.rf.c66e5aeb57ee2ed660fdf0162156127d.jpg?rlkey=xoi3iw45vksgiejycau2ha7fh&st=etiawigv&dl=0',
107
+ # 'https://www.dropbox.com/scl/fi/8f08ehy53vsemw164g8n7/Recording2024-06-26184319.mp4?rlkey=pnmov906ttodl0cm92rpvc5ta&st=2twc9pjn&dl=0'
108
+ # ]
109
+
110
+
111
+ # def download_file(url, save_name):
112
+ # url = url
113
+ # if not os.path.exists(save_name):
114
+ # file = requests.get(url)
115
+ # open(save_name, 'wb').write(file.content)
116
+
117
+ # for i, url in enumerate(file_urls):
118
+ # if 'mp4' in file_urls[i]:
119
+ # download_file(
120
+ # file_urls[i],
121
+ # f"video.mp4"
122
+ # )
123
+ # else:
124
+ # download_file(
125
+ # file_urls[i],
126
+ # f"image_{i}.jpg"
127
+ # )
128
+
129
+ # model = YOLO('modelbest.pt')
130
+ # path = [['image_0.jpg'], ['image_1.jpg']]
131
+ # video_path = [['video.mp4']]
132
+
133
+ # def show_preds_image(image_path):
134
+ # image = cv2.imread(image_path)
135
+ # outputs = model.predict(source=image_path)
136
+ # results = outputs[0].cpu().numpy()
137
+ # for i, det in enumerate(results.boxes.xyxy):
138
+ # cv2.rectangle(
139
+ # image,
140
+ # (int(det[0]), int(det[1])),
141
+ # (int(det[2]), int(det[3])),
142
+ # color=(0, 0, 255),
143
+ # thickness=2,
144
+ # lineType=cv2.LINE_AA
145
+ # )
146
+ # return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
147
+
148
+ # inputs_image = [
149
+ # gr.components.Image(type="filepath", label="Input Image"),
150
+ # ]
151
+ # outputs_image = [
152
+ # gr.components.Image(type="numpy", label="Output Image"),
153
+ # ]
154
+
155
+ # interface_image = gr.Interface(
156
+ # fn=show_preds_image,
157
+ # inputs=inputs_image,
158
+ # outputs=outputs_image,
159
+ # title="Pothole detector",
160
+ # examples=path,
161
+ # cache_examples=False,
162
+ # )
163
+
164
+
165
+ # def show_preds_video(video_path):
166
+ # cap = cv2.VideoCapture(video_path)
167
+ # while(cap.isOpened()):
168
+ # ret, frame = cap.read()
169
+ # if ret:
170
+ # frame_copy = frame.copy()
171
+ # outputs = model.predict(source=frame)
172
+ # results = outputs[0].cpu().numpy()
173
+ # for i, det in enumerate(results.boxes.xyxy):
174
+ # cv2.rectangle(
175
+ # frame_copy,
176
+ # (int(det[0]), int(det[1])),
177
+ # (int(det[2]), int(det[3])),
178
+ # color=(0, 0, 255),
179
+ # thickness=2,
180
+ # lineType=cv2.LINE_AA
181
+ # )
182
+ # yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
183
+
184
+ # inputs_video = [
185
+ # gr.components.Video(type="filepath", label="Input Video"),
186
+
187
+ # ]
188
+ # outputs_video = [
189
+ # gr.components.Image(type="numpy", label="Output Image"),
190
+ # ]
191
+ # interface_video = gr.Interface(
192
+ # fn=show_preds_video,
193
+ # inputs=inputs_video,
194
+ # outputs=outputs_video,
195
+ # title="Pothole detector",
196
+ # examples=video_path,
197
+ # cache_examples=False,
198
+ # )
199
+
200
+ # gr.TabbedInterface(
201
+ # [interface_image, interface_video],
202
+ # tab_names=['Image inference', 'Video inference']
203
+ # ).queue().launch()