Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -180,24 +180,30 @@ def draw_angled_line(image, line_params, color=(0, 255, 0), thickness=2):
|
|
180 |
_, _, start_point, end_point = line_params
|
181 |
cv2.line(image, start_point, end_point, color, thickness)
|
182 |
|
|
|
183 |
def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None):
|
184 |
"""
|
185 |
-
Processes the IP camera stream
|
186 |
"""
|
187 |
global line_params
|
188 |
|
189 |
-
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
|
|
|
192 |
cap = cv2.VideoCapture(stream_url)
|
193 |
if not cap.isOpened():
|
194 |
-
|
195 |
-
|
196 |
-
# Initialize variables
|
197 |
-
frames_buffer = []
|
198 |
-
crossed_objects = {}
|
199 |
-
batch_size = 16
|
200 |
-
max_tracked_objects = 1000
|
201 |
|
202 |
# Set capture properties for better performance
|
203 |
cap.set(cv2.CAP_PROP_BUFFERSIZE, 30)
|
@@ -205,58 +211,112 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
205 |
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
|
206 |
|
207 |
model = YOLO(model="yolo11n.pt")
|
|
|
|
|
|
|
|
|
208 |
|
|
|
209 |
while cap.isOpened():
|
210 |
ret, frame = cap.read()
|
211 |
if not ret:
|
|
|
212 |
break
|
213 |
|
214 |
frames_buffer.append(frame)
|
215 |
|
216 |
if len(frames_buffer) >= batch_size:
|
217 |
# Process batch of frames
|
218 |
-
results = model.track(frames_buffer, persist=True, conf=confidence_threshold
|
219 |
|
220 |
-
# Process each frame
|
221 |
-
for
|
222 |
if result.boxes.id is not None:
|
223 |
track_ids = result.boxes.id.int().cpu().tolist()
|
224 |
clss = result.boxes.cls.cpu().tolist()
|
225 |
boxes = result.boxes.xyxy.cpu()
|
226 |
confs = result.boxes.conf.cpu().tolist()
|
227 |
|
228 |
-
# Create annotated frame
|
229 |
-
annotated_frame = frames_buffer[frame_idx].copy()
|
230 |
-
|
231 |
for box, cls, t_id, conf in zip(boxes, clss, track_ids, confs):
|
232 |
if conf >= confidence_threshold and model.names[cls] in selected_classes:
|
233 |
-
# Check line crossing
|
234 |
if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
|
235 |
crossed_objects[t_id] = True
|
236 |
|
237 |
-
# Clear if too many objects
|
238 |
if len(crossed_objects) > max_tracked_objects:
|
239 |
crossed_objects.clear()
|
240 |
|
241 |
-
|
242 |
-
|
243 |
-
cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
244 |
|
245 |
-
|
246 |
-
|
247 |
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
252 |
|
253 |
-
|
254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
-
# Clear buffer after processing batch
|
257 |
frames_buffer = []
|
258 |
|
259 |
cap.release()
|
|
|
|
|
260 |
# Define the Gradio interface
|
261 |
with gr.Blocks() as demo:
|
262 |
gr.Markdown("<h1>Real-time monitoring, object tracking, and line-crossing detection for CCTV camera streams.</h1></center>")
|
|
|
180 |
_, _, start_point, end_point = line_params
|
181 |
cv2.line(image, start_point, end_point, color, thickness)
|
182 |
|
183 |
+
|
184 |
def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None):
|
185 |
"""
|
186 |
+
Processes the IP camera stream to count objects of the selected classes crossing the line.
|
187 |
"""
|
188 |
global line_params
|
189 |
|
190 |
+
errors = []
|
191 |
+
|
192 |
+
if line_params is None:
|
193 |
+
errors.append("Error: No line drawn. Please draw a line on the first frame.")
|
194 |
+
if selected_classes is None or len(selected_classes) == 0:
|
195 |
+
errors.append("Error: No classes selected. Please select at least one class to detect.")
|
196 |
+
if stream_url is None or stream_url.strip() == "":
|
197 |
+
errors.append("Error: No stream URL provided.")
|
198 |
+
|
199 |
+
if errors:
|
200 |
+
return None, "\n".join(errors)
|
201 |
|
202 |
+
logger.info("Connecting to the IP camera stream...")
|
203 |
cap = cv2.VideoCapture(stream_url)
|
204 |
if not cap.isOpened():
|
205 |
+
errors.append("Error: Could not open stream.")
|
206 |
+
return None, "\n".join(errors)
|
|
|
|
|
|
|
|
|
|
|
207 |
|
208 |
# Set capture properties for better performance
|
209 |
cap.set(cv2.CAP_PROP_BUFFERSIZE, 30)
|
|
|
211 |
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
|
212 |
|
213 |
model = YOLO(model="yolo11n.pt")
|
214 |
+
crossed_objects = {}
|
215 |
+
max_tracked_objects = 1000
|
216 |
+
frames_buffer = []
|
217 |
+
batch_size = 16
|
218 |
|
219 |
+
logger.info("Starting to process the stream...")
|
220 |
while cap.isOpened():
|
221 |
ret, frame = cap.read()
|
222 |
if not ret:
|
223 |
+
errors.append("Error: Could not read frame from the stream.")
|
224 |
break
|
225 |
|
226 |
frames_buffer.append(frame)
|
227 |
|
228 |
if len(frames_buffer) >= batch_size:
|
229 |
# Process batch of frames
|
230 |
+
results = model.track(frames_buffer, persist=True, conf=confidence_threshold)
|
231 |
|
232 |
+
# Process and yield each frame immediately to maintain real-time appearance
|
233 |
+
for idx, result in enumerate(results):
|
234 |
if result.boxes.id is not None:
|
235 |
track_ids = result.boxes.id.int().cpu().tolist()
|
236 |
clss = result.boxes.cls.cpu().tolist()
|
237 |
boxes = result.boxes.xyxy.cpu()
|
238 |
confs = result.boxes.conf.cpu().tolist()
|
239 |
|
|
|
|
|
|
|
240 |
for box, cls, t_id, conf in zip(boxes, clss, track_ids, confs):
|
241 |
if conf >= confidence_threshold and model.names[cls] in selected_classes:
|
|
|
242 |
if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
|
243 |
crossed_objects[t_id] = True
|
244 |
|
|
|
245 |
if len(crossed_objects) > max_tracked_objects:
|
246 |
crossed_objects.clear()
|
247 |
|
248 |
+
# Visualize the results with bounding boxes, masks, and IDs
|
249 |
+
annotated_frame = result.plot()
|
|
|
250 |
|
251 |
+
# Draw the angled line on the frame
|
252 |
+
draw_angled_line(annotated_frame, line_params, color=(0, 255, 0), thickness=2)
|
253 |
|
254 |
+
# Display the count on the frame with a modern look
|
255 |
+
count = len(crossed_objects)
|
256 |
+
(text_width, text_height), _ = cv2.getTextSize(f"COUNT: {count}", cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
|
|
257 |
|
258 |
+
# Calculate the position for the middle of the top
|
259 |
+
margin = 10
|
260 |
+
x = (annotated_frame.shape[1] - text_width) // 2
|
261 |
+
y = text_height + margin
|
262 |
+
|
263 |
+
# Draw the black background rectangle
|
264 |
+
cv2.rectangle(annotated_frame,
|
265 |
+
(x - margin, y - text_height - margin),
|
266 |
+
(x + text_width + margin, y + margin),
|
267 |
+
(0, 0, 0), -1)
|
268 |
+
|
269 |
+
# Draw the text
|
270 |
+
cv2.putText(annotated_frame, f"COUNT: {count}",
|
271 |
+
(x, y), cv2.FONT_HERSHEY_SIMPLEX,
|
272 |
+
1, (0, 255, 0), 2)
|
273 |
+
|
274 |
+
# Yield each frame as soon as it's processed
|
275 |
+
yield annotated_frame, ""
|
276 |
+
|
277 |
+
# Clear the buffer after processing
|
278 |
+
frames_buffer = []
|
279 |
+
|
280 |
+
# If we have remaining frames that don't make a full batch, process them too
|
281 |
+
elif frames_buffer:
|
282 |
+
results = model.track(frames_buffer, persist=True, conf=confidence_threshold)
|
283 |
+
|
284 |
+
for result in results:
|
285 |
+
if result.boxes.id is not None:
|
286 |
+
track_ids = result.boxes.id.int().cpu().tolist()
|
287 |
+
clss = result.boxes.cls.cpu().tolist()
|
288 |
+
boxes = result.boxes.xyxy.cpu()
|
289 |
+
confs = result.boxes.conf.cpu().tolist()
|
290 |
+
|
291 |
+
for box, cls, t_id, conf in zip(boxes, clss, track_ids, confs):
|
292 |
+
if conf >= confidence_threshold and model.names[cls] in selected_classes:
|
293 |
+
if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
|
294 |
+
crossed_objects[t_id] = True
|
295 |
+
|
296 |
+
annotated_frame = result.plot()
|
297 |
+
draw_angled_line(annotated_frame, line_params, color=(0, 255, 0), thickness=2)
|
298 |
+
|
299 |
+
count = len(crossed_objects)
|
300 |
+
(text_width, text_height), _ = cv2.getTextSize(f"COUNT: {count}", cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
|
301 |
+
margin = 10
|
302 |
+
x = (annotated_frame.shape[1] - text_width) // 2
|
303 |
+
y = text_height + margin
|
304 |
+
|
305 |
+
cv2.rectangle(annotated_frame,
|
306 |
+
(x - margin, y - text_height - margin),
|
307 |
+
(x + text_width + margin, y + margin),
|
308 |
+
(0, 0, 0), -1)
|
309 |
+
cv2.putText(annotated_frame, f"COUNT: {count}",
|
310 |
+
(x, y), cv2.FONT_HERSHEY_SIMPLEX,
|
311 |
+
1, (0, 255, 0), 2)
|
312 |
+
|
313 |
+
yield annotated_frame, ""
|
314 |
|
|
|
315 |
frames_buffer = []
|
316 |
|
317 |
cap.release()
|
318 |
+
logger.info("Stream processing completed.")
|
319 |
+
|
320 |
# Define the Gradio interface
|
321 |
with gr.Blocks() as demo:
|
322 |
gr.Markdown("<h1>Real-time monitoring, object tracking, and line-crossing detection for CCTV camera streams.</h1></center>")
|