main.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import base64
  3. import signal
  4. import time
  5. import cv2
  6. import numpy as np
  7. from fastapi import Response
  8. import nicegui.globals
  9. from nicegui import app, run, ui
  10. # In case you don't have a webcam, this will provide a black placeholder image.
  11. black_1px = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdjYGBg+A8AAQQBAHAgZQsAAAAASUVORK5CYII='
  12. placeholder = Response(content=base64.b64decode(black_1px.encode('ascii')), media_type='image/png')
  13. # OpenCV is used to access the webcam.
  14. video_capture = cv2.VideoCapture(0)
  15. def convert(frame: np.ndarray) -> bytes:
  16. _, imencode_image = cv2.imencode('.jpg', frame)
  17. return imencode_image.tobytes()
  18. @app.get('/video/frame')
  19. # Thanks to FastAPI's `app.get`` it is easy to create a web route which always provides the latest image from OpenCV.
  20. async def grab_video_frame() -> Response:
  21. if not video_capture.isOpened():
  22. return placeholder
  23. # The `video_capture.read` call is a blocking function.
  24. # So we run it in a separate thread (default executor) to avoid blocking the event loop.
  25. _, frame = await run.io_bound(video_capture.read)
  26. if frame is None:
  27. return placeholder
  28. # `convert` is a CPU-intensive function, so we run it in a separate process to avoid blocking the event loop and GIL.
  29. jpeg = await run.cpu_bound(convert, frame)
  30. return Response(content=jpeg, media_type='image/jpeg')
  31. # For non-flickering image updates an interactive image is much better than `ui.image()`.
  32. video_image = ui.interactive_image().classes('w-full h-full')
  33. # A timer constantly updates the source of the image.
  34. # Because data from same paths are cached by the browser,
  35. # we must force an update by adding the current timestamp to the source.
  36. ui.timer(interval=0.1, callback=lambda: video_image.set_source(f'/video/frame?{time.time()}'))
  37. async def disconnect() -> None:
  38. """Disconnect all clients from current running server."""
  39. for client in nicegui.globals.clients.keys():
  40. await app.sio.disconnect(client)
  41. def handle_sigint(signum, frame) -> None:
  42. # `disconnect` is async, so it must be called from the event loop; we use `ui.timer` to do so.
  43. ui.timer(0.1, disconnect, once=True)
  44. # Delay the default handler to allow the disconnect to complete.
  45. ui.timer(1, lambda: signal.default_int_handler(signum, frame), once=True)
  46. async def cleanup() -> None:
  47. # This prevents ugly stack traces when auto-reloading on code change,
  48. # because otherwise disconnected clients try to reconnect to the newly started server.
  49. await disconnect()
  50. # Release the webcam hardware so it can be used by other applications again.
  51. video_capture.release()
  52. app.on_shutdown(cleanup)
  53. # We also need to disconnect clients when the app is stopped with Ctrl+C,
  54. # because otherwise they will keep requesting images which lead to unfinished subprocesses blocking the shutdown.
  55. signal.signal(signal.SIGINT, handle_sigint)
  56. ui.run()