question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question on frame rate

See original GitHub issue

I was using the CalculateFPSExample.java app in order to test the frame rate. As expected with my HD cam it is always about 30 fps.

Then I combined it with the preview display and was surprised, that the frame rate is only about 15 fps now. The same happens, if I do a webcam.open(false) w/o preview

Any idea, why? For me this is a bit too much “Heisenbergsche Unschärfe” (if you know what I mean) 😃

My sample app:

public static void main(String[] args) {
        final Webcam webcam = Webcam.getDefault();
        webcam.setViewSize(WebcamResolution.VGA.getSize());
        webcam.open();

//        Uncommenting this reduces the framerate by 50 %

//        final WebcamPanel panel = new WebcamPanel(webcam);
//        panel.setFPSDisplayed(true);
//        panel.setImageSizeDisplayed(true);
//        panel.setMirrored(true);
//        JFrame window = new JFrame("Test webcam panel");
//        window.setLayout(new FlowLayout());
//        window.add(panel);
//
//        window.setResizable(true);
//        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//        window.pack();
//        window.setVisible(true);

        new Thread(new Runnable() {
            @Override
            public void run() {
                long t1 = 0;
                long t2 = 0;

                int p = 100;
                int r = 10;

                for (int k = 0; k < p; k++) {
                    t1 = System.currentTimeMillis();
                    for (int i = 0; ++i <= r; webcam.getImageBytes()) {
                    }
                    t2 = System.currentTimeMillis();
                    System.out.println("FPS " + k + ": " + (1000 * r / (t2 - t1)));
                }
            }
        }).start();
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sarxoscommented, Dec 11, 2017

Hi @neilyoung 😄

Indeed, the quantum uncertainty may be to blame, and it may be actually caused by a different quantum states of electrons in the CPU due to the corresponding wave function:

image

But in your specific case this is much simpler to explain. You see, when you take a look on how WebcamPanel is implemented, you may notice a Thread running in background which constantly invokes getImage() and renders it on a screen. This happens with maximum FPS rate to have as smooth preview as possible.

Now add next thread to the solution (the one from your example), so we have two threads now. Because getImage() is blocking by default, these two threads are fighting for access time. Since both have exactly the same priority, JVM gives 50% of access time to first one, and 50% of access time to the second one. Do you see connection here? 100% of access time is 30 FPS (max what your camera can give), then 50% is 15 FPS, and when you add one more thread this will drop down to 33%, which is 10 FPS:

FPS thread 2: 5: 9
FPS thread 1: 5: 9
FPS thread 2: 6: 10
FPS thread 1: 6: 10

You may ask: if this is true, why the hell I see 30 PFS displayed in preview? This is because preview displays FPS of camera, not the thread which capture image. If you sum FPS from all threads then you will get ca. 30 FPS.

Solution? Yes, there is and it’s fairy simple. You have to make getImage() non blocking. This can be done by doing webcam.open(true) which will open webcam in a asynchronous (non-blocking) mode. But please note that this will only work for getImage(), not for getImageBytes().

I have to think about a way of how you can make getImageBytes() non blocking and will come back to you later.

0reactions
neilyoungcommented, Jan 15, 2018

@sarxos Thanks for your patience. Will try to check that out later

Read more comments on GitHub >

github_iconTop Results From Across the Web

Video Frame Rate - Top 10 Questions - Gumlet
3. Why does video frame rate matter? · How realistic do you want your video to be? · Do you plan on using...
Read more >
The Frames Per Second Question - MeltTools
First, slow the balls down to 50 px/s. Notice the background – it does appear more lifelike at 48 fps than 24 fps....
Read more >
Frame rate | Algebra I Quiz - Quizizz
Play this game to review Algebra I. what is the frame rate if 50 frames pass in 2 seconds. ... 10 QuestionsShow answers....
Read more >
Some questions on frame rates. : r/gamedev - Reddit
I know that variable frame rates are now the norm in 3D PC games, but are there any noteworthy examples of modern games...
Read more >
newbie frame rate question - Gearspace.com
I've asked the director what the final destination frame rate will be and to provide me a video at that rate ... My...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found