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

fixing camera n1235

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +51 -30
src/streamlit_app.py CHANGED
@@ -291,53 +291,74 @@ def process_video(video_path, model, conf_threshold, frame_skip=1):
291
  output_fd, output_path = tempfile.mkstemp(suffix=".mp4")
292
  os.close(output_fd)
293
 
294
- fourcc = cv2.VideoWriter_fourcc(*"mp4v")
295
- writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
296
-
297
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 0
298
  processed_frames = 0
299
  detection_counts = {}
300
 
301
  progress = st.progress(0, text="Processing video frames...")
302
 
303
- frame_index = 0
304
- while True:
305
- ret, frame = cap.read()
306
- if not ret:
307
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
 
309
- frame_index += 1
310
- if frame_skip > 1 and frame_index % frame_skip != 0:
311
- writer.write(frame)
312
- continue
313
 
314
- img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
315
- model.conf = conf_threshold
316
- results = model(img_rgb)
317
 
318
- detections = results.pandas().xyxy[0]
319
- relevant = detections[detections['name'].isin(WHEELCHAIR_CLASSES.values())]
 
 
 
320
 
321
- for name in relevant['name'].tolist():
322
- detection_counts[name] = detection_counts.get(name, 0) + 1
 
323
 
324
- rendered_frame = results.render()[0]
325
- if rendered_frame.shape[1] != width or rendered_frame.shape[0] != height:
326
- rendered_frame = cv2.resize(rendered_frame, (width, height))
327
- writer.write(rendered_frame)
328
 
329
- processed_frames += 1
330
- if total_frames > 0:
331
- progress.progress(min(processed_frames / total_frames, 1.0))
332
 
333
- progress.empty()
334
- cap.release()
335
- writer.release()
 
 
 
 
 
 
336
 
337
  stats = {
338
  "total_frames": total_frames,
339
  "processed_frames": processed_frames,
340
- "detection_counts": detection_counts
 
341
  }
342
 
343
  return output_path, stats
 
291
  output_fd, output_path = tempfile.mkstemp(suffix=".mp4")
292
  os.close(output_fd)
293
 
 
 
 
294
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 0
295
  processed_frames = 0
296
  detection_counts = {}
297
 
298
  progress = st.progress(0, text="Processing video frames...")
299
 
300
+ container = None
301
+ stream = None
302
+ codec_name = None
303
+ try:
304
+ container = av.open(output_path, mode="w")
305
+ try:
306
+ stream = container.add_stream("libx264", rate=fps)
307
+ codec_name = "libx264"
308
+ except av.AVError:
309
+ stream = container.add_stream("mpeg4", rate=fps)
310
+ codec_name = "mpeg4"
311
+ stream.width = width
312
+ stream.height = height
313
+ stream.pix_fmt = "yuv420p"
314
+
315
+ frame_index = 0
316
+ while True:
317
+ ret, frame = cap.read()
318
+ if not ret:
319
+ break
320
 
321
+ frame_index += 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