Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to use QThread to display videos
Forum Updated to NodeBB v4.3 + New Features

How to use QThread to display videos

Scheduled Pinned Locked Moved Unsolved Qt for Python
20 Posts 7 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #6

    Not knowing your current pipeline I can't really recommend anything.
    Can you give more details ?
    Are you showing the video plus some additional data ?
    Did you benchmark your analysis code ?
    Which framework are you using ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • M muxing

      @SGaist Thanks for reminding me
      Heavy processing involves real-time PIV analysis of images, which requires some time for acute calculations.If you wait for PIV analysis in the main thread for a few seconds, the main interface will appear unresponsive.Performing PIV analysis multiple times will reveal that the video display becomes stuttering and has high latency.

      The video signal was captured using a mobile phone camera and 1920X1080 in size.

      Due to the Python‘s GIL, even if I put complex calculation parts into auxiliary threads, the video displayed on the main interface may still be delayed and stuttered? If I use C++language for development, can auxiliary threads solve this problem?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #7

      @muxing The first thing to check if GPU is fully used. Another thing is to check if your display area has the same resolution as your camera 1920X1080. If not, reduce the resolution of your pipeline before the sink. For example, if the display area is 500*500, the resolution 1920X1080 of the stream can not be kept and your GPU will do something unuseful.

      If PIV analysis of images takes some time, I guess you may need to cache your streaming data or buy a better GPU for quicker processing.

      M 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @muxing The first thing to check if GPU is fully used. Another thing is to check if your display area has the same resolution as your camera 1920X1080. If not, reduce the resolution of your pipeline before the sink. For example, if the display area is 500*500, the resolution 1920X1080 of the stream can not be kept and your GPU will do something unuseful.

        If PIV analysis of images takes some time, I guess you may need to cache your streaming data or buy a better GPU for quicker processing.

        M Offline
        M Offline
        muxing
        wrote on last edited by
        #8

        @JoeCFD
        I have implemented real-time video display using the OpenCV module in the main program. However, later on, the PIV lamp calculation and analysis function was added, and it was found that performing PIV analysis and calculation would affect the display of the video. Therefore, I wanted to put the video in a separate thread.

        The good news is that I have successfully used threads to display the video today. The bad news is that I have also put the PIV analysis and calculation function into another separate thread, but when running the PIV calculation and analysis, there will still be some delay in the video, but the video display is continuous and not lagging.

        So when there are time-consuming tasks, will video inevitably experience delays?

        How to solve the problem of video delay?

        JonBJ J.HilkJ 2 Replies Last reply
        0
        • M muxing

          @JoeCFD
          I have implemented real-time video display using the OpenCV module in the main program. However, later on, the PIV lamp calculation and analysis function was added, and it was found that performing PIV analysis and calculation would affect the display of the video. Therefore, I wanted to put the video in a separate thread.

          The good news is that I have successfully used threads to display the video today. The bad news is that I have also put the PIV analysis and calculation function into another separate thread, but when running the PIV calculation and analysis, there will still be some delay in the video, but the video display is continuous and not lagging.

          So when there are time-consuming tasks, will video inevitably experience delays?

          How to solve the problem of video delay?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #9

          @muxing
          If you do computations which take longer to perform than the rate at which you need to display the result you will have trouble keeping up and no delay or frame skipping, won't you?

          M 1 Reply Last reply
          0
          • M muxing

            @JoeCFD
            I have implemented real-time video display using the OpenCV module in the main program. However, later on, the PIV lamp calculation and analysis function was added, and it was found that performing PIV analysis and calculation would affect the display of the video. Therefore, I wanted to put the video in a separate thread.

            The good news is that I have successfully used threads to display the video today. The bad news is that I have also put the PIV analysis and calculation function into another separate thread, but when running the PIV calculation and analysis, there will still be some delay in the video, but the video display is continuous and not lagging.

            So when there are time-consuming tasks, will video inevitably experience delays?

            How to solve the problem of video delay?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #10

            @muxing if you've done the video processing properly, it should be an easy enough task to create multiple instances of it and process multiple frames in parallel.

            As long as you do not exceed the number of your cpu/gpu cores it should improve the performance. Just make sure you pipeline the frames correctly so you can assemble them in the correct order after processing.

            Are you actually using CUDA or OpenCL? if not all processing is done on the CPU anyway. So keep that in mind.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • JonBJ JonB

              @muxing
              If you do computations which take longer to perform than the rate at which you need to display the result you will have trouble keeping up and no delay or frame skipping, won't you?

              M Offline
              M Offline
              muxing
              wrote on last edited by
              #11

              @JonB
              There is not much direct relationship between calculation and video display.

              Because video updates are still in the main interface, what we have observed now is that even without complex calculations, there will still be delays over time. After running the program for more than two hours, the delay has reached about 8 seconds.

              According to online reports, using OpenCV to read network camera videos generally results in delays. However, I think the time spent on other running parts of the program will also affect the update speed of the video screen in the main interface, and as time accumulates, the delay becomes more and more obvious.

              Now I have written a main interface that only displays videos without any other functions. I will test and see how much video delay can accumulate over time to determine the impact of my other functions on video display.

              1 Reply Last reply
              0
              • CristianMaureiraC Offline
                CristianMaureiraC Offline
                CristianMaureira
                wrote on last edited by
                #12

                (Side note: a lot of thread-related issues were improved in PySide6, so if possible try to use that instead of PySide2)

                M 1 Reply Last reply
                0
                • M muxing

                  @SGaist Thanks for reminding me
                  Heavy processing involves real-time PIV analysis of images, which requires some time for acute calculations.If you wait for PIV analysis in the main thread for a few seconds, the main interface will appear unresponsive.Performing PIV analysis multiple times will reveal that the video display becomes stuttering and has high latency.

                  The video signal was captured using a mobile phone camera and 1920X1080 in size.

                  Due to the Python‘s GIL, even if I put complex calculation parts into auxiliary threads, the video displayed on the main interface may still be delayed and stuttered? If I use C++language for development, can auxiliary threads solve this problem?

                  S Offline
                  S Offline
                  SimonSchroeder
                  wrote on last edited by
                  #13

                  @muxing said in How to use QThread to display videos:

                  Due to the Python‘s GIL, even if I put complex calculation parts into auxiliary threads, the video displayed on the main interface may still be delayed and stuttered? If I use C++language for development, can auxiliary threads solve this problem?

                  Python's GIL means that only a single thread is executed at a time (there's currently an effort to remove the GIL, but you have to compile Python yourself to get it). You don't get real multithreading. This means that C++ would solve your problem. Almost any other language will solve your problem (I am not aware of any other language having something comparable to the GIL). In the future you might be able to use Mojo which aims to be fully compatible with Python syntax (eventually).

                  JonBJ M 2 Replies Last reply
                  0
                  • S SimonSchroeder

                    @muxing said in How to use QThread to display videos:

                    Due to the Python‘s GIL, even if I put complex calculation parts into auxiliary threads, the video displayed on the main interface may still be delayed and stuttered? If I use C++language for development, can auxiliary threads solve this problem?

                    Python's GIL means that only a single thread is executed at a time (there's currently an effort to remove the GIL, but you have to compile Python yourself to get it). You don't get real multithreading. This means that C++ would solve your problem. Almost any other language will solve your problem (I am not aware of any other language having something comparable to the GIL). In the future you might be able to use Mojo which aims to be fully compatible with Python syntax (eventually).

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #14

                    @SimonSchroeder said in How to use QThread to display videos:

                    Python's GIL means that only a single thread is executed at a time

                    When I looked this up a long time ago, my limited understanding was: Python script can have multiple threads executing like other (e.g. C++) programs. But when a thread comes to execute a Python instruction (e.g. as opposed to running just Qt code) then it blocks (becomes single-threaded) against any other thread executing a Python instruction? It is the Python instruction interpreter which is effectively single-threaded, not what those threads are doing (if executing some non-Python instruction code).

                    M 1 Reply Last reply
                    0
                    • CristianMaureiraC CristianMaureira

                      (Side note: a lot of thread-related issues were improved in PySide6, so if possible try to use that instead of PySide2)

                      M Offline
                      M Offline
                      muxing
                      wrote on last edited by
                      #15

                      @CristianMaureira
                      I initially installed PySide6, but when using QtDesigner, it froze and I had no choice but to install PySide2 as the second best option.

                      1 Reply Last reply
                      0
                      • S SimonSchroeder

                        @muxing said in How to use QThread to display videos:

                        Due to the Python‘s GIL, even if I put complex calculation parts into auxiliary threads, the video displayed on the main interface may still be delayed and stuttered? If I use C++language for development, can auxiliary threads solve this problem?

                        Python's GIL means that only a single thread is executed at a time (there's currently an effort to remove the GIL, but you have to compile Python yourself to get it). You don't get real multithreading. This means that C++ would solve your problem. Almost any other language will solve your problem (I am not aware of any other language having something comparable to the GIL). In the future you might be able to use Mojo which aims to be fully compatible with Python syntax (eventually).

                        M Offline
                        M Offline
                        muxing
                        wrote on last edited by
                        #16

                        @SimonSchroeder
                        So Python is currently not very suitable for developing overly complex programs.

                        Thank you for recommending Mojo language to me. This is my first time hearing about this new language and I look forward to it getting better and better.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          Python is suitable, however it needs some care depending on what you do.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          M 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @SimonSchroeder said in How to use QThread to display videos:

                            Python's GIL means that only a single thread is executed at a time

                            When I looked this up a long time ago, my limited understanding was: Python script can have multiple threads executing like other (e.g. C++) programs. But when a thread comes to execute a Python instruction (e.g. as opposed to running just Qt code) then it blocks (becomes single-threaded) against any other thread executing a Python instruction? It is the Python instruction interpreter which is effectively single-threaded, not what those threads are doing (if executing some non-Python instruction code).

                            M Offline
                            M Offline
                            muxing
                            wrote on last edited by
                            #18

                            @JonB
                            Thank you for sharing your valuable experience, it is very helpful for beginners to understand.

                            1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Python is suitable, however it needs some care depending on what you do.

                              M Offline
                              M Offline
                              muxing
                              wrote on last edited by
                              #19

                              @SGaist
                              So what is Python suitable for?

                              SGaistS 1 Reply Last reply
                              0
                              • M muxing

                                @SGaist
                                So what is Python suitable for?

                                SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #20

                                @muxing It's useful for a large range of applications whether on the command line or using a GUI.

                                It's used for automation, scientific computation, application extension, etc.

                                As I wrote above, you have to be careful when you code for it (as for any other language, all have limits).

                                As for the multi-threading part, check Python 3.13. The GIL is being removed.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved