Closed kevinranks closed 2 weeks ago
I'm experiencing camera lag
What does that mean? What is camera lag? Is the FPS slow? Is it completely freezing after 6 images? Is it stuttering behind? Is it sometimes smooth sometimes stuttering? Post a video so I can see what you are talking about.
STRATEGY_BLOCK_PRODUCER
is used intentionally, this is the behaviour a Frame Processor should have. This is the same strategy that is also used on iOS.
It's what I'm trying to explain in the Frame Processor documentation page - frames stream in as often as they can (with fps={30}
, 30 times a second, or every 33ms), and if your Frame Processor is not fast enough to finish executing before a new frame arrives, it might drop a new Frame.
STRATEGY_BLOCK_PRODUCER
basically means that we push Frames into a queue, and if the queue is full we drop all Frames until a slot is free in the queue.
So if your queue is too slow, you need to do one of two things;
scanOCR
faster)runAsync
to run something that takes longer asynchronously. If your FP is called 30 times a second and you use runAsync
which takes longer than 33ms to execute, it will skip running that runAsync code until the Async Context is free again, which is exactly the same behaviour as using STRATEGY_KEEP_ONLY_LATEST
.
That's what the enableFpsGraph={true}
should help you with. If you easily hit 30 or 60 there, you're good.With the introduction of runAsync
we have the power of using STRATEGY_BLOCK_PRODUCER
by default, and STRATEGY_KEEP_ONLY_LATEST
being opt in (aka using runAsync
).
What STRATEGY_KEEP_ONLY_LATEST
does is essentially just not calling the ImageAnalysis
function if it is still busy. There is no magic behind it - it's simply dropping Frames and allowing the Preview to run freely, we can do the same thing with runAsync
.
So I'm not sure if I understood your issue correctly, so curious to hear your thoughts on this after the above explanation as to why I think STRATEGY_KEEP_ONLY_LATEST
is a bad idea. :)
As per the documentation of STRATEGY_KEEP_ONLY_LATEST
:
If only one image is delivered at a time, runAsync
would effectively achieve nothing anymore, since no Frames can come in in parallel.
@mrousavy here are some videos and also two ways of handling this https://github.com/ismaelsousa/vision-camera-ocr/issues/9
Replied over in that issue. This is expected behaviour.
What's happening?
With the back pressure strategy set to
STRATEGY_BLOCK_PRODUCER
instead ofSTRATEGY_KEEP_ONLY_LATEST
I'm experiencing camera lag while processing OCR unless I userunAsync()
. I've confirmed that changing the strategy resolves the issue andrunAsync()
isn't required.https://github.com/mrousavy/react-native-vision-camera/blob/main/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt#L348
I previously opened issues in these two OCR repos prior to learning with Ismael on Discord that the issue was indeed the back pressure strategy:
Reproduceable Code
Relevant log output
Camera Device
Device
Samsung Galaxy 24 Ultra
VisionCamera Version
4.0.1
Can you reproduce this issue in the VisionCamera Example app?
Yes, I can reproduce the same issue in the Example app here
Additional information