Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,12 +6,21 @@ import tempfile
|
|
6 |
import time
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
|
9 |
-
|
10 |
def run_yolo(image):
|
11 |
# Run the model on the image and get results
|
12 |
results = model(image)
|
13 |
return results
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def process_results(results, image):
|
16 |
# Draw bounding boxes and labels on the image
|
17 |
boxes = results[0].boxes # Get boxes from results
|
@@ -22,13 +31,15 @@ def process_results(results, image):
|
|
22 |
cls = int(box.cls[0]) # Class index
|
23 |
label = model.names[cls] # Get class name from index
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
return image
|
30 |
|
31 |
-
import tempfile
|
32 |
|
33 |
def process_video(uploaded_file):
|
34 |
# Create a temporary file to save the uploaded video
|
@@ -101,19 +112,45 @@ def process_video(uploaded_file):
|
|
101 |
video_bytes = f.read()
|
102 |
st.download_button(label='Download Processed Video', data=video_bytes, file_name='processed_video.mp4', mime='video/mp4')
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
def main():
|
105 |
model_file = hf_hub_download(repo_id="TheKnight115/Yolov8m", filename="yolov8_Medium.pt")
|
106 |
|
107 |
global model
|
108 |
model = YOLO(model_file)
|
109 |
-
|
110 |
st.title("Motorbike Violation Detection")
|
111 |
|
112 |
-
#
|
113 |
-
|
114 |
|
115 |
-
|
116 |
-
|
|
|
|
|
117 |
# Process the image
|
118 |
image = np.array(cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1))
|
119 |
results = run_yolo(image)
|
@@ -124,10 +161,15 @@ def main():
|
|
124 |
# Display the processed image
|
125 |
st.image(processed_image, caption='Detected Image', use_column_width=True)
|
126 |
|
127 |
-
|
|
|
|
|
128 |
# Process the video
|
129 |
-
process_video(uploaded_file)
|
130 |
-
|
|
|
|
|
|
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
main()
|
|
|
6 |
import time
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
|
|
|
9 |
def run_yolo(image):
|
10 |
# Run the model on the image and get results
|
11 |
results = model(image)
|
12 |
return results
|
13 |
|
14 |
+
# Color definitions for each class
|
15 |
+
class_colors = {
|
16 |
+
0: (0, 255, 0), # Green (Helmet)
|
17 |
+
1: (255, 0, 0), # Blue (License Plate)
|
18 |
+
2: (0, 0, 255), # Red (MotorbikeDelivery)
|
19 |
+
3: (255, 255, 0), # Cyan (MotorbikeSport)
|
20 |
+
4: (255, 0, 255), # Magenta (No Helmet)
|
21 |
+
5: (0, 255, 255), # Yellow (Person)
|
22 |
+
}
|
23 |
+
|
24 |
def process_results(results, image):
|
25 |
# Draw bounding boxes and labels on the image
|
26 |
boxes = results[0].boxes # Get boxes from results
|
|
|
31 |
cls = int(box.cls[0]) # Class index
|
32 |
label = model.names[cls] # Get class name from index
|
33 |
|
34 |
+
# Get the color for the current class
|
35 |
+
color = class_colors.get(cls, (255, 255, 255)) # Default to white if class not found
|
36 |
+
|
37 |
+
# Draw rectangle and label on the image with the appropriate color
|
38 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2) # Draw bounding box
|
39 |
+
cv2.putText(image, f"{label} {conf:.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # Draw label
|
40 |
|
41 |
return image
|
42 |
|
|
|
43 |
|
44 |
def process_video(uploaded_file):
|
45 |
# Create a temporary file to save the uploaded video
|
|
|
112 |
video_bytes = f.read()
|
113 |
st.download_button(label='Download Processed Video', data=video_bytes, file_name='processed_video.mp4', mime='video/mp4')
|
114 |
|
115 |
+
def live_video_feed():
|
116 |
+
stframe = st.empty() # Placeholder for the video stream in Streamlit
|
117 |
+
video = cv2.VideoCapture(0) # Capture live video from the webcam
|
118 |
+
|
119 |
+
while True:
|
120 |
+
ret, frame = video.read()
|
121 |
+
if not ret:
|
122 |
+
break
|
123 |
+
|
124 |
+
# Run YOLO model on the current frame
|
125 |
+
results = run_yolo(frame)
|
126 |
+
|
127 |
+
# Process the results and draw boxes on the current frame
|
128 |
+
processed_frame = process_results(results, frame)
|
129 |
+
|
130 |
+
# Display the processed frame in the Streamlit app
|
131 |
+
stframe.image(processed_frame, channels="BGR", use_column_width=True)
|
132 |
+
|
133 |
+
# Stop the live feed when user clicks on the "Stop" button
|
134 |
+
if st.button("Stop"):
|
135 |
+
break
|
136 |
+
|
137 |
+
video.release()
|
138 |
+
|
139 |
def main():
|
140 |
model_file = hf_hub_download(repo_id="TheKnight115/Yolov8m", filename="yolov8_Medium.pt")
|
141 |
|
142 |
global model
|
143 |
model = YOLO(model_file)
|
144 |
+
|
145 |
st.title("Motorbike Violation Detection")
|
146 |
|
147 |
+
# Create a selection box for input type
|
148 |
+
input_type = st.selectbox("Select Input Type", ("Image", "Video", "Live Feed"))
|
149 |
|
150 |
+
# Image or video file uploader
|
151 |
+
if input_type == "Image":
|
152 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
153 |
+
if uploaded_file is not None:
|
154 |
# Process the image
|
155 |
image = np.array(cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1))
|
156 |
results = run_yolo(image)
|
|
|
161 |
# Display the processed image
|
162 |
st.image(processed_image, caption='Detected Image', use_column_width=True)
|
163 |
|
164 |
+
elif input_type == "Video":
|
165 |
+
uploaded_file = st.file_uploader("Choose a video...", type=["mp4", "mov"])
|
166 |
+
if uploaded_file is not None:
|
167 |
# Process the video
|
168 |
+
process_video(uploaded_file)
|
169 |
+
|
170 |
+
elif input_type == "Live Feed":
|
171 |
+
st.write("Live video feed from webcam. Press 'Stop' to stop the feed.")
|
172 |
+
live_video_feed()
|
173 |
|
174 |
if __name__ == "__main__":
|
175 |
main()
|