Trying to control focus on an Arducam B029001 using Python + OpenCV on Windows, and I’m stuck. I can set capture width and height fine with cap.set(), but focus won’t respond at all. If I disable autofocus, that part works, but then I still can’t actually set the focus value. Is the focus range supposed to be 0-255? The weird thing is the same camera focuses fine manually in the Windows camera app and in the Arducam capture software, so the hardware can do it - it’s only broken through Python/OpenCV. The stream is also a bit intermittent/unstable in general. I’ve been reading docs and SDKs online but they’re hard to follow, and I’m not getting any errors - it just doesn’t change focus.
Let’s separate two things: the focus mode (auto/manual) and the focus value. Disabling autofocus only changes the mode; to actually set focus you have to modify the focus value parameter itself. The valid focus range is 0-255. I’ve sent you a screenshot of the parameter via private message. I’ve also sent you a demo code script via PM - give that a try and let me know what happens.
I added a trackbar to the OpenCV window and passed the raw slider value straight into cap.set(). What’s strange is values from roughly 0 all the way up to 1000 seem to do something - the image keeps changing. That’s way past the 0-255 range you mentioned. Could there be some scaling going on, or a difference in drivers/firmware?
The valid focus range is 0-255. What you’re seeing past 255 - once you pull the slider beyond 255, the preview window stops changing - indicates those higher values aren’t actually valid. Keep the focus value within 0-255 and ignore anything above it.
Even so, the slider still changes the focus past 255 in my tests. I switched to autofocus mode and printed the current focus value with cap.get(), and it reports anywhere from about 90 to about 912. That’s well outside 0-255. I can send a video of the script running if that helps.
Yes, please send the video. It’ll be easier to see exactly what the focus value does while you move the slider.
Here it is: https://drive.google.com/file/d/1dKV-4u3zAMgvK6SfaO-FMjBGtosekaPn/view
Once the slider hits about 1013, it stops changing the actual focus value at all. And I can’t get the camera to focus on anything in the frame - not manually, not with autofocus.
Thanks. Try the demo code I sent you earlier and see if it behaves any differently. Right now it looks like the focus is being applied as raw values beyond the expected 0-255 range, and it stops changing past ~1013. In autofocus you were also seeing cap.get() report roughly 90-912. The demo code may help narrow this down.
I ran your script and it’s the same as the first video. Then I changed the VideoCapture preferred backend to cv.CAP_MSMF (line 8) and set the slider max very high. With that, focus changes from 0 to about 1023. So why isn’t the range 0-255? For comparison, the Windows camera app shows focus as a percentage. Two videos:
- Windows camera app: https://drive.google.com/file/d/1jHpl93YhoUJ8qSdrBnbBUUqKX8tYrPHd/view
- OpenCV with CAP_MSMF: https://drive.google.com/file/d/1YrWowbir4FU-ZEknwlE8OQKeYkQllGuh/view
That’s a useful data point. With cv.CAP_MSMF it looks like focus is being handled as a 10-bit value (0-1023) instead of 0-255, which would explain the numbers you’re seeing. What resolution are you running the OpenCV capture at? Can you try changing the resolution and see if the focus range or behavior shifts? The Windows camera app showing a percentage is consistent with it mapping the internal range to 0-100%.
After more testing, the camera no longer adjusts focus below about 400. I suspect it might not be entirely a backend issue, because I changed the resolution to 4656 x 3496 for this test. For reference, the earlier OpenCV video was 1280 x 720, and the Windows test was 1744p 4:3 at 30fps.
To summarize where things stand: with the default backend the slider stopped changing focus around 1013, and with cv.CAP_MSMF it ranged 0-1023 rather than 0-255, so the camera appears to expose a 10-bit focus range to OpenCV, and the behavior also shifts with resolution. After switching to 4656 x 3496, focus wouldn’t go below ~400. We don’t have a confirmed root cause yet. In the meantime, if you need reliable manual focus, the Windows camera app or Arducam capture software still work - we’ll keep looking into the OpenCV backend behavior at different resolutions.