Arducam 16 MP autofocus UVC cam, settings focus not working

1.Which seller did you purchase the product(s) from?
UCTRONICS
2.The Model number of the product(s) you have purchased?
B0290
3.Which Platform are you using the product(s) on?
Jetson Xavier NX
4.Which instruction are you following?

5.Has your product ever worked properly?
Yes
6.What problems are you experiencing?
Hello,

I try to set a fixed focus and exposure time to my camera (16MP, USB-UVC, Autofocus).

The problem is when I run the script the first time, the image is OK. But if I run it again (and again), I get a blurry image. The only way to get back a good image with the correct focus is to change a little the focus value (for example 350->351) before running again the script.

How can I avoid that?

What am I doing wrong?

Here the script:

import numpy as np
import cv2
import time
import os
from datetime import datetime

cap = cv2.VideoCapture(0, cv2.CAP_V4L2)

Set MJPG Mode

fourcc = cv2.VideoWriter.fourcc(*“MJPG”)
cap.set(cv2.CAP_PROP_FOURCC, fourcc)

Set resolution

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 3264)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 2448)

Set exposure time

#cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
cap.set(cv2.CAP_PROP_EXPOSURE, 30)

Set focus

#cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
cap.set(cv2.CAP_PROP_FOCUS, 350) # need to change before each run of the script

time.sleep(1)

Capture frame

ret, frame = cap.read()

filename = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
cv2.imwrite(filename+’.jpg’, frame)

cap.release()

Thanks in advance!

Daniel
7.What attempts at troubleshooting have you already made?

8.How would you like us to help you?

Somebody here?

Please…

Hi,

B0290 is out of stock, we will test it for you as soon as possible when the production department produces the camera in stock. Until then, please be patient.

Hi,

I tested it according to the code you provided, but an error was reported when the program was executed. I think it may be a version problem.
The python version I currently use is 2.7.16
OpenCV version is 3.2.0
Can you provide a detailed test environment? For example, test code files, software version and other information.

 

Hi,

So I run it on Ubuntu (on a Jetson Xavier NX, fully updated)

I updated OpenCV to 4.5 and I use Python 3.

On Mac OS, the script doesn’t change camera settings…so unusable

Thanks

import numpy as np

import cv2

import os

import time

from datetime import datetime

 

cap = cv2.VideoCapture(0, cv2.CAP_V4L2)

 

Set MJPG Mode

fourcc = cv2.VideoWriter.fourcc(*“MJPG”)

cap.set(cv2.CAP_PROP_FOURCC, fourcc)

 

Set Brightness

cap.set(cv2.CAP_PROP_BRIGHTNESS, 0)

 

Set Contrast

cap.set(cv2.CAP_PROP_CONTRAST, 32)

 

Set resolution

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 4656)

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 3496)

 

Set exposure time

cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)

cap.set(cv2.CAP_PROP_EXPOSURE, 50)

 

Set focus

cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)

focus = 445

if cap.get(cv2.CAP_PROP_FOCUS) != focus:

cap.set(cv2.CAP_PROP_FOCUS, focus)

else:

cap.set(cv2.CAP_PROP_FOCUS, focus+1)

to change a each time the focus -> but still don’t work

 

Set buffer (so read the most recent frame)

cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)

 

if not cap.isOpened():

print(“Cannot open camera”)

exit()

 

To emtpy buffer …

for i in range(10):

ret, frame = cap.read()

 

while(True):

Capture frame

#ret, frame = cap.read() # to empty buffer

cap.grab() # to empty buffer

ret, frame = cap.read()

if not ret:

print(“Can’t receive frame (stream end?). Exiting …”)

break

 

filename = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")

cv2.imwrite(filename+’.jpg’, frame)

 

inp = input("Press enter to take next frame or q: ")

if inp == ‘q’:

break

 

cap.release()

Hi,

I have completely tested the code you provided. When the focus is set to a value that can focus clearly, each time the script is restarted, a picture is taken and saved. In the end, the clarity of each picture is exactly the same.