Arducam_64mp autofocus via picamera2

Well, in a final act of desperation I just started trying everything I could find in any thread anywhere. In one thread I read someone was entering “sudo ./install_pivariety_pkgs.sh -d” and now suddenly my 16mp camera will autofocus and is very clear using:
libcamera-still -t5000 --autofocus-mode auto -o test.jpg

Also Picamera2 seems to be working with:
picam2.set_controls({“AfMode”: 0, “LensPosition”: 425})

Now, if I can get all my other items installed into this environment and it still works I’ll be beside myself with joy. Will update everyone in what is probably a multi-day journey.

1 Like

And as an update. I can get the 64mp to autofocus and take a 16mp picture with libcamera-still using:
libcamera-still --autofocus-mode auto -t 8000 -o test64.jpg

I am unable to get it to take a 64mp image as it just hangs or gives me an error that it cannot allocate memory and failed to allocate buffers/start camera (even though it opens a preview and autofocuses before it throws the error).
libcamera-still --autofocus-mode auto --width 9152 --height 6944 -t 8000 -o test64.jpg

I am also unable to get it to work with autofocus within python, but still playing with it.

In python, setting dimensions down to 16mp works for me with the 64mp camera.

import time
from picamera2 import Picamera2, Preview

picam2 = Picamera2()

capture_config = picam2.create_still_configuration(main={"size": (4264,3472)})
picam2.configure(capture_config)
picam2.start()
time.sleep(1)

picam2.set_controls({"AfSpeed": 1, "AfMode": 1})

picam2.switch_mode_and_capture_file(capture_config, "image64.png")

picam2.stop()

But, still having issues when trying to capture at full resolution (removing the capture_config part main={“size”: (4264,3472)})

I receive:

[1:54:00.253199480] [2507] ERROR V4L2 v4l2_videodevice.cpp:1241 /dev/video21[17:cap]: Unable to request 1 buffers: Cannot allocate memory
Failed to allocate buffers.
Traceback (most recent call last):
  File "/home/pi/python_test/ard16.py", line 30, in <module>
    picam2.switch_mode_and_capture_file(capture_config, "image64c.png")
  File "/usr/local/lib/python3.9/dist-packages/picamera2/picamera2.py", line 1232, in switch_mode_and_capture_file
    return self.dispatch_functions(functions, wait, signal_function)
  File "/usr/local/lib/python3.9/dist-packages/picamera2/picamera2.py", line 1163, in dispatch_functions
    return job.get_result() if wait else job
  File "/usr/local/lib/python3.9/dist-packages/picamera2/job.py", line 77, in get_result
    return self._future.result()
  File "/usr/lib/python3.9/concurrent/futures/_base.py", line 440, in result
    return self.__get_result()
  File "/usr/lib/python3.9/concurrent/futures/_base.py", line 389, in __get_result
    raise self._exception
  File "/usr/local/lib/python3.9/dist-packages/picamera2/job.py", line 47, in execute
    done, result = self._functions[0]()
  File "/usr/local/lib/python3.9/dist-packages/picamera2/picamera2.py", line 1208, in switch_mode_
    self.configure_(camera_config)
  File "/usr/local/lib/python3.9/dist-packages/picamera2/picamera2.py", line 956, in configure_
    raise RuntimeError("Failed to allocate buffers.")
RuntimeError: Failed to allocate buffers.

You need to enable more buffer space for 64mp: How to use arducam_64mp & arducam_64mp FAQ - #2 by Edward

1 Like

thanks for all your hard work @MichRX7
i’m really not trying to be a complainer, like all the other folks in this forum i just want this thing to work, but i am REALLY disappointed in the Arducam team for upgrading/breaking the entire thing they sold us, and not replying for WEEKS now :frowning: to what should be VERY BASIC ISSUES (like how to get the 64mp autofocus camera to take 64mp autofocus images)

cmon folks like @Dion @Edward and the rest of the support team. Help us out here, or at the very least give us (and folks like MichRX7) the source code so we can fix the stuff for you

@MichRX7 @hikinghack @Minesv @Nighthawk @mgrl

First of all I’m sorry for your experience.

I would like to make some explanations about how the focus part is used, modified again and again.
Libcamera did not implement the focus function before, so we developed it and continued to improve the way of use based on user feedback. Now libcamera has also developed this function, so we keep in sync with the official, so we made a big change.

I thought about why the usage aspect happened, it has something to do with not posting how to use it, I feel like this is missing in our work, so sorry.

The way it was resolved was that I took the advice of @AnotherDredd, an active member of our forum community, who has been supporting our work and offering ways to improve it, for which I thank you again.

Then there is the 64mp maximum resolution problem, and I have been looking for a solution.

Just like the link provided by @mgrl, I sorted out a lot of questions about 64mp, and I also hope that you can use our camera smoothly, but the reality is that there are many states, and this is one of my jobs, which is to find different states solution.

Because this post has said a lot now, if you still have questions, you can open a new post to discuss with me, I will be happy to communicate with you.

Because of other work reasons, sometimes I may see a thread being replied to. I would think that everyone was discussing together, so I didn’t participate. Or sometimes I forget to reply to certain posts. If I have any bad impression on you, I am very very sorry.

In the following work, I will also consider your feelings more, look at problems from your perspective, and be more timely.

1 Like

