How is it possible to get autofocus working along with Picamera2 for IMX519

@Edward

This is every command I have run from the point of the fresh install of RaspberryPi 64-bit OS:

   1  dpkg -l | grep libcamera
    2  sudo apt install -y python3-kms++
    3  sudo apt install -y python3-pyqt5 python3-prctl libatlas-base-dev ffmpeg
    4  sudo pip3 install numpy --upgrade
    5  sudo pip3 install picamera2==0.3.2
    6  sudo raspi-config
    7  sudo apt install vim
    8  sudo vim /boot/confi
    9  sudo vim /boot/config.txt 
   10  sudo reboot now
   11  git clone https://github.com/raspberrypi/picamera2.git
   12  cd picamera2/examples/
   13  cd ..
   14  git clone https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver.git
   15  ls
   16  cd picamera2/examples/
   17  ls
   18  python3 easy_video.py 
   19  cd ..
   20  ls
   21  cd Arducam-Pivariety-V4L2-Driver/
   22  sudo vim /boot/config.txt 
   23  sudo reboot now
   24  cd Arducam-Pivariety-V4L2-Driver/focus/
   25  python3 FocuserExample.py -d /dev/v4l-subdev1
   26  sudo apt remove libcamera0
   27  python3 FocuserExample.py -d /dev/v4l-subdev1
   28  python3 FocuserExample.py -d /dev/v4l-subdev0
   29  libcamera-still
   30  ls
   31  cd ..
   32  ls
   33  cd ..
   34  ls
   35  mkdir install
   36  cd install/
   37  wget -O install_pivariety_pkgs.sh https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/install_script/install_pivariety_pkgs.sh
   38  chmod +x install_pivariety_pkgs.sh 
   39  ./install_pivariety_pkgs.sh -p libcamera_dev
   40  ./install_pivariety_pkgs.sh -p libcamera_apps
   41  vim /boot/config.txt 
   42  cat /boot/config.txt 
   43  vim /boot/config.txt 
   44  sudo vim /boot/config.txt 
   45  ls /dev/video/*
   46  ls /dev/video
   47  ls /dev/video*
   48  libcamera-hello 
   49  history
   50  history > boot1.txt
   51  sudo reboot now
   52  libcamera-hello 
   53  sudo vim /boot/config.txt 
   54  sudo reboot now
   55  libcamera-hello 
   56  cd Arducam-Pivariety-V4L2-Driver/focus/
   57  python3 FocuserExample.py -d /dev/v4l-subdev1
   58  ls /dev
   59  ls /dev/v4*
   60  python3 FocuserExample.py -d /dev/v4l-subdev0
   61  history
   62  libcamera-hello --autofocus
   63  cd ..
   64  ls
   65  cd src/
   66  ls
   67  cd ../patchs/
   68  ls
   69  cd ../tools/
   70  ls
   71  python3 keyboard_ctrl_tools.py 
   72  cd ..
   73  ls
   74  cd focus/
   75  ls
   76  python3 Focuser
   77  python3 FocuserExample.py 
   78  python3 FocuserExample.py -d /dev/media3
   79  python3 FocuserExample.py -d /dev/v4l-subdev0
   80  python3 FocuserExample.py -d /dev/v4l-subdev1
   81  python3 FocuserExample.py -d /dev/v4l-subdev2
   82  python3 FocuserExample.py -d /dev/v4l-subdev0
   83  ./install_pivariety_pkgs.sh -p imx519_low_speed_kernel_driver
   84  cd ..
   85  ld
   86  ls
   87  cd install/
   88  ls
   89  ./install_pivariety_pkgs.sh -p imx519_low_speed_kernel_driver
   90  ./install_pivariety_pkgs.sh -p imx519_kernel_driver
   91  libcamera-hello --autofocus
   92  v4l2-ctl -l -d /dev/v4l-subdev0
   93  ls
   94  ./install_pivariety_pkgs.sh -p imx519_low_speed_kernel_driver
   95  ./install_pivariety_pkgs.sh -p imx519_kernel_driver_low_speed
   96  libcamera-hello 
   97  libcamera-hello -t 1000000000000
   98  ls
   99  cd picamera2/
  100  ls
  101  cd examples/
  102  ls
  103  python3 easy_video.py 
  104  mkdir jp
  105  cp easy_video.py jp/
  106  cd jp/
  107  vim easy_video.py 
  108  python3 easy_video.py 
  109  ls ../
  110  ls
  111  cat easy_video.py 
  112  ls
  113  python3 easy_video.py 
  114  vim easy_video.py 
  115  cat easy_video.py 
  116  python3 easy_video.py 
  117  sudo reboot now
  118  sudo apt remove python3-libcamera
  119  clear

libcamera-hello --autofocus

Works now

@Edward

This is working now with Autofocus!

#!/usr/bin/python3

from picamera2 import Picamera2

picam2 = Picamera2()

picam2.set_controls({"AfTrigger":0})

# Record a 5 second video.
picam2.start_and_record_video("test.mp4", duration=5)

But I’m not sure I totally understand why… Which piece of what I was doing caused the issue?? When I tried to remove python3-libcamera it looked like it said that nothing was removed, but at the same time it seemed that after that things started working… Why would this be?

@trezero

The thing is, libcamera itself is written in C and C++. We packaged him as a dynamic link library so that users can use it in python, and python3-libcamera does the same thing.

Installing python3-libcamera will result in an override that prevents the use of our libcamera, and thus our specific functionality.

picamera2 calls the first layer of wrappers and then makes a new one that is more user friendly.

@Edward whats the proper way to get opencv working with this camera? I noticed that if I do this:


sudo pip3 install opencv-contrib-python==4.5.3.56

Then I end up getting this error

python easy_video.py

Could not write header for output file #0 (incorrect codec parameters ?): Immediate exit requested

If I uninstall opencv everything works again. My goal is to do OCR an utilize other tensor flow recognition utilities. Is there a specific version or method of installing opencv recommended?

@trezero

The data needs to be fetched with picamera2 and then processed with opencv.

If you have no problem with the usage, this is a question you can ask the official.

@Edward I’m not sure I follow your last answer. If the script you sent me requires opencv to be installed, how can this work when after installing opencv the very simple easy_video.py no longer works?

@trezero

easy_video.py

Can I see how it is written?
I can run and give you the version of my environment.

@Edward
#!/usr/bin/python3

from picamera2 import Picamera2

picam2 = Picamera2()

picam2.set_controls({“AfTrigger”:0})

Record a 5 second video.

picam2.start_and_record_video(“test.mp4”, duration=5)

Should be using opencv-python or opencv-contrib-python?

It seems that the contrib version has a lot of good stuff in it that most people recommend but both seem to cause issues.

@trezero

This is what I use without any problems.

opencv_python==4.6.0.66
picamera2==0.3.2

@Edward I managed to get opencv_python installed. When I try to run python3 real_time.py --model mobilenet_v2.tflite --label coco_labels.txt

I get this error:

[194:16:09.999584747] [85494]  WARN RPI raspberrypi.cpp:1274 Mismatch between Unicam and CamHelper for embedded data usage!
[194:16:10.000859887] [85494] ERROR DelayedControls delayed_controls.cpp:87 Delay request for control id 0x009a090a but control is not exposed by device /dev/v4l-subdev0
[194:16:10.001221340] [85494]  INFO RPI raspberrypi.cpp:1398 Registered camera /base/soc/i2c0mux/i2c@1/imx519@1a to Unicam device /dev/media3 and ISP device /dev/media0
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/dist-packages/picamera2/previews/qt_previews.py", line 22, in thread_func
    self.qpicamera2 = self.make_picamera2_widget(picam2, width=self.width, height=self.height, transform=self.transform)
  File "/usr/local/lib/python3.9/dist-packages/picamera2/previews/qt_previews.py", line 75, in make_picamera2_widget
    return QGlPicamera2(picam2, width=self.width, height=self.height, transform=self.transform)
  File "/usr/local/lib/python3.9/dist-packages/picamera2/previews/q_gl_picamera2.py", line 106, in __init__
    self.init_gl()
  File "/usr/local/lib/python3.9/dist-packages/picamera2/previews/q_gl_picamera2.py", line 142, in init_gl
    self.create_surface()
  File "/usr/local/lib/python3.9/dist-packages/picamera2/previews/q_gl_picamera2.py", line 136, in create_surface
    surface = eglCreateWindowSurface(self.egl.display, self.egl.config,
  File "/usr/lib/python3/dist-packages/OpenGL/platform/baseplatform.py", line 415, in __call__
    return self( *args, **named )
  File "/usr/lib/python3/dist-packages/OpenGL/error.py", line 230, in glCheckError
    raise self._errorClass(
OpenGL.raw.EGL._errors.EGLError: EGLError(
	err = EGL_BAD_ALLOC,
	baseOperation = eglCreateWindowSurface,
	cArguments = (
		<OpenGL._opaque.EGLDisplay_pointer object at 0x7f790017c0>,
		<OpenGL._opaque.EGLConfig_pointer object at 0x7f79001d40>,
		c_void_p(1),
		None,
	),
	result = <OpenGL._opaque.EGLSurface_pointer object at 0x7f78e09340>
)

@Edward

Hello, I made a post on github regarding an issue I’m experiencing: Error Attribute Libcamera · Issue #303 · raspberrypi/picamera2 · GitHub

I’ve followed all of the steps you have provided for trezero, but I ended up with the following error message:

AttributeError: type object 'libcamera._libcamera.ColorSpace' has no attribute 'Jpeg'

davidplowman whom I believe is a developer for picamera2 has indicated that the fix for this is to install python3-picamera2. When attempting to install it using:

sudo apt install -y python3-picamera2

I get the following message:

python3-picamera2 : Depends: python3-libcamera but it is not going to be installed
E: Unmet dependencies. Try ‘apt --fix-broken install’ with no packages (or specify a solution).

You mentioned to not install python3-libcamera as this can lead to conflict. I want to be able to write Python Script and utilize Arducam’s AutoFocus. Currently it looks like I have to pick one or the other. How should I proceed?

@trezero Did you by any chance get this to work?

@trezero

I’m sorry I took a long vacation, real_time. py seems to be the official code, you can submit a lssues at github.

@Alpha-Badger

try first:

sudo apt-get remove --purge python3-picamera2

If you want to use autofocus, you must use our libcamera.

I’ve followed all of the steps you have provided for trezero, but I ended up with the following error message:

We have recently updated libcamera, you need to update our libcamera to resolve this issue.

wget -O install_pivariety_pkgs.sh https://github.com/ArduCAM/Arducam-Pivariety-V4L2-Driver/releases/download/install_script/install_pivariety_pkgs.sh
chmod +x install_pivariety_pkgs.sh
./install_pivariety_pkgs.sh -p libcamera_dev
./install_pivariety_pkgs.sh -p libcamera_apps
1 Like

Is picam2.set_controls({“AfModeContinuous”:0}) working for anyone?

I’m getting a runtime error:

control AfModeContinuous is not advertied by libcamera

I can’t find reference to this error anywhere on the web

I did the recent libcamera update mentioned in the above reply and got this at the end:

W: Repository is broken: Libcamera-apps:armhf (= 0.0.9) has no Size information

Is this normal?

1 Like

Okay it looks like the afTrigger is working as a continuous auto focus but only refocuses with an object really close like 2" away.

Using:

picam2.set_controls({“AFTrigger”:1})

What does the The numeric after AFTrigger control?

1 Like

@ZEROTYPE

W: Repository is broken: Libcamera-apps:armhf (= 0.0.9) has no Size information
Don’t worry, we set the size the same internally.

picam2.set_controls({“AFTrigger”:1})

Should use picam2.set_controls({“AFTrigger”:0})

Official Explanation
https://libcamera.org/api-html/namespacelibcamera_1_1controls.html#a4e2c54ebb9dd200e458aea7632404f4a


1 Like

Interesting, it seems to be refocusing more when set to 1.

Have the AF modes been cut?

What controls are available through set_controls in your version of libcamera?

Is there any way to tune the auto focus through python?

@ZEROTYPE

Interesting, it seems to be refocusing more when set to 1.
Have the AF modes been cut?

This seems to be a bug , I will debug it.

What controls are available through set_controls in your version of libcamera?

You can print it out by following

picam2 = Picamera2()
controls = picam2.camera_controls
print(controls)

Is there any way to tune the auto focus through python?

What do you mean by that and can you elaborate on it? Are you trying to manually control the focal length?