fouadmahmoud283-ai commited on
Commit
ecf5f79
·
1 Parent(s): dfe1c5b

fixing camera n1235

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +61 -22
src/streamlit_app.py CHANGED
@@ -300,6 +300,30 @@ def process_video(video_path, model, conf_threshold, frame_skip=1):
300
  container = None
301
  stream = None
302
  codec_name = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  try:
304
  container = av.open(output_path, mode="w")
305
  try:
@@ -322,43 +346,54 @@ def process_video(video_path, model, conf_threshold, frame_skip=1):
322
  if frame_skip > 1 and frame_index % frame_skip != 0:
323
  continue
324
 
325
- img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
326
- model.conf = conf_threshold
327
- results = model(img_rgb)
328
-
329
- detections = results.pandas().xyxy[0]
330
- relevant = detections[detections['name'].isin(WHEELCHAIR_CLASSES.values())]
331
-
332
- for name in relevant['name'].tolist():
333
- detection_counts[name] = detection_counts.get(name, 0) + 1
334
-
335
- rendered_frame = results.render()[0]
336
- if rendered_frame.shape[1] != width or rendered_frame.shape[0] != height:
337
- rendered_frame = cv2.resize(rendered_frame, (width, height))
338
 
339
  video_frame = av.VideoFrame.from_ndarray(rendered_frame, format="bgr24")
340
  for packet in stream.encode(video_frame):
341
  container.mux(packet)
342
 
343
- processed_frames += 1
344
- if total_frames > 0:
345
- progress.progress(min(processed_frames / total_frames, 1.0))
346
-
347
  for packet in stream.encode():
348
  container.mux(packet)
349
- except Exception:
350
- output_path = None
351
  finally:
352
- progress.empty()
353
- cap.release()
354
  if container is not None:
355
  container.close()
356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
  stats = {
358
  "total_frames": total_frames,
359
  "processed_frames": processed_frames,
360
  "detection_counts": detection_counts,
361
- "codec": codec_name
 
362
  }
363
 
364
  return output_path, stats
@@ -606,6 +641,10 @@ def main():
606
  stats = st.session_state.video_stats
607
  st.metric("🎞️ Total Frames", stats.get("total_frames", 0))
608
  st.metric("✅ Processed Frames", stats.get("processed_frames", 0))
 
 
 
 
609
 
610
  if stats.get("detection_counts"):
611
  counts_df = pd.DataFrame(
 
300
  container = None
301
  stream = None
302
  codec_name = None
303
+ error_message = None
304
+
305
+ def _process_frame(frame_bgr):
306
+ nonlocal processed_frames
307
+ img_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
308
+ model.conf = conf_threshold
309
+ results = model(img_rgb)
310
+
311
+ detections = results.pandas().xyxy[0]
312
+ relevant = detections[detections['name'].isin(WHEELCHAIR_CLASSES.values())]
313
+
314
+ for name in relevant['name'].tolist():
315
+ detection_counts[name] = detection_counts.get(name, 0) + 1
316
+
317
+ rendered_frame = results.render()[0]
318
+ if rendered_frame.shape[1] != width or rendered_frame.shape[0] != height:
319
+ rendered_frame = cv2.resize(rendered_frame, (width, height))
320
+
321
+ processed_frames += 1
322
+ if total_frames > 0:
323
+ progress.progress(min(processed_frames / total_frames, 1.0))
324
+
325
+ return rendered_frame
326
+
327
  try:
328
  container = av.open(output_path, mode="w")
329
  try:
 
346
  if frame_skip > 1 and frame_index % frame_skip != 0:
347
  continue
348
 
349
+ rendered_frame = _process_frame(frame)
 
 
 
 
 
 
 
 
 
 
 
 
350
 
351
  video_frame = av.VideoFrame.from_ndarray(rendered_frame, format="bgr24")
352
  for packet in stream.encode(video_frame):
353
  container.mux(packet)
354
 
 
 
 
 
355
  for packet in stream.encode():
356
  container.mux(packet)
357
+ except Exception as e:
358
+ error_message = f"PyAV encode failed: {e}"
359
  finally:
 
 
360
  if container is not None:
361
  container.close()
362
 
363
+ if error_message:
364
+ try:
365
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
366
+ writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
367
+
368
+ cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
369
+ frame_index = 0
370
+ while True:
371
+ ret, frame = cap.read()
372
+ if not ret:
373
+ break
374
+
375
+ frame_index += 1
376
+ if frame_skip > 1 and frame_index % frame_skip != 0:
377
+ continue
378
+
379
+ rendered_frame = _process_frame(frame)
380
+ writer.write(rendered_frame)
381
+
382
+ writer.release()
383
+ error_message = None
384
+ codec_name = "mp4v"
385
+ except Exception as e:
386
+ error_message = f"OpenCV encode failed: {e}"
387
+ output_path = None
388
+ progress.empty()
389
+ cap.release()
390
+
391
  stats = {
392
  "total_frames": total_frames,
393
  "processed_frames": processed_frames,
394
  "detection_counts": detection_counts,
395
+ "codec": codec_name,
396
+ "error": error_message
397
  }
398
 
399
  return output_path, stats
 
641
  stats = st.session_state.video_stats
642
  st.metric("🎞️ Total Frames", stats.get("total_frames", 0))
643
  st.metric("✅ Processed Frames", stats.get("processed_frames", 0))
644
+ if stats.get("codec"):
645
+ st.caption(f"Codec: {stats.get('codec')}")
646
+ if stats.get("error"):
647
+ st.error(stats.get("error"))
648
 
649
  if stats.get("detection_counts"):
650
  counts_df = pd.DataFrame(