Spaces:
Runtime error
Runtime error
fouadmahmoud283-ai commited on
Commit ·
8856829
1
Parent(s): 27f8936
fixing camera n1235
Browse files- requirements.txt +3 -1
- src/streamlit_app.py +65 -31
requirements.txt
CHANGED
|
@@ -14,4 +14,6 @@ ultralytics
|
|
| 14 |
av
|
| 15 |
aiortc
|
| 16 |
requests
|
| 17 |
-
tqdm
|
|
|
|
|
|
|
|
|
| 14 |
av
|
| 15 |
aiortc
|
| 16 |
requests
|
| 17 |
+
tqdm
|
| 18 |
+
imageio
|
| 19 |
+
imageio-ffmpeg
|
src/streamlit_app.py
CHANGED
|
@@ -16,6 +16,7 @@ import warnings
|
|
| 16 |
import requests
|
| 17 |
import threading
|
| 18 |
import av
|
|
|
|
| 19 |
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase
|
| 20 |
|
| 21 |
# Suppress WebRTC/asyncio warnings and errors
|
|
@@ -327,6 +328,27 @@ def process_video(video_path, model, conf_threshold, frame_skip=1):
|
|
| 327 |
|
| 328 |
return rendered_frame
|
| 329 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
try:
|
| 331 |
container = av.open(output_path, mode="w")
|
| 332 |
try:
|
|
@@ -344,22 +366,7 @@ def process_video(video_path, model, conf_threshold, frame_skip=1):
|
|
| 344 |
stream.height = height
|
| 345 |
stream.pix_fmt = "yuv420p"
|
| 346 |
|
| 347 |
-
|
| 348 |
-
while True:
|
| 349 |
-
ret, frame = cap.read()
|
| 350 |
-
if not ret:
|
| 351 |
-
break
|
| 352 |
-
|
| 353 |
-
frame_index += 1
|
| 354 |
-
if frame_skip > 1 and frame_index % frame_skip != 0:
|
| 355 |
-
continue
|
| 356 |
-
|
| 357 |
-
rendered_frame = _process_frame(frame)
|
| 358 |
-
|
| 359 |
-
video_frame = av.VideoFrame.from_ndarray(rendered_frame, format="bgr24")
|
| 360 |
-
for packet in stream.encode(video_frame):
|
| 361 |
-
container.mux(packet)
|
| 362 |
-
|
| 363 |
for packet in stream.encode():
|
| 364 |
container.mux(packet)
|
| 365 |
except Exception as e:
|
|
@@ -369,24 +376,51 @@ def process_video(video_path, model, conf_threshold, frame_skip=1):
|
|
| 369 |
container.close()
|
| 370 |
|
| 371 |
if error_message:
|
|
|
|
| 372 |
try:
|
| 373 |
-
|
| 374 |
-
writer =
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
-
|
| 388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
writer.release()
|
| 391 |
error_message = None
|
| 392 |
codec_name = "mp4v"
|
|
|
|
| 16 |
import requests
|
| 17 |
import threading
|
| 18 |
import av
|
| 19 |
+
import imageio.v2 as imageio
|
| 20 |
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase
|
| 21 |
|
| 22 |
# Suppress WebRTC/asyncio warnings and errors
|
|
|
|
| 328 |
|
| 329 |
return rendered_frame
|
| 330 |
|
| 331 |
+
def _reset_stats():
|
| 332 |
+
nonlocal processed_frames, detection_counts
|
| 333 |
+
processed_frames = 0
|
| 334 |
+
detection_counts = {}
|
| 335 |
+
progress.progress(0)
|
| 336 |
+
|
| 337 |
+
def _run_loop(write_frame):
|
| 338 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
| 339 |
+
frame_index = 0
|
| 340 |
+
while True:
|
| 341 |
+
ret, frame = cap.read()
|
| 342 |
+
if not ret:
|
| 343 |
+
break
|
| 344 |
+
|
| 345 |
+
frame_index += 1
|
| 346 |
+
if frame_skip > 1 and frame_index % frame_skip != 0:
|
| 347 |
+
continue
|
| 348 |
+
|
| 349 |
+
rendered_frame = _process_frame(frame)
|
| 350 |
+
write_frame(rendered_frame)
|
| 351 |
+
|
| 352 |
try:
|
| 353 |
container = av.open(output_path, mode="w")
|
| 354 |
try:
|
|
|
|
| 366 |
stream.height = height
|
| 367 |
stream.pix_fmt = "yuv420p"
|
| 368 |
|
| 369 |
+
_run_loop(lambda rendered_frame: [container.mux(p) for p in stream.encode(av.VideoFrame.from_ndarray(rendered_frame, format="bgr24"))])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
for packet in stream.encode():
|
| 371 |
container.mux(packet)
|
| 372 |
except Exception as e:
|
|
|
|
| 376 |
container.close()
|
| 377 |
|
| 378 |
if error_message:
|
| 379 |
+
_reset_stats()
|
| 380 |
try:
|
| 381 |
+
imageio_path = output_path.replace(".webm", ".mp4")
|
| 382 |
+
writer = imageio.get_writer(
|
| 383 |
+
imageio_path,
|
| 384 |
+
fps=fps,
|
| 385 |
+
codec="libx264",
|
| 386 |
+
ffmpeg_params=["-pix_fmt", "yuv420p"],
|
| 387 |
+
format="FFMPEG"
|
| 388 |
+
)
|
| 389 |
+
_run_loop(lambda rendered_frame: writer.append_data(cv2.cvtColor(rendered_frame, cv2.COLOR_BGR2RGB)))
|
| 390 |
+
writer.close()
|
| 391 |
+
output_path = imageio_path
|
| 392 |
+
codec_name = "libx264"
|
| 393 |
+
output_mime = "video/mp4"
|
| 394 |
+
error_message = None
|
| 395 |
+
except Exception as e:
|
| 396 |
+
error_message = f"ImageIO H.264 encode failed: {e}"
|
| 397 |
|
| 398 |
+
if error_message:
|
| 399 |
+
_reset_stats()
|
| 400 |
+
try:
|
| 401 |
+
imageio_path = output_path.replace(".mp4", ".webm")
|
| 402 |
+
writer = imageio.get_writer(
|
| 403 |
+
imageio_path,
|
| 404 |
+
fps=fps,
|
| 405 |
+
codec="libvpx",
|
| 406 |
+
ffmpeg_params=["-pix_fmt", "yuv420p"],
|
| 407 |
+
format="FFMPEG"
|
| 408 |
+
)
|
| 409 |
+
_run_loop(lambda rendered_frame: writer.append_data(cv2.cvtColor(rendered_frame, cv2.COLOR_BGR2RGB)))
|
| 410 |
+
writer.close()
|
| 411 |
+
output_path = imageio_path
|
| 412 |
+
codec_name = "libvpx"
|
| 413 |
+
output_mime = "video/webm"
|
| 414 |
+
error_message = None
|
| 415 |
+
except Exception as e:
|
| 416 |
+
error_message = f"ImageIO WebM encode failed: {e}"
|
| 417 |
|
| 418 |
+
if error_message:
|
| 419 |
+
_reset_stats()
|
| 420 |
+
try:
|
| 421 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 422 |
+
writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 423 |
+
_run_loop(lambda rendered_frame: writer.write(rendered_frame))
|
| 424 |
writer.release()
|
| 425 |
error_message = None
|
| 426 |
codec_name = "mp4v"
|