fastplotlib / fastplotlib

Next-gen fast plotting library running on WGPU using the pygfx rendering engine
https://fastplotlib.readthedocs.io/
Apache License 2.0
348 stars 33 forks source link

Tutorial simple videoloop capture #493

Open johnnynunez opened 1 month ago

johnnynunez commented 1 month ago

I have discovered this fantastic repository and it looks great. But it would be ideal to have tutorials to migrate code from other libraries like opencv. An example:

How to replace this type of loops by fastplotlib? I want to avoid cv2 and waitkey that drops 50% of the fps. https://forum.opencv.org/t/fps-drop-due-to-the-waitkey-function/3717

stream = FrameCapture(0)
    stream.start()
    frame_count = 0
    start_time = time.time()

    try:
        while True:
            frame = stream.read()
            if frame is not None:
                frame_count += 1
                elapsed_time = time.time() - start_time
                if elapsed_time > 0:
                    fps = frame_count / elapsed_time
                    print(f"FPS: {fps:.2f}")
                    cv2.putText(frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

                cv2.imshow('Webcam', frame)
                if cv2.waitKey(1) & 0xFF == ord('q'): # this drops 50% of the fps
                    break
            else:
                break
    finally:
        cv2.destroyAllWindows()
        stream.stop()
        print("Exiting program")
kushalkolar commented 1 month ago

It sound like you need a separate process for streaming in the data and another for rendering the frames. We have a multiprocessing example that uses zmq: https://github.com/fastplotlib/fastplotlib/tree/main/examples/notebooks/multiprocessing_zmq

johnnynunez commented 1 month ago

It sound like you need a separate process for streaming in the data and another for rendering the frames. We have a multiprocessing example that uses zmq: https://github.com/fastplotlib/fastplotlib/tree/main/examples/notebooks/multiprocessing_zmq

I have one thread for capture using vidgear, another for videowrite and in the main thread the GUI But opencv is terrible for showing a simple video if you want high fps or same as real time

https://github.com/STCE-Detector/small-fast-detector/tree/demo/demo

kushalkolar commented 1 month ago

You can use fastplotlib to display frames received from another process, the zmq example I linked above shows how to do it. The CONFLATE option in zmq allows you to display only the most recent frame so that the rendering process doesn't create a backlog (you may receive frames faster than the refresh rate of your monitor).

johnnynunez commented 1 month ago
fig = fpl.Figure()
      ^^^^^^^^^^

AttributeError: module 'fastplotlib' has no attribute 'Figure'

My installation was with pip. Should I install from the repository?

johnnynunez commented 1 month ago
fig = fpl.Figure()
      ^^^^^^^^^^

AttributeError: module 'fastplotlib' has no attribute 'Figure'

My installation was with pip. Should I install from the repository?

fixed with pip install git+https://github.com/fastplotlib/fastplotlib.git

kushalkolar commented 1 month ago

Yes, we're rapidly developing and the API is evolving but we're happy to help out if you need anything :)