Spaces:
Sleeping
Sleeping
File size: 8,828 Bytes
11e0e08 951117d 11e0e08 3c5007c 11e0e08 8b74f10 11e0e08 951117d 11e0e08 951117d 11e0e08 951117d 11e0e08 0d64151 edc687b 0d64151 edc687b 0d64151 11e0e08 3c5007c 11e0e08 3c5007c 0d64151 3c5007c 0d64151 3c5007c 11e0e08 951117d 0d64151 3c5007c 11e0e08 951117d 11e0e08 951117d 11e0e08 3c5007c 11e0e08 edc687b 11e0e08 edc687b 951117d 11e0e08 edc687b 951117d edc687b 0d64151 edc687b 11e0e08 951117d 11e0e08 edc687b 11e0e08 951117d edc687b 951117d |
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 |
import gradio as gr
import cv2
import requests
import os
from ultralytics import YOLO
file_urls = [
'https://www.dropbox.com/scl/fi/kqd1z6wby1212c6ndodb3/Pol_20_jpg.rf.133c835b66958a7d48c12deeda31a719.jpg?rlkey=uqgvs2cwvahnmju15fv1zgorg&st=snv2yvtk&dl=0',
'https://www.dropbox.com/scl/fi/39aakapeh2y5ztk94rsyu/11e-a347-3f2d_jpg.rf.c66e5aeb57ee2ed660fdf0162156127d.jpg?rlkey=xoi3iw45vksgiejycau2ha7fh&st=etiawigv&dl=0',
'https://www.dropbox.com/scl/fi/8f08ehy53vsemw164g8n7/Recording2024-06-26184319.mp4?rlkey=pnmov906ttodl0cm92rpvc5ta&st=2twc9pjn&dl=0'
]
def download_file(url, save_name):
if not os.path.exists(save_name):
file = requests.get(url)
open(save_name, 'wb').write(file.content)
for i, url in enumerate(file_urls):
if 'mp4' in file_urls[i]:
download_file(file_urls[i], f"video.mp4")
else:
download_file(file_urls[i], f"image_{i}.jpg")
colors = {
0: (255, 0, 0), # Red for class 0
1: (0, 128, 0), # Green (dark) for class 1
2: (0, 0, 255), # Blue for class 2
3: (255, 255, 0), # Yellow for class 3
4: (255, 0, 255), # Magenta for class 4
5: (0, 255, 255), # Cyan for class 5
6: (128, 0, 0), # Maroon for class 6
7: (0, 225, 0), # Green for class 7
}
model = YOLO('modelbest.pt')
path = [['image_0.jpg'], ['image_1.jpg']]
video_path = [['video.mp4']]
def show_preds_image(image_path):
image = cv2.imread(image_path)
outputs = model.predict(source=image_path)
results = outputs[0].cpu().numpy()
for i, det in enumerate(results.boxes.xyxy):
class_id = int(results.boxes.cls[i])
label = model.names[class_id]
# Get the bounding box coordinates
x1, y1, x2, y2 = int(det[0]), int(det[1]), int(det[2]), int(det[3])
# Draw the bounding box with the specified color
color = colors.get(class_id, (0, 0, 255))
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2, cv2.LINE_AA)
# Calculate text size and position
label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 2)
text_x = x1 + (x2 - x1) // 2 - label_size[0] // 2
text_y = y1 + (y2 - y1) // 2 + label_size[1] // 2
# Draw the label text
cv2.putText(image, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2, cv2.LINE_AA)
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# def show_preds_image(image_path):
# image = cv2.imread(image_path)
# outputs = model.predict(source=image_path)
# results = outputs[0].cpu().numpy()
# for i, det in enumerate(results.boxes.xyxy):
# cv2.rectangle(
# image,
# (int(det[0]), int(det[1])),
# (int(det[2]), int(det[3])),
# color=(0, 0, 255),
# thickness=2,
# lineType=cv2.LINE_AA
# )
# return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
inputs_image = [
gr.Image(type="filepath", label="Input Image"),
]
outputs_image = [
gr.Image(type="numpy", label="Output Image"),
]
interface_image = gr.Interface(
fn=show_preds_image,
inputs=inputs_image,
outputs=outputs_image,
title="Smoke Detection on Indian Roads",
examples=path,
cache_examples=False,
)
def show_preds_video(video_path):
# Open the input video
cap = cv2.VideoCapture(video_path)
# Get video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
# Define the codec and create a VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 'mp4v' for .mp4 format
out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (width, height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_copy = frame.copy()
outputs = model.predict(source=frame)
results = outputs[0].cpu().numpy()
for i, det in enumerate(results.boxes.xyxy):
class_id = int(results.boxes.cls[i])
label = model.names[class_id]
# Get the bounding box coordinates
x1, y1, x2, y2 = int(det[0]), int(det[1]), int(det[2]), int(det[3])
# Draw the bounding box with the specified color
color = colors.get(class_id, (0, 0, 255))
cv2.rectangle(frame_copy, (x1, y1), (x2, y2), color, 2, cv2.LINE_AA)
# Calculate text size and position
label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 2)
text_x = x1 + (x2 - x1) // 2 - label_size[0] // 2
text_y = y1 + (y2 - y1) // 2 + label_size[1] // 2
# Draw the label text
cv2.putText(frame_copy, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2, cv2.LINE_AA)
# Write the frame to the output video
out.write(frame_copy)
# Release everything
cap.release()
out.release()
return 'output_video.mp4'
# Updated Gradio interface
inputs_video = [
gr.Video(format="mp4", label="Input Video"),
]
outputs_video = [
gr.Video(label="Output Video"),
]
interface_video = gr.Interface(
fn=show_preds_video,
inputs=inputs_video,
outputs=outputs_video,
title="Pothole detector",
examples=video_path,
cache_examples=False,
)
gr.TabbedInterface(
[interface_image, interface_video],
tab_names=['Image inference', 'Video inference']
).queue().launch()
# import gradio as gr
# import cv2
# import requests
# import os
# from ultralytics import YOLO
# file_urls = [
# 'https://www.dropbox.com/scl/fi/kqd1z6wby1212c6ndodb3/Pol_20_jpg.rf.133c835b66958a7d48c12deeda31a719.jpg?rlkey=uqgvs2cwvahnmju15fv1zgorg&st=snv2yvtk&dl=0',
# 'https://www.dropbox.com/scl/fi/39aakapeh2y5ztk94rsyu/11e-a347-3f2d_jpg.rf.c66e5aeb57ee2ed660fdf0162156127d.jpg?rlkey=xoi3iw45vksgiejycau2ha7fh&st=etiawigv&dl=0',
# 'https://www.dropbox.com/scl/fi/8f08ehy53vsemw164g8n7/Recording2024-06-26184319.mp4?rlkey=pnmov906ttodl0cm92rpvc5ta&st=2twc9pjn&dl=0'
# ]
# def download_file(url, save_name):
# url = url
# if not os.path.exists(save_name):
# file = requests.get(url)
# open(save_name, 'wb').write(file.content)
# for i, url in enumerate(file_urls):
# if 'mp4' in file_urls[i]:
# download_file(
# file_urls[i],
# f"video.mp4"
# )
# else:
# download_file(
# file_urls[i],
# f"image_{i}.jpg"
# )
# model = YOLO('modelbest.pt')
# path = [['image_0.jpg'], ['image_1.jpg']]
# video_path = [['video.mp4']]
# def show_preds_image(image_path):
# image = cv2.imread(image_path)
# outputs = model.predict(source=image_path)
# results = outputs[0].cpu().numpy()
# for i, det in enumerate(results.boxes.xyxy):
# cv2.rectangle(
# image,
# (int(det[0]), int(det[1])),
# (int(det[2]), int(det[3])),
# color=(0, 0, 255),
# thickness=2,
# lineType=cv2.LINE_AA
# )
# return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# inputs_image = [
# gr.components.Image(type="filepath", label="Input Image"),
# ]
# outputs_image = [
# gr.components.Image(type="numpy", label="Output Image"),
# ]
# interface_image = gr.Interface(
# fn=show_preds_image,
# inputs=inputs_image,
# outputs=outputs_image,
# title="Pothole detector",
# examples=path,
# cache_examples=False,
# )
# def show_preds_video(video_path):
# cap = cv2.VideoCapture(video_path)
# while(cap.isOpened()):
# ret, frame = cap.read()
# if ret:
# frame_copy = frame.copy()
# outputs = model.predict(source=frame)
# results = outputs[0].cpu().numpy()
# for i, det in enumerate(results.boxes.xyxy):
# cv2.rectangle(
# frame_copy,
# (int(det[0]), int(det[1])),
# (int(det[2]), int(det[3])),
# color=(0, 0, 255),
# thickness=2,
# lineType=cv2.LINE_AA
# )
# yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
# inputs_video = [
# gr.components.Video(type="filepath", label="Input Video"),
# ]
# outputs_video = [
# gr.components.Image(type="numpy", label="Output Image"),
# ]
# interface_video = gr.Interface(
# fn=show_preds_video,
# inputs=inputs_video,
# outputs=outputs_video,
# title="Pothole detector",
# examples=video_path,
# cache_examples=False,
# )
# gr.TabbedInterface(
# [interface_image, interface_video],
# tab_names=['Image inference', 'Video inference']
# ).queue().launch() |