Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import cv2
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
# Load YOLO model
|
| 7 |
model = YOLO("best.pt")
|
| 8 |
|
| 9 |
|
| 10 |
-
# -------------------------------
|
| 11 |
-
# IMAGE DETECTION
|
| 12 |
-
# -------------------------------
|
| 13 |
def detect_image(image):
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
results = model(image, conf=0.2)
|
| 16 |
|
| 17 |
-
result_img = results[0].plot()
|
| 18 |
-
|
| 19 |
-
return result_img
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# -------------------------------
|
| 23 |
-
# VIDEO DETECTION
|
| 24 |
-
# -------------------------------
|
| 25 |
def detect_video(video):
|
| 26 |
|
| 27 |
cap = cv2.VideoCapture(video)
|
|
@@ -34,23 +22,22 @@ def detect_video(video):
|
|
| 34 |
|
| 35 |
out = cv2.VideoWriter(
|
| 36 |
output_path,
|
| 37 |
-
cv2.VideoWriter_fourcc(*
|
| 38 |
fps,
|
| 39 |
(width, height)
|
| 40 |
)
|
| 41 |
|
| 42 |
while cap.isOpened():
|
| 43 |
-
|
| 44 |
ret, frame = cap.read()
|
| 45 |
|
| 46 |
if not ret:
|
| 47 |
break
|
| 48 |
|
| 49 |
-
results = model(frame, conf=0.
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
-
out.write(
|
| 54 |
|
| 55 |
cap.release()
|
| 56 |
out.release()
|
|
@@ -58,84 +45,125 @@ def detect_video(video):
|
|
| 58 |
return output_path
|
| 59 |
|
| 60 |
|
| 61 |
-
# ---------------------------
|
| 62 |
-
#
|
| 63 |
-
# ---------------------------
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"""
|
| 68 |
|
| 69 |
-
description = """
|
| 70 |
-
Upload an **image or driving video** to detect objects such as:
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
• Pedestrians
|
| 76 |
-
• Traffic Lights
|
| 77 |
|
| 78 |
-
|
| 79 |
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
with gr.Tab("📷 Image Detection"):
|
| 90 |
|
| 91 |
-
with gr.
|
| 92 |
|
| 93 |
input_img = gr.Image(
|
| 94 |
type="numpy",
|
| 95 |
-
label="Upload
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
output_img = gr.Image(
|
| 99 |
-
label="Detection Result"
|
| 100 |
)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
detect_btn.click(
|
| 105 |
-
fn=detect_image,
|
| 106 |
-
inputs=input_img,
|
| 107 |
-
outputs=output_img
|
| 108 |
-
)
|
| 109 |
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
|
| 120 |
-
|
| 121 |
-
fn=detect_video,
|
| 122 |
-
inputs=input_video,
|
| 123 |
-
outputs=output_video
|
| 124 |
-
)
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
"""
|
| 129 |
-
|
| 130 |
-
|
|
|
|
| 131 |
"""
|
| 132 |
-
|
| 133 |
|
| 134 |
|
| 135 |
-
|
| 136 |
-
demo.launch(
|
| 137 |
-
server_name="0.0.0.0",
|
| 138 |
-
server_port=7860,
|
| 139 |
-
theme=gr.themes.Soft(),
|
| 140 |
-
share = True
|
| 141 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import cv2
|
|
|
|
| 4 |
|
|
|
|
| 5 |
model = YOLO("best.pt")
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
def detect_image(image):
|
| 9 |
+
results = model(image, conf=0.5)
|
| 10 |
+
return results[0].plot()
|
| 11 |
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def detect_video(video):
|
| 14 |
|
| 15 |
cap = cv2.VideoCapture(video)
|
|
|
|
| 22 |
|
| 23 |
out = cv2.VideoWriter(
|
| 24 |
output_path,
|
| 25 |
+
cv2.VideoWriter_fourcc(*"mp4v"),
|
| 26 |
fps,
|
| 27 |
(width, height)
|
| 28 |
)
|
| 29 |
|
| 30 |
while cap.isOpened():
|
|
|
|
| 31 |
ret, frame = cap.read()
|
| 32 |
|
| 33 |
if not ret:
|
| 34 |
break
|
| 35 |
|
| 36 |
+
results = model(frame, conf=0.5)
|
| 37 |
|
| 38 |
+
annotated = results[0].plot()
|
| 39 |
|
| 40 |
+
out.write(annotated)
|
| 41 |
|
| 42 |
cap.release()
|
| 43 |
out.release()
|
|
|
|
| 45 |
return output_path
|
| 46 |
|
| 47 |
|
| 48 |
+
# ---------------------------
|
| 49 |
+
# CUSTOM CSS (your HTML style)
|
| 50 |
+
# ---------------------------
|
| 51 |
+
|
| 52 |
+
css = """
|
| 53 |
+
|
| 54 |
+
body{
|
| 55 |
+
background:linear-gradient(120deg,#f6f9fc,#e9f2ff);
|
| 56 |
+
font-family:Segoe UI;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
.main-container{
|
| 60 |
+
max-width:950px;
|
| 61 |
+
margin:auto;
|
| 62 |
+
background:white;
|
| 63 |
+
padding:35px;
|
| 64 |
+
border-radius:20px;
|
| 65 |
+
box-shadow:0px 10px 30px rgba(0,0,0,0.15);
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.header{
|
| 69 |
+
text-align:center;
|
| 70 |
+
font-size:34px;
|
| 71 |
+
font-weight:bold;
|
| 72 |
+
color:#2c3e50;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
.subheader{
|
| 76 |
+
text-align:center;
|
| 77 |
+
font-size:18px;
|
| 78 |
+
margin-bottom:20px;
|
| 79 |
+
color:#555;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.gr-button{
|
| 83 |
+
background:#3498db !important;
|
| 84 |
+
color:white !important;
|
| 85 |
+
border-radius:10px !important;
|
| 86 |
+
font-size:18px !important;
|
| 87 |
+
padding:12px 30px !important;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
.gr-button:hover{
|
| 91 |
+
background:#217dbb !important;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
footer{
|
| 95 |
+
text-align:center;
|
| 96 |
+
margin-top:20px;
|
| 97 |
+
color:#777;
|
| 98 |
+
font-size:14px;
|
| 99 |
+
}
|
| 100 |
"""
|
| 101 |
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# ---------------------------
|
| 104 |
+
# UI
|
| 105 |
+
# ---------------------------
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
with gr.Blocks(css=css) as demo:
|
| 108 |
|
| 109 |
+
with gr.Column(elem_classes="main-container"):
|
| 110 |
|
| 111 |
+
gr.Markdown(
|
| 112 |
+
"""
|
| 113 |
+
<div class='header'>
|
| 114 |
+
Kurshi's Project 🙋🏻♀️✨
|
| 115 |
+
</div>
|
| 116 |
|
| 117 |
+
<div class='header'>
|
| 118 |
+
Object Detection in Autonomous Vehicles 🚗
|
| 119 |
+
</div>
|
| 120 |
|
| 121 |
+
<div class='subheader'>
|
| 122 |
+
YOLO Object Detection
|
| 123 |
+
</div>
|
| 124 |
+
"""
|
| 125 |
+
)
|
| 126 |
|
| 127 |
+
with gr.Tabs():
|
|
|
|
| 128 |
|
| 129 |
+
with gr.Tab("Upload Image"):
|
| 130 |
|
| 131 |
input_img = gr.Image(
|
| 132 |
type="numpy",
|
| 133 |
+
label="Upload Image"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
+
detect_btn = gr.Button("Start Detection")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
output_img = gr.Image(label="Detection Result")
|
| 139 |
|
| 140 |
+
detect_btn.click(
|
| 141 |
+
detect_image,
|
| 142 |
+
inputs=input_img,
|
| 143 |
+
outputs=output_img
|
| 144 |
+
)
|
| 145 |
|
| 146 |
+
with gr.Tab("Upload Video"):
|
| 147 |
|
| 148 |
+
input_video = gr.Video(label="Upload Video")
|
| 149 |
|
| 150 |
+
video_btn = gr.Button("Start Detection")
|
| 151 |
|
| 152 |
+
output_video = gr.Video(label="Detection Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
video_btn.click(
|
| 155 |
+
detect_video,
|
| 156 |
+
inputs=input_video,
|
| 157 |
+
outputs=output_video
|
| 158 |
+
)
|
| 159 |
|
| 160 |
+
gr.Markdown(
|
| 161 |
"""
|
| 162 |
+
<footer>
|
| 163 |
+
YOLO | Self Driving Car Dataset | STET College
|
| 164 |
+
</footer>
|
| 165 |
"""
|
| 166 |
+
)
|
| 167 |
|
| 168 |
|
| 169 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share = True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|