andito HF Staff commited on
Commit
5a17c56
·
verified ·
1 Parent(s): bbecb41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -6
app.py CHANGED
@@ -35,7 +35,7 @@ NUDGE_BODY = 0.3 # degrees for body_yaw
35
  NUDGE_PITCH = 5.0 # degrees for pitch
36
 
37
  # Video loop timing
38
- FRAME_SLEEP_S = 0.02
39
 
40
  # TURN config
41
  TURN_TTL_SERVER_MS = 360_000
@@ -134,7 +134,6 @@ class GlobalState:
134
  try:
135
  dropped = q.get_nowait()
136
  del dropped
137
- print(f"Dropping oldest audio {description}, queue size is {q.qsize()}")
138
  except queue.Empty:
139
  break
140
  q.put(item)
@@ -287,6 +286,8 @@ def send_pose_to_robot(mov: Movement, msg: str = "Move sent"):
287
 
288
  def generate_mjpeg_stream():
289
  last_timestamp = 0.0
 
 
290
  while True:
291
  with state.frame_lock:
292
  current_bytes = state.latest_frame_bytes
@@ -294,6 +295,13 @@ def generate_mjpeg_stream():
294
 
295
  if current_timestamp > last_timestamp and current_bytes is not None:
296
  last_timestamp = current_timestamp
 
 
 
 
 
 
 
297
  yield (
298
  b"--frame\r\n"
299
  b"Content-Type: image/jpeg\r\n\r\n" + current_bytes + b"\r\n"
@@ -311,6 +319,8 @@ def webrtc_video_generator():
311
  """
312
  last_ts = 0.0
313
  frame = state.black_frame.copy()
 
 
314
 
315
  while True:
316
  with state.frame_lock:
@@ -325,8 +335,16 @@ def webrtc_video_generator():
325
  frame = decoded
326
  else:
327
  frame = state.black_frame.copy()
 
 
 
 
 
 
 
328
 
329
  yield frame
 
330
 
331
 
332
  # -------------------------------------------------------------------
@@ -366,18 +384,29 @@ def video_feed():
366
  async def stream_endpoint(ws: WebSocket):
367
  """Endpoint for Robot/Sim to send video frames."""
368
  await ws.accept()
 
 
369
  try:
370
  while True:
371
  msg = await ws.receive()
372
  data = msg.get("bytes")
373
  if data:
374
  state.update_frame(data)
 
 
 
 
 
 
 
 
 
375
  except asyncio.CancelledError:
376
  print("[Video] stream_endpoint cancelled")
377
- except Exception:
378
- print("[Video] stream_endpoint closed")
379
  finally:
380
- print("[Video] stream_endpoint closed")
381
 
382
  @app.websocket("/audio_stream")
383
  async def audio_endpoint(ws: WebSocket):
@@ -1053,5 +1082,5 @@ app = gr.mount_gradio_app(app, demo, path="/")
1053
  if __name__ == "__main__":
1054
  print("🚀 Server starting on http://0.0.0.0:7860")
1055
  print("ℹ️ Point your Robot/Sim to: ws://<YOUR_PC_IP>:7860/robot")
1056
- uvicorn.run(app, host="0.0.0.0", port=7860, proxy_headers=True, forwarded_allow_ips="*")
1057
 
 
35
  NUDGE_PITCH = 5.0 # degrees for pitch
36
 
37
  # Video loop timing
38
+ FRAME_SLEEP_S = 0.04 # 25 fps
39
 
40
  # TURN config
41
  TURN_TTL_SERVER_MS = 360_000
 
134
  try:
135
  dropped = q.get_nowait()
136
  del dropped
 
137
  except queue.Empty:
138
  break
139
  q.put(item)
 
286
 
287
  def generate_mjpeg_stream():
288
  last_timestamp = 0.0
289
+ frame_count = 0
290
+ start_time = time.time()
291
  while True:
292
  with state.frame_lock:
293
  current_bytes = state.latest_frame_bytes
 
295
 
296
  if current_timestamp > last_timestamp and current_bytes is not None:
297
  last_timestamp = current_timestamp
298
+ frame_count += 1
299
+ elapsed = time.time() - start_time
300
+ if elapsed > 1.0:
301
+ fps = frame_count / elapsed
302
+ print(f"[generate_mjpeg_stream] Current FPS: {fps:.2f}")
303
+ frame_count = 0
304
+ start_time = time.time()
305
  yield (
306
  b"--frame\r\n"
307
  b"Content-Type: image/jpeg\r\n\r\n" + current_bytes + b"\r\n"
 
319
  """
320
  last_ts = 0.0
321
  frame = state.black_frame.copy()
322
+ frame_count = 0
323
+ start_time = time.time()
324
 
325
  while True:
326
  with state.frame_lock:
 
335
  frame = decoded
336
  else:
337
  frame = state.black_frame.copy()
338
+ frame_count += 1
339
+ elapsed = time.time() - start_time
340
+ if elapsed > 1.0:
341
+ fps = frame_count / elapsed
342
+ print(f"[webrtc_video_generator] Current FPS: {fps:.2f}")
343
+ frame_count = 0
344
+ start_time = time.time()
345
 
346
  yield frame
347
+ time.sleep(FRAME_SLEEP_S)
348
 
349
 
350
  # -------------------------------------------------------------------
 
384
  async def stream_endpoint(ws: WebSocket):
385
  """Endpoint for Robot/Sim to send video frames."""
386
  await ws.accept()
387
+ frame_count = 0
388
+ start_time = time.time()
389
  try:
390
  while True:
391
  msg = await ws.receive()
392
  data = msg.get("bytes")
393
  if data:
394
  state.update_frame(data)
395
+ frame_count += 1
396
+ elapsed = time.time() - start_time
397
+ if elapsed > 1.0:
398
+ fps = frame_count / elapsed
399
+ print(f"[Video] Receiving FPS: {fps:.2f}")
400
+ frame_count = 0
401
+ start_time = time.time()
402
+ except WebSocketDisconnect as e:
403
+ print(f"[Video] WebSocketDisconnect: code={e.code}, reason={e.reason}")
404
  except asyncio.CancelledError:
405
  print("[Video] stream_endpoint cancelled")
406
+ except Exception as e:
407
+ print(f"[Video] stream_endpoint closed with error: {e!r}")
408
  finally:
409
+ print("[Video] stream_endpoint closed (finally)")
410
 
411
  @app.websocket("/audio_stream")
412
  async def audio_endpoint(ws: WebSocket):
 
1082
  if __name__ == "__main__":
1083
  print("🚀 Server starting on http://0.0.0.0:7860")
1084
  print("ℹ️ Point your Robot/Sim to: ws://<YOUR_PC_IP>:7860/robot")
1085
+ uvicorn.run(app, host="0.0.0.0", port=7860, proxy_headers=True, forwarded_allow_ips="*", log_level="warning")
1086