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

change parameters, make adjustments.

Specifically contrast(if that’s what it’s using) settings that trigger the auto focus or how it is using opencv.

idk seems like it would be alot more useful for everyone if that was possible.

@ZEROTYPE

I confirm this function, 0 is single trigger focus, 1 is continuous autofocus.

There is no interface to adjust, you can only use set_controls. In python, you can’t use opencv to control the camera, you can only use picamera2 to get the data, and then use opencv, you can’t use opencv to control directly.

1 Like

Shouldn’t it comply libcamera AfModeEnum and AfTriggerEnum?

https://libcamera.org/api-html/namespacelibcamera_1_1controls.html

1 Like

@AnotherDredd

We use aftrigger to trigger autofocus, but since aftrigger has only two states, we set start to single autofocus and canncel to continuous autofocus.

1 Like

Good thing I messed with it.

Should really clear that up in documentation somewhere.

In the python for the 16mp autofocus, picamera was using PiRGBarray module but picamera2 doesn’t have it?

import cv2 #sudo apt-get install python-opencv
import numpy as py
import os
import time
import smbus
bus = smbus.SMBus(0)
try:
import picamera
from picamera.array import PiRGBArray
except:
sys.exit(0)

def focusing(val):
value = (val << 4) & 0x3ff0
data1 = (value >> 8) & 0x3f
data2 = value & 0xf0
# time.sleep(0.5)
print(“focus value: {}”.format(val))
# bus.write_byte_data(0x0c,data1,data2)
os.system(“i2cset -y 0 0x0c %d %d” % (data1,data2))

def sobel(img):
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
img_sobel = cv2.Sobel(img_gray,cv2.CV_16U,1,1)
return cv2.mean(img_sobel)[0]

def laplacian(img):
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
img_sobel = cv2.Laplacian(img_gray,cv2.CV_16U)
return cv2.mean(img_sobel)[0]

def calculation(camera):
rawCapture = PiRGBArray(camera)
camera.capture(rawCapture,format=“bgr”, use_video_port=True)
image = rawCapture.array
rawCapture.truncate(0)
return laplacian(image)

if name == “main”:
#open camera
camera = picamera.PiCamera()

    #camera.awb_gains=4
    #camera.exposure_mode='off'	
    #camera.awb_mode='fluorescent'
    #open camera preview
camera.start_preview()
#set camera resolution to 640x480(Small resolution for faster speeds.)
camera.resolution = (640, 480)
    time.sleep(0.1)
print("Start focusing")

max_index = 10
max_value = 0.0
last_value = 0.0
dec_count = 0
focal_distance = 10


    

while True:
    #Adjust focus
	focusing(focal_distance)
	#Take image and calculate image clarity
	val = calculation(camera)
	#Find the maximum image clarity
	if val > max_value:
		max_index = focal_distance
		max_value = val
		
	#If the image clarity starts to decrease
	if val < last_value:
		dec_count += 1
	else:
		dec_count = 0
	#Image clarity is reduced by six consecutive frames
	if dec_count > 6:
		break
	last_value = val
	
	#Increase the focal distance
	focal_distance += 15
	if focal_distance > 1000:
		break

#Adjust focus to the best
focusing(max_index)
time.sleep(1)
#set camera resolution to 2592x1944
camera.resolution = (1920,1080)
#save image to file.
camera.capture("test.jpg")
print("max index = %d,max value = %lf" % (max_index,max_value))
#while True:
#	time.sleep(1)
	
camera.stop_preview()
camera.close()

@Edward
Hi Edward,

I used:

Which executed fine. However, when I executed :

I was left with the following message:
W: Repository is broken: libcamera-apps:arm64 (= 0.0.9) has no Size information

The problem has persisted and I am still left with the following error message when attempting to run my app. It reads:

rhinosd@raspberrypi:~/Desktop/rhinocodes/svp2codefiles/aa_picamera2main $ python pcam2_qapp.py
Traceback (most recent call last):
  File "/home/rhinosd/Desktop/rhinocodes/svp2codefiles/aa_picamera2main/pcam2_qapp.py", line 6, in <module>
    from picamera2 import Picamera2
  File "/usr/local/lib/python3.9/dist-packages/picamera2/__init__.py", line 5, in <module>
    from .picamera2 import Picamera2, Preview
  File "/usr/local/lib/python3.9/dist-packages/picamera2/picamera2.py", line 45, in <module>
    class Picamera2:
  File "/usr/local/lib/python3.9/dist-packages/picamera2/picamera2.py", line 458, in Picamera2
    def create_preview_configuration(self, main={}, lores=None, raw=None, transform=libcamera.Transform(), colour_space=libcamera.ColorSpace.Jpeg(), buffer_count=4, controls={}, display="main", encode="main"):
AttributeError: type object 'libcamera._libcamera.ColorSpace' has no attribute 'Jpeg'
rhinosd@raspberrypi:~/Desktop/rhinocodes/svp2codefiles/aa_picamera2main $ 

I feel like I am quite close to resolving this… Is there anything I could be missing? Thanks Edward.

1 Like

He chimed in on that error when I brought it up:

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

1 Like

Thank you, yeah I noticed that. I just wanted to be as comprehensive as possible in troubleshooting this. Are you able to use Autofocus within Python?

Yes, with:

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

I think my current issue is the light levels I’m trying to test it in are quite dim and have yet to do an outside AF test.

@ZEROTYPE

Sorry, I haven’t written picamera before, I read your code and what PiRGBarray seems to do is to take the image data.

There are also functions in picamera2 to get the data out.

This is a demo I wrote

1 Like

@Alpha-Badger

Please try

python3 -m pip install picamera2 --upgrade

That wasn’t my python that was from the Arducam site for the 16mp AF:

thanks ill give that a go

Is it normal, that it takes 2-3s to focus after calling

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

Is there anyway to speed up the process?

@Openscan

Can I look at the code you wrote?
It also takes time to start the camera, and you can add time to each step.

If you are referring to the time to focus clearly, then it is normal.
It needs to be tested at each focus point to calculate the value with the highest degree of clarity.

Okay so I got everything working and took it out for the first test drive.

Autofocus is working really well with only a few hiccups not triggering refocus but overall really well.

But now playing back the videos on VLC is the issue, low frame rate and choppy as hell.

The hawkeye forced us to move to bullseye and bullseye is having issues playing the videos on VLC because of the drivers?

@ZEROTYPE

It is true that vlc does not perform that well in Raspberry Pi, you can try other software such as gstreamer. The upgrade to bullseye is because following the changes in Raspberry Pi, the system is getting better day by day.

Tried to set up pi as bluetooth receiver, pulseaudio crashed lxpanel and pretty much ruined the gui.

Had to reimage sd card and I’m going thru the whole install process again but cmd

./install_pivariety_pkgs.sh -p 64mp_pi_hawk_eye_kernel_driver

is returning a cannot find corresponding package now wtf

Just tried again for a fresh image of bullseye 32bit

@Edward

Raspberry pi has changed something that is causing issues with the driver I confirmed this by booting up a sd that was imaged at the same time(oct 17th 22) as my last working sd image.

The newest image from raspberry pi imager is creating issues

@ZEROTYPE

Raspberry Pi just released a new kernel today, we are compiling and uploading the new one, and it is expected to be available tomorrow.

2 Likes