PTZ does not work

Hello Arducam,

My PTZ does not work. python shows preview for 1 second then program exits. Connected to Pi 4, using Python 2.7.16. Everything seems to be connected okay.

Please advise. Thanks.


In order to help you debug it, I need detail error messages and your detail operation steps.


I had a difficult time getting this to work. It had a lot to do with OpenCV installation. Also when I run the code from the terminal, the import cv2 line fails. If I run the code in an IDE with terminal it works.


Great to hear you have solved your problem. For the openCV, which need X11 display system, you can’t run it in remote terminal.


I had that problem initially as I tried to run the python script using a remote connection to my PI. Once I used the connected monitor AND the Thonny Python IDE it worked. When I tried to run the python script from a terminal it said it couldn’t import CV2. I was using python3 though. Anything I run the autofocus script, the program results in error and it does not focus.

Hi @fisha462 ,

Have you tried python2.7? If the IDE works, it means that you have a python version that works, please try multiple versions.

By the way, if you still have problems please send us the error message.


My IDE is using python 3.7.3. Inside my IDE, I am using Numpy version 4.1.0 and OpenCV 1.16.2. I am able to run the except when I hit “Enter”:

When I try to run the, I get the following error:

TypeError: slice indices must be integers or None or have an index method on line 77:

“image = image[(height / 4):((height / 4) * 3),(width / 4):((width / 4)) * 3]”.

I did some research and it looks like you could do this in Numpy 1.11 but you can no longer in Numpy 1.12 and above.

Do you have any suggestions? I am trying to use this camera in conjunction with other code that will require never version of Python, OpenCV, and Numpy. I am a relative novice with Python but I am stubborn so I will continue to attempt to make it work. In the mean time, I will try to install OpenCV in Python 2, it looks like it takes a little bit of work to get it installed in that older version of Python.

Thanks for your help.



I got the code to run on Python3.7 but changing all of the ‘/’ to ‘//’ where its trying to slice the index. The code runs now but it doesn’t work very well. I imagine taking those floats away made it lose it’s level of accuracy?



Changing the’/’ to’//’ is the correct approach, this should not affect accuracy.
This focusing procedure is very simple, its accuracy is not very high, is there a big difference between you before and after modification?


I was never able to run the code in Python 2 so I can’t comment on the performance of the auto-focus code. I have observed that the camera usually goes past the point of focus when I run the code in Python 3. About 50% of the time I get a blurry image after running. I have tried it with different levels of zoom and tried pointing the camera at different objects at different lengths away. Once the code is finished I can always manually adjust the focus, it seems like the auto-focus code doesn’t adjust the zoom. I should ask what is the intent of the code? Was it to give your customers a pure example that wasn’t mean’t to be used in a real application? In other words can that code be improved upon? It seems to me that it can be improved. I am having a lot of fun doing this, I hope my questions are engaging for you as well!


I should ask what is the intent of the code?
There are several intentions to provide this code: 1. Explain how to control the hardware 2. Verify if there is a problem with the hardware 3. Realize the simplest auto-focus algorithm, show the auto-focus workflow
In other words can that code be improved upon? It seems to me that it can be improved.
If you think this code meets your application requirements, you can use it in actual applications, and if it does not meet your requirements, you can also improve it.

The algorithm implemented in this code is very basic, and it has a lot of room for improvement. But these improvements need to be implemented by the user.



Thanks, that helps a lot. I naively thought I could use the code “out of the box”. The specification of my project will dictate what the auto-focus does in the end. My intent is to use the setup to do some object identification and tracking along with a function that will zoom in on an object and get a detailed picture. It seems that for me to get the focus to do what I want I will need “tell” the camera what area to focus on…it seems like the auto-focus code that ArduCam has provided generally try’s to focus on the entire image, would you say that is a fair assessment?


Hi @fisha462 ,

The code we provide does not focus on the entire image, you can adjust the focus area by modifying this line of code.