How to correctly autofocus (trigger an autofocus) Hawkeye 64mp python

I’m reading through so many outdated forums posts and getting totally confused now.

In the current state of affairs, how do I trigger an autofocus cycle in picamera2, with the 64mp. I have this code now which does the job but is incredibly slow

def initialize_arducam_camera(dev_mode: bool = False) -> Picamera2:
    picam2 = Picamera2()

    preview_config = picam2.create_preview_configuration(main={"size": (1920, 1080)})
    picam2.configure(preview_config)
    picam2.start_preview()
    picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous,
                         "AfSpeed": controls.AfSpeedEnum.Fast,})
    picam2.start()
    return picam2


def take_arducam_picture(picam2, serial_number: int, zoom_factor=2.0) -> str:
    picam2.stop()
    config = picam2.create_still_configuration(main={"size": (9152, 6944), "format": "RGB888"}, buffer_count=1)
    picam2.configure(config)
    picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous, "AfSpeed": controls.AfSpeedEnum.Fast,})
    picam2.start()
    print('Autofocusing...')
    picam2.autofocus_cycle()
    print("Taking picture...")
    time.sleep(3)
    RGB888 = picam2.capture_array("main")
    cv2.imwrite("test.png", RGB888)
    print('Picture taken.')
    return "test.png"

Hi,
Suggesting that you install the 64mp camera driver by the command below:

./install_pivariety_pkgs.sh -l
./install_pivariety_pkgs.sh -p 64mp_pi_hawk_eye_kernel_driver

Little heads up that you need to connect the 64mp camera to the CAM 1 port on your Pi 5’s board.

Thank you I just did this. Should this increase focus speed?

Hi
Give it a try. This version of the camera driver provides the PDAF functionality that will speed up the focus to some extent.

Thank you it does seem to speed it up!
I’m finally getting the arducam to work in a way I can use it for my client. I’m getting hopeful again.

Because of the new driver and because it was working so well I tried to use switch_mode_and_capture() again.

    capture_config = picam2.create_still_configuration()
    autofocus_complete = False
    while not autofocus_complete:
        autofocus_complete = picam2.autofocus_cycle()
    picam2.switch_mode_and_capture_file(capture_config, filename)

However, it does not freeze but I get this error:

Is this the bug mentioned here? Troubleshooting - Arducam Wiki
Or am I doing something else wrong?

Traceback (most recent call last):
  File "/home/pinlock2/pinlock_production_video_monitor/pi_camera_utils.py", line 192, in <module>
    take_picture(picam, 1)
  File "/home/pinlock2/pinlock_production_video_monitor/pi_camera_utils.py", line 115, in take_picture
    picam2.switch_mode_and_capture_file(capture_config, filename)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1423, in switch_mode_and_capture_file
    return self.dispatch_functions(functions, wait, signal_function, immediate=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1319, in dispatch_functions
    return job.get_result() if wait else job
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/job.py", line 79, in get_result
    return self._future.result(timeout=timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/usr/lib/python3/dist-packages/picamera2/job.py", line 48, in execute
    done, result = self._functions[0]()
                   ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1416, in capture_and_switch_back_
    self.switch_mode_(preview_config)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1386, in switch_mode_
    self.configure_(camera_config)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1045, in configure_
    self.check_camera_config(camera_config)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 836, in check_camera_config
    self.check_stream_config(camera_config["raw"], "raw")
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 797, in check_stream_config
    raise RuntimeError("Unrecognised raw format " + format)
RuntimeError: Unrecognised raw format RGGB16_PISP_COMP1
Segmentation fault

I get the same with

    array = picam2.switch_mode_and_capture_array(capture_config, 'main')
    cv2.imwrite(filename, array, [cv2.IMWRITE_JPEG_QUALITY, 100])
Traceback (most recent call last):
  File "/home/pinlock2/pinlock_production_video_monitor/pi_camera_utils.py", line 194, in <module>
    take_picture(picam, 1)
  File "/home/pinlock2/pinlock_production_video_monitor/pi_camera_utils.py", line 115, in take_picture
    array = picam2.switch_mode_and_capture_array(capture_config, 'main')
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1595, in switch_mode_and_capture_array
    return self.dispatch_functions(functions, wait, signal_function, immediate=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1319, in dispatch_functions
    return job.get_result() if wait else job
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/job.py", line 79, in get_result
    return self._future.result(timeout=timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/usr/lib/python3/dist-packages/picamera2/job.py", line 48, in execute
    done, result = self._functions[0]()
                   ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1589, in capture_array_and_switch_back_
    self.switch_mode_(preview_config)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1386, in switch_mode_
    self.configure_(camera_config)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1045, in configure_
    self.check_camera_config(camera_config)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 836, in check_camera_config
    self.check_stream_config(camera_config["raw"], "raw")
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 797, in check_stream_config
    raise RuntimeError("Unrecognised raw format " + format)
RuntimeError: Unrecognised raw format RGGB16_PISP_COMP1
Segmentation fault

@Dion Is there a way I can actually confirm its using PDAF?

The autofocus still looks like CDAF like in this video, not like your PDAF example at all:

I had to reboot my device in order for it to activate. It seems a lot faster, but less accurate. I will continue testing

Is there a way to revert back to CDAF? I’m getting terrible results with PDAF. yes the speed is much better but the focus is worse

Hi,
It is easy.
Open a new terminal, and run the command LIBCAMERA_NOTPARSE_EMBEDDED_DATA=1
Heads up that this command only works on the terminal where it runs.

Okay thank you. If I would use this, would it be permanent (after boot?) And how do I return back if needed?

Follow-up question. Does the arducam-64mp support autofocus-window settings? It feels like it ignores them. Can you maybe explain to me, or someone else, how to ues it properly in libcamera-still for example.

This is my current command

libcamera-still -t 7000 --autofocus-mode continuous --exposure sport -n --autofocus-window 0.5,0.5,0.1,0.1 -o test.jpg

It sitll focuses on things out of the centre of the image

Hi,
It is not permanent. Just exiting with the terminal where this command runs, it means that when you close the terminal or reboot your Pi, the libcamera will go back to the PDAF.

It does not seem to work. Is there a way to set this in picamera2 as well?