adeem6 munals commited on
Commit
e89e5aa
Β·
1 Parent(s): eb54c39

Update backend/api.py (#8)

Browse files

- Update backend/api.py (3d7732c7acf648b7fd830ded7c62408a4a543e78)


Co-authored-by: Munirah <munals@users.noreply.huggingface.co>

Files changed (1) hide show
  1. backend/api.py +54 -15
backend/api.py CHANGED
@@ -239,23 +239,59 @@ def _serialize_state() -> dict:
239
 
240
  def _pipeline_loop():
241
  cap = pipeline.get_video_capture()
242
- print('πŸŽ₯ [API] Video loop started')
 
 
 
243
  while True:
 
244
  ret, frame = cap.read()
245
  if not ret:
246
- # Loop video β€” reset EMA so score doesn't carry stale HIGH into next loop
247
  cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
248
- pipeline.risk._peak_ema = 0.0
249
- pipeline.risk._occ_ema = 0.0
250
- pipeline.state['risk_score'] = 0.0
251
- pipeline.state['risk_level'] = 'LOW'
252
- pipeline.state['density_pct'] = 0.0
253
- print('πŸ” [API] Video loop restarted β€” peak_ema + occ_ema reset to 0')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  continue
255
  try:
256
  pipeline.process_one_frame(frame)
257
  except Exception as exc:
258
  print(f'[API] Frame error: {exc}')
 
 
 
 
259
 
260
 
261
  # ── Startup ────────────────────────────────────────────────────────────
@@ -326,13 +362,16 @@ def reset_pipeline():
326
  # Reset video to start
327
  cap = pipeline.get_video_capture()
328
  cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
329
- # Reset risk EMA
330
- pipeline.risk._peak_ema = 0.0
331
- pipeline.risk._occ_ema = 0.0
332
- pipeline.state['risk_score'] = 0.0
333
- pipeline.state['risk_level'] = 'LOW'
334
- pipeline.state['density_pct'] = 0.0
335
- pipeline.state['frame_id'] = 0
 
 
 
336
  # Reset perception agent
337
  pipeline.perception.frame_id = 0
338
  # Clear frame buffer
 
239
 
240
  def _pipeline_loop():
241
  cap = pipeline.get_video_capture()
242
+ # ~10 FPS: fast enough to look real-time, slow enough for LLM to keep up
243
+ TARGET_FPS = 10
244
+ frame_delay = 1.0 / TARGET_FPS
245
+ print(f'πŸŽ₯ [API] Video loop started (target {TARGET_FPS} FPS, {frame_delay*1000:.0f}ms/frame)')
246
  while True:
247
+ t0 = time.time()
248
  ret, frame = cap.read()
249
  if not ret:
250
+ # Loop video β€” full reset so next loop plays fresh
251
  cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
252
+
253
+ # Reset risk agent (clip state + EMA)
254
+ pipeline.risk._reset_clip(0, 'video loop restart')
255
+ pipeline.risk._peak_ema = 0.0
256
+ pipeline.risk._occ_ema = 0.0
257
+ pipeline.risk._prev_count = 0
258
+ pipeline.risk._prev_density_sc = 0.0
259
+ pipeline.risk._boundary_buf = 0
260
+ pipeline.risk._in_boundary = False
261
+
262
+ # Reset perception frame counter
263
+ pipeline.perception.frame_id = 0
264
+
265
+ # Reset effective level tracker so first level change fires correctly
266
+ pipeline._last_effective_risk_level = 'LOW'
267
+
268
+ # Reset P0 rate-limit so previous loop's cooldown doesn't block
269
+ from datetime import datetime as _dt
270
+ pipeline.operations._boot_time = _dt.now()
271
+
272
+ # Reset pipeline state (scores + decisions)
273
+ pipeline.state['risk_score'] = 0.0
274
+ pipeline.state['risk_level'] = 'LOW'
275
+ pipeline.state['density_pct'] = 0.0
276
+ pipeline.state['frame_id'] = 0
277
+ pipeline.state['decisions_log'] = []
278
+ pipeline.state['latest_decision'] = None
279
+ pipeline.state['arabic_alert'] = ''
280
+ pipeline.state['coordinator_plan'] = None
281
+
282
+ # Clear frame buffer so frontend shows fresh frames
283
+ pipeline._frame_buffer.clear()
284
+
285
+ print('πŸ” [API] Video loop restarted β€” full state reset')
286
  continue
287
  try:
288
  pipeline.process_one_frame(frame)
289
  except Exception as exc:
290
  print(f'[API] Frame error: {exc}')
291
+ # Pace to match real video FPS
292
+ elapsed = time.time() - t0
293
+ if elapsed < frame_delay:
294
+ time.sleep(frame_delay - elapsed)
295
 
296
 
297
  # ── Startup ────────────────────────────────────────────────────────────
 
362
  # Reset video to start
363
  cap = pipeline.get_video_capture()
364
  cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
365
+ # Reset risk β€” full clip state + EMA
366
+ pipeline.risk._reset_clip(0, 'API reset')
367
+ pipeline.risk._peak_ema = 0.0
368
+ pipeline.risk._occ_ema = 0.0
369
+ pipeline.risk._prev_count = 0
370
+ pipeline.risk._prev_density_sc = 0.0
371
+ pipeline.state['risk_score'] = 0.0
372
+ pipeline.state['risk_level'] = 'LOW'
373
+ pipeline.state['density_pct'] = 0.0
374
+ pipeline.state['frame_id'] = 0
375
  # Reset perception agent
376
  pipeline.perception.frame_id = 0
377
  # Clear frame buffer