Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -170,7 +170,7 @@ def draw_angled_line(image, line_params, color=(0, 255, 0), thickness=2):
|
|
| 170 |
_, _, start_point, end_point = line_params
|
| 171 |
cv2.line(image, start_point, end_point, color, thickness)
|
| 172 |
|
| 173 |
-
def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None):
|
| 174 |
"""
|
| 175 |
Processes the IP camera stream to count objects of the selected classes crossing the line.
|
| 176 |
"""
|
|
@@ -202,6 +202,7 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
| 202 |
frame_queue = deque(maxlen=10)
|
| 203 |
|
| 204 |
logger.info("Starting to process the stream...")
|
|
|
|
| 205 |
while cap.isOpened():
|
| 206 |
ret, frame = cap.read()
|
| 207 |
if not ret:
|
|
@@ -258,6 +259,15 @@ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=No
|
|
| 258 |
# Yield the annotated frame to Gradio
|
| 259 |
yield annotated_frame, ""
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
cap.release()
|
| 262 |
logger.info("Stream processing completed.")
|
| 263 |
|
|
@@ -291,6 +301,10 @@ with gr.Blocks() as demo:
|
|
| 291 |
gr.Markdown("### Step 3: Adjust Confidence Threshold (Optional)")
|
| 292 |
confidence_threshold = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, label="Confidence Threshold")
|
| 293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
# Process the stream
|
| 295 |
process_button = gr.Button("Process Stream")
|
| 296 |
|
|
@@ -301,7 +315,7 @@ with gr.Blocks() as demo:
|
|
| 301 |
error_box = gr.Textbox(label="Errors/Warnings", interactive=False)
|
| 302 |
|
| 303 |
# Event listener for processing the video
|
| 304 |
-
process_button.click(process_video, inputs=[confidence_threshold, selected_classes, stream_url], outputs=[output_image, error_box])
|
| 305 |
|
| 306 |
# Launch the interface
|
| 307 |
demo.launch(debug=True)
|
|
|
|
| 170 |
_, _, start_point, end_point = line_params
|
| 171 |
cv2.line(image, start_point, end_point, color, thickness)
|
| 172 |
|
| 173 |
+
def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None, target_fps=30):
|
| 174 |
"""
|
| 175 |
Processes the IP camera stream to count objects of the selected classes crossing the line.
|
| 176 |
"""
|
|
|
|
| 202 |
frame_queue = deque(maxlen=10)
|
| 203 |
|
| 204 |
logger.info("Starting to process the stream...")
|
| 205 |
+
last_time = time.time()
|
| 206 |
while cap.isOpened():
|
| 207 |
ret, frame = cap.read()
|
| 208 |
if not ret:
|
|
|
|
| 259 |
# Yield the annotated frame to Gradio
|
| 260 |
yield annotated_frame, ""
|
| 261 |
|
| 262 |
+
# Calculate the time taken to process the frame
|
| 263 |
+
current_time = time.time()
|
| 264 |
+
elapsed_time = current_time - last_time
|
| 265 |
+
last_time = current_time
|
| 266 |
+
|
| 267 |
+
# Calculate the time to sleep to maintain the target FPS
|
| 268 |
+
sleep_time = max(0, (1.0 / target_fps) - elapsed_time)
|
| 269 |
+
time.sleep(sleep_time)
|
| 270 |
+
|
| 271 |
cap.release()
|
| 272 |
logger.info("Stream processing completed.")
|
| 273 |
|
|
|
|
| 301 |
gr.Markdown("### Step 3: Adjust Confidence Threshold (Optional)")
|
| 302 |
confidence_threshold = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, label="Confidence Threshold")
|
| 303 |
|
| 304 |
+
# Step 4: Set target FPS
|
| 305 |
+
gr.Markdown("### Step 4: Set Target FPS (Optional)")
|
| 306 |
+
target_fps = gr.Slider(minimum=1, maximum=60, value=30, label="Target FPS")
|
| 307 |
+
|
| 308 |
# Process the stream
|
| 309 |
process_button = gr.Button("Process Stream")
|
| 310 |
|
|
|
|
| 315 |
error_box = gr.Textbox(label="Errors/Warnings", interactive=False)
|
| 316 |
|
| 317 |
# Event listener for processing the video
|
| 318 |
+
process_button.click(process_video, inputs=[confidence_threshold, selected_classes, stream_url, target_fps], outputs=[output_image, error_box])
|
| 319 |
|
| 320 |
# Launch the interface
|
| 321 |
demo.launch(debug=True)
|