Well, I have tried everything in those links at this point and am still getting the error:
Failed to allocate buffers ERROR:
failed to start camera

My guess at this point is because I am running the GUI and dual monitors. I guess I’m sticking with the 16mp which is a shame.

@Edward Another problem is that when autofocus does work (in libcamera-still) it is now very slow. It takes almost twice as long to autofocus in the latest release (.12) than it did in .10. While I appreciate that some changes were made to make it compliant with the autofocus parameters added to libcamera, surely this could be better.

Can you share your autofocus algorithm? Part of the slowness appears to be that it only changes the lens position one step for every 5 frames.actually captured.

1 Like

@Nighthawk

In fact, we recently open-sourced it, including the autofocus algorithm.

We will also implement a faster autofocus algorithm later, so stay tuned.

I’ve been working with 4 separate pi’s with 64MP cameras and have managed to get them all to take 9152x6944 images. A couple of things that I am doing / not doing.

I’m not using picamera2 python libraries (was planning on trying that this weekend)

I’m doing it via a custom python app and using pOpen to execute a cmd line straight to the libcamera-still commands. I boot it up in signal mode waiting for a code execution to trigger SIGUSR1 to take the photo.

Single monitor only.

I dont show preview window at all using - n

I’m setting manual focus ( can test autofocus this weekend probably for you. I have stayed on the 0.11 drivers though as 0.12 broke my focus numbers and I need to figure those out again)

I had 2 of my Pis machine with the dtoverlay change set in the [all] area and they weren’t working at full res. moved them to [pi4] section and they worked.

3 of my Pi’s are 4GB and one is an 8GB.

My cmd line arguments are basically this
libcamera-still -t 0 -n --autofocus-mode manual --lens-position 2 --width 9152 --height 6944 --denoise cdn_off --gain 9 --shutter 10000 --awbgains 2.0,1.8 --signal -o PiCam1_%04d.jpg --framestart 1

Hope some of this may help.

1 Like

@DaveMcD

Regarding usage, you don’t have to worry about it in the future. After 0.0.12, our usage has been synchronized with the official ones, and we will not change unless there are major changes in the official ones.

For the latest update, you can refer to the link below
https://docs.arducam.com/Raspberry-Pi-Camera/Native-camera/Libcamera-User-Guide/#for-arducam-16mp64mp-autofocus-camera

Thanks Edward. Is there a documentation update that describes the units used in the focus distance changes. It used to be the reciprocal in meters as far as I understood. i.e
–lens-position 5
would have been 1/5 (0.2m)

What are the numbers in relation to now?

@DaveMcD

The scope has been changed, please see my post below for details:

In fact, this value is used to control the size of the motor current.
For all the updates later, I will write them in the document, and it will get better and better.

Thanks Edward. Is there any description though of how those numbers correspond to real world distances?

I get that they map from one value to the other but I can’t find info on what either of those numbers are measuring.

@DaveMcD

Sorry, we don’t have an accurate correspondence table yet.
At present, if you want to know the specific correspondence, you can only measure it yourself.
This may be a plan we need to do in the future.

Apologies, but I dont find that to be a particular useful answer.

How would you suggest I derive what numerical values I need to enter into --lens-position to match a real world distance from the lens?

Your link to the other discussion mentions remapping values in a Json file but there isn’t an indication of what we’d be remapping from and to. Perhaps it makes immediate sens to software developers familiar with the source etc but for users expecting to be able to use the devices very of little of that makes sense.

Is it expected to use lens position that we need to modify that Json file and remap it? We’d need to know what those values represet in practical terms to the lens to know what we’re modifying and why?

Say I stick with the Json values you mentioned [0.0, 0 ,15.0,1023]

One explanation I can guess is that I have float values between 0-15 to play with and the device is really using 0 - 1023 potential distance steps it can make? so 0 being infinity focus distance ( most cameras this usually means after 10m or so it stops being relevant)? 15 being the closest focus distance?

If this is the case then 0 would equal 10,000cm and 15 probably ~15 cms closest focus. So my math might be off here but my calcs say that a focus distance of 1 would be 65.7cm

I certainly wasnt getting feasible results like this when I tested using 0.12 earlier this week. All the focus values seeimgly did very little and I ended up downgrading to 0.11

If my assumptions about how these values work is wrong then if you could indicate a practical example using correct values then that would be very useful.

@DaveMcD

I understand what you mean.
We are also currently working on the specific measurement method, I would say this is an ongoing research, please be patient, if there is an update, I will reply in this thread.

I’ve been pulling my hair out over the past week trying to get these cameras to work. Is it safe to assume that following the instructions in Edwards latest update link is the path to Arducam nirvana? After screwing around with this and not getting any results I plugged in a USB webcam and it came right up. Pretty strong argument for ditching Arducam altogether and going with a standard webcam. In fairness I am not a programmer so I don’t understand a lot of what is being said here. However, I am certainly able to type command lines into my Pi and would REALLY like to get this working.

1 Like

Link to another thread where I’ve listed the working and broken commands I’ve experimented with and my notes on those.

@RavenOne

Please don’t worry, you can talk about where you have a problem in detail so that I can help you find a problem. Or I can help you solve this problem remotely, or if you want, we can also have a meeting to talk about, please give our products more patient and trust.

@DaveMcD

I saw you said in your post that you cleared the buffer manually, how did you do it, can you share it? Thank you.