Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,4 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
#Maximize CPU usage
|
4 |
import multiprocessing
|
5 |
import cv2
|
6 |
|
@@ -178,7 +176,7 @@ def draw_angled_line(image, line_params, color=(0, 255, 0), thickness=2):
|
|
178 |
_, _, start_point, end_point = line_params
|
179 |
cv2.line(image, start_point, end_point, color, thickness)
|
180 |
|
181 |
-
def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None):
|
182 |
"""
|
183 |
Processes the IP camera stream to count objects of the selected classes crossing the line.
|
184 |
"""
|
@@ -213,8 +211,14 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
213 |
errors.append("Error: Could not read frame from the stream.")
|
214 |
break
|
215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
# Perform object tracking with confidence threshold
|
217 |
-
results = model.track(
|
218 |
|
219 |
if results[0].boxes.id is not None:
|
220 |
track_ids = results[0].boxes.id.int().cpu().tolist()
|
@@ -224,6 +228,8 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
224 |
|
225 |
for box, cls, t_id, conf in zip(boxes, clss, track_ids, confs):
|
226 |
if conf >= confidence_threshold and model.names[cls] in selected_classes:
|
|
|
|
|
227 |
# Check if the object crosses the line
|
228 |
if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
|
229 |
crossed_objects[t_id] = True
|
@@ -232,8 +238,8 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
232 |
if len(crossed_objects) > max_tracked_objects:
|
233 |
crossed_objects.clear()
|
234 |
|
235 |
-
# Visualize the results with bounding boxes, masks, and IDs
|
236 |
-
annotated_frame = results[0].plot()
|
237 |
|
238 |
# Draw the angled line on the frame
|
239 |
draw_angled_line(annotated_frame, line_params, color=(0, 255, 0), thickness=2)
|
@@ -264,11 +270,7 @@ with gr.Blocks() as demo:
|
|
264 |
gr.Markdown("<h1>Real-time monitoring, object tracking, and line-crossing detection for CCTV camera streams.</h1></center>")
|
265 |
gr.Markdown("## https://github.com/SanshruthR/CCTV_SENTRY_YOLO11")
|
266 |
|
267 |
-
|
268 |
-
|
269 |
# Step 1: Enter the IP Camera Stream URL
|
270 |
-
# gr.Markdown("### Step 0: Enter the IP Camera Stream URL")
|
271 |
-
# stream_url = gr.Textbox(label="Enter IP Camera Stream URL", value="https://s103.ipcamlive.com/streams/67n4ojknye7lkxpmf/stream.m3u8", visible=False)
|
272 |
stream_url = gr.Textbox(label="Enter IP Camera Stream URL", value="https://s104.ipcamlive.com/streams/68idokwtondsqpmkr/stream.m3u8", visible=False)
|
273 |
|
274 |
# Step 1: Extract the first frame from the stream
|
@@ -280,15 +282,9 @@ with gr.Blocks() as demo:
|
|
280 |
# Image component for displaying the first frame
|
281 |
image = gr.Image(value=first_frame, label="First Frame of Stream", type="pil")
|
282 |
|
283 |
-
|
284 |
line_info = gr.Textbox(label="Line Coordinates", value="Line Coordinates:\nStart: None, End: None")
|
285 |
image.select(update_line, inputs=image, outputs=[image, line_info])
|
286 |
|
287 |
-
# Reset the line (optional)
|
288 |
-
# gr.Markdown("### Step 4: Reset the Line (Optional)")
|
289 |
-
# reset_button = gr.Button("Reset Line")
|
290 |
-
# reset_button.click(reset_line, inputs=None, outputs=[image, line_info])
|
291 |
-
|
292 |
# Step 2: Select classes to detect
|
293 |
gr.Markdown("### Step 2: Select Classes to Detect")
|
294 |
model = YOLO(model="yolo11n.pt") # Load the model to get class names
|
@@ -299,7 +295,11 @@ with gr.Blocks() as demo:
|
|
299 |
gr.Markdown("### Step 3: Adjust Confidence Threshold (Optional)")
|
300 |
confidence_threshold = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, label="Confidence Threshold")
|
301 |
|
302 |
-
#
|
|
|
|
|
|
|
|
|
303 |
process_button = gr.Button("Process Stream")
|
304 |
|
305 |
# Output image for real-time frame rendering
|
@@ -309,7 +309,7 @@ with gr.Blocks() as demo:
|
|
309 |
error_box = gr.Textbox(label="Errors/Warnings", interactive=False)
|
310 |
|
311 |
# Event listener for processing the video
|
312 |
-
process_button.click(process_video, inputs=[confidence_threshold, selected_classes, stream_url], outputs=[output_image, error_box])
|
313 |
|
314 |
# Launch the interface
|
315 |
-
demo.launch(debug=True)
|
|
|
1 |
+
# Maximize CPU usage
|
|
|
|
|
2 |
import multiprocessing
|
3 |
import cv2
|
4 |
|
|
|
176 |
_, _, start_point, end_point = line_params
|
177 |
cv2.line(image, start_point, end_point, color, thickness)
|
178 |
|
179 |
+
def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None, resolution_scale=1.0):
|
180 |
"""
|
181 |
Processes the IP camera stream to count objects of the selected classes crossing the line.
|
182 |
"""
|
|
|
211 |
errors.append("Error: Could not read frame from the stream.")
|
212 |
break
|
213 |
|
214 |
+
# Resize the frame for processing
|
215 |
+
height, width = frame.shape[:2]
|
216 |
+
new_width = int(width * resolution_scale)
|
217 |
+
new_height = int(height * resolution_scale)
|
218 |
+
resized_frame = cv2.resize(frame, (new_width, new_height))
|
219 |
+
|
220 |
# Perform object tracking with confidence threshold
|
221 |
+
results = model.track(resized_frame, persist=True, conf=confidence_threshold)
|
222 |
|
223 |
if results[0].boxes.id is not None:
|
224 |
track_ids = results[0].boxes.id.int().cpu().tolist()
|
|
|
228 |
|
229 |
for box, cls, t_id, conf in zip(boxes, clss, track_ids, confs):
|
230 |
if conf >= confidence_threshold and model.names[cls] in selected_classes:
|
231 |
+
# Scale the bounding box back to the original resolution
|
232 |
+
box = box * (width / new_width)
|
233 |
# Check if the object crosses the line
|
234 |
if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
|
235 |
crossed_objects[t_id] = True
|
|
|
238 |
if len(crossed_objects) > max_tracked_objects:
|
239 |
crossed_objects.clear()
|
240 |
|
241 |
+
# Visualize the results with bounding boxes, masks, and IDs on the original frame
|
242 |
+
annotated_frame = results[0].plot(img=frame)
|
243 |
|
244 |
# Draw the angled line on the frame
|
245 |
draw_angled_line(annotated_frame, line_params, color=(0, 255, 0), thickness=2)
|
|
|
270 |
gr.Markdown("<h1>Real-time monitoring, object tracking, and line-crossing detection for CCTV camera streams.</h1></center>")
|
271 |
gr.Markdown("## https://github.com/SanshruthR/CCTV_SENTRY_YOLO11")
|
272 |
|
|
|
|
|
273 |
# Step 1: Enter the IP Camera Stream URL
|
|
|
|
|
274 |
stream_url = gr.Textbox(label="Enter IP Camera Stream URL", value="https://s104.ipcamlive.com/streams/68idokwtondsqpmkr/stream.m3u8", visible=False)
|
275 |
|
276 |
# Step 1: Extract the first frame from the stream
|
|
|
282 |
# Image component for displaying the first frame
|
283 |
image = gr.Image(value=first_frame, label="First Frame of Stream", type="pil")
|
284 |
|
|
|
285 |
line_info = gr.Textbox(label="Line Coordinates", value="Line Coordinates:\nStart: None, End: None")
|
286 |
image.select(update_line, inputs=image, outputs=[image, line_info])
|
287 |
|
|
|
|
|
|
|
|
|
|
|
288 |
# Step 2: Select classes to detect
|
289 |
gr.Markdown("### Step 2: Select Classes to Detect")
|
290 |
model = YOLO(model="yolo11n.pt") # Load the model to get class names
|
|
|
295 |
gr.Markdown("### Step 3: Adjust Confidence Threshold (Optional)")
|
296 |
confidence_threshold = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, label="Confidence Threshold")
|
297 |
|
298 |
+
# Step 4: Adjust resolution scale
|
299 |
+
gr.Markdown("### Step 4: Adjust Resolution Scale (Optional)")
|
300 |
+
resolution_scale = gr.Slider(minimum=0.1, maximum=1.0, value=1.0, label="Resolution Scale")
|
301 |
+
|
302 |
+
# Process the stream
|
303 |
process_button = gr.Button("Process Stream")
|
304 |
|
305 |
# Output image for real-time frame rendering
|
|
|
309 |
error_box = gr.Textbox(label="Errors/Warnings", interactive=False)
|
310 |
|
311 |
# Event listener for processing the video
|
312 |
+
process_button.click(process_video, inputs=[confidence_threshold, selected_classes, stream_url, resolution_scale], outputs=[output_image, error_box])
|
313 |
|
314 |
# Launch the interface
|
315 |
+
demo.launch(debug=True)
|