Skip to content

[BUG] AfMode does not work as expected in Continuous mode #1272

Open
@ldct-polemos

Description

@ldct-polemos

Describe the bug
When you start your script with the camera in continuous autofocus mode, it correctly focuses on an object placed about one meter away. If you then place a smaller, labeled object very close to the lens (around 10 centimeters), the camera successfully shifts its focus to the near object. However, if you quickly remove the close object, the camera fails to refocus on the original object at one meter. The lens appears to remain locked in position as if it is still trying to focus at the previous, short distance, so the distant object stays blurry.

If you manually change the autofocus mode to manual and then back to continuous, the camera immediately refocuses on the distant object. The same problem happens in reverse as well: if the camera is focused up close and you quickly introduce a distant object, the focus remains stuck until you toggle the autofocus mode. It seems that fast changes in the scene or the sudden removal of an object can “break” the continuous autofocus, causing the focus mechanism to become unresponsive until the autofocus mode is reset.

Expected behaviour
The continuous AfMode should continuously focus.

To reproduce
Use this script to switch to Continuous or Manual at runtime after adding / removing a closer object to the scene.

from picamera2 import Picamera2, Preview
from libcamera import controls
import time

picam2 = Picamera2()

video_config = picam2.create_video_configuration(
    main={"size": (1920, 1080)},
    controls={"FrameRate": 20.0}
)
picam2.configure(video_config)
picam2.start_preview(Preview.QTGL)

# Set initial autofocus mode and speed
picam2.set_controls({
    "AfMode": controls.AfModeEnum.Continuous,
    "AfSpeed": controls.AfSpeedEnum.Normal  # or controls.AfSpeedEnum.Fast
})

picam2.start()

print("Press 'm' + Enter to set autofocus to MANUAL")
print("Press 'c' + Enter to set autofocus to CONTINUOUS")
print("Press 'q' + Enter to quit")

while True:
    user_input = input("Enter command (m/c/q): ").strip().lower()
    if user_input == 'm':
        picam2.set_controls({"AfMode": controls.AfModeEnum.Manual})
        print("Set AfMode to MANUAL")
    elif user_input == 'c':
        picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
        print("Set AfMode to CONTINUOUS")
    elif user_input == 'q':
        print("Exiting...")
        break
    else:
        print("Invalid input. Use 'm', 'c', or 'q'.")

picam2.stop_preview()
picam2.close()


Hardware :
RPI4, Camera V3

Additional context
Sw versions:
Picamera2: Version: 0.3.27-1
libcamera: Version: 0.5.0+rpt20250429-1
kernel: 6.12.25+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.25-1+rpt1 (2025-04-30) aarch64 GNU/Linux
distro: bookworm

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions