-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
278 lines (238 loc) · 9.38 KB
/
Copy pathhelper.py
File metadata and controls
278 lines (238 loc) · 9.38 KB
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
274
275
276
277
278
from ultralytics import YOLO
import traceback
import streamlit as st
import cv2
import settings
import tempfile
import os
#import winsound
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
def load_model(model_path):
"""
Loads a YOLO object detection model from the specified model_path.
Parameters:
model_path (str): The path to the YOLO model file.
Returns:
A YOLO object detection model.
"""
model = YOLO(model_path)
return model
def display_tracker_options():
display_tracker = st.sidebar.radio("Display Tracker", ('Yes', 'No'))
is_display_tracker = True if display_tracker == 'Yes' else False
if is_display_tracker:
tracker_type = st.sidebar.radio("Tracker", ("bytetrack.yaml", "botsort.yaml"))
return is_display_tracker, tracker_type
return is_display_tracker, None
def plot(conf, model, image, res, st_plot):
try:
for result in res:
boxes = result.boxes.xywh
# Extract center coordinates
center_x = boxes[:, 0]
center_y = boxes[:, 1]
pil_image = Image.fromarray(image) # Convert numpy array to PIL Image
width, height = pil_image.size
# Plotting
ids = [f'{i+1}' for i in range(len(center_x))]
fig, ax = plt.subplots(figsize=(width / 100, height / 100)) # Convert pixel to inches for figsize
# Plot center coordinates
ax.scatter(center_x, center_y, color='red', label='People')
# Annotate each point with its ID
for i, (x, y) in enumerate(zip(center_x, center_y)):
ax.annotate(ids[i], (x, y), textcoords="offset points", xytext=(0,10), ha='center')
# Add labels and title
ax.set_title('Positions of Workers')
ax.set_xlabel('X Coordinate')
ax.set_ylabel('Y Coordinate')
# Set plot limits to match the image dimensions
ax.set_xlim(0, width)
ax.set_ylim(height, 0)
# Show plot
plt.legend()
plt.grid(True)
ax.set_aspect('equal', adjustable='box')
st_plot.pyplot(fig)
except Exception as e:
# Display the error message with traceback
st.error(f"An error occurred: {e}")
st.text_area("Traceback", traceback.format_exc())
def _display_detected_frames(conf, model, st_frame, image, plot_person, st_plot, is_display_tracking=None, tracker=None):
"""
Display the detected objects on a video frame using the YOLOv8 model.
Args:
- conf (float): Confidence threshold for object detection.
- model (YoloV8): A YOLOv8 object detection model.
- st_frame (Streamlit object): A Streamlit object to display the detected video.
- image (numpy array): A numpy array representing the video frame.
- is_display_tracking (bool): A flag indicating whether to display object tracking (default=None).
Returns:
None
"""
# Display object tracking, if specified
if is_display_tracking:
res = model.track(image, conf=conf, persist=True, tracker=tracker)
else:
# Predict the objects in the image using the YOLOv8 model
res = model.predict(image, conf=conf)
#alert
"""for result in res:
if any(value in result.boxes.cls for value in [3.0, 4.0, 5.0, 6.0]):
winsound.Beep(500, 500)
"""
# # Plot the detected objects on the video frame
res_plotted = res[0].plot()
st_frame.image(res_plotted,
caption='Detected Video',
channels="BGR",
use_container_width=True
)
if plot_person == 1:
plot(conf, model, res_plotted, res, st_plot)
def play_webcam(conf, model, plot_person):
"""
Plays a webcam stream. Detects Objects in real-time using the YOLOv8 object detection model.
Parameters:
conf: Confidence of YOLOv8 model.
model: An instance of the `YOLOv8` class containing the YOLOv8 model.
Returns:
None
Raises:
None
"""
source_webcam = settings.WEBCAM_PATH
is_display_tracker, tracker = display_tracker_options()
if st.sidebar.button('Detect Objects'):
try:
vid_cap = cv2.VideoCapture(source_webcam)
col1, col2 = st.columns(2)
if plot_person == 1:
with col1:
st_frame = st.empty()
with col2:
st_plot= st.empty()
else:
st_frame = st.empty()
st_plot= st.empty()
while (vid_cap.isOpened()):
success, image = vid_cap.read()
if success:
_display_detected_frames(conf,
model,
st_frame,
image,
plot_person,
st_plot,
is_display_tracker,
tracker
)
else:
vid_cap.release()
break
except Exception as e:
st.sidebar.error("Error loading video: " + str(e))
def play_rtsp_stream(conf, model, plot_person):
"""
Plays an rtsp stream. Detects Objects in real-time using the YOLOv8 object detection model.
Parameters:
conf: Confidence of YOLOv8 model.
model: An instance of the `YOLOv8` class containing the YOLOv8 model.
Returns:
None
Raises:
None
"""
source_rtsp = st.sidebar.text_input("rtsp stream url")
is_display_tracker, tracker = display_tracker_options()
if st.sidebar.button('Detect Objects'):
try:
vid_cap = cv2.VideoCapture(source_rtsp)
frame_count = 0
col1, col2 = st.columns(2)
if plot_person == 1:
with col1:
st_frame = st.empty()
with col2:
st_plot= st.empty()
else:
st_frame = st.empty()
st_plot= st.empty()
while (vid_cap.isOpened()):
success, image = vid_cap.read()
frame_count += 1
if success:
if frame_count % 3 != 0:
continue
_display_detected_frames(conf,
model,
st_frame,
image,
plot_person,
st_plot,
is_display_tracker,
tracker
)
else:
vid_cap.release()
break
except Exception as e:
st.sidebar.error("Error loading RTSP stream: " + str(e))
def play_stored_video(conf, model):
"""
Plays a stored video file. Tracks and detects objects in real-time using the YOLOv8 object detection model.
Parameters:
conf: Confidence of YOLOv8 model.
model: An instance of the `YOLOv8` class containing the YOLOv8 model.
Returns:
None
Raises:
None
"""
is_display_tracker, tracker = display_tracker_options()
source_vid = st.sidebar.file_uploader(
"Choose a video...", type=("mp4", "avi", "mov"))
if source_vid is not None:
st.video(source_vid)
temp_dir = tempfile.mkdtemp()
path = os.path.join(temp_dir, source_vid.name)
with open(path, "wb") as f:
f.write(source_vid.getvalue())
st.write("Uploaded Video:", source_vid.name)
else:
st.warning("Please upload a video file.")
if st.sidebar.button('Detect Video Objects'):
try:
cap = cv2.VideoCapture(path)
st_frame = st.empty()
frame_count = 0
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
frame_count += 1
if success:
if frame_count % 3 != 0:
continue
# Run YOLOv8 inference on the frame
if is_display_tracker:
results = model.track(frame, conf=conf, persist=True, tracker=tracker)
else:
# Predict the objects in the image using the YOLOv8 model
results = model.predict(frame, conf=conf)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Display the annotated frame
st_frame.image(annotated_frame,
caption='Detected Video',
channels="BGR",
use_container_width=True
)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
except Exception as e:
st.sidebar.error("Error loading video: " + str(e))