Hi, I can get the camera itself to work but I cannot get the focuser part to work at all. When I try to run the example code FocuserExample.py I get an error code: AttributeError: module ‘libcamera’ has no attribute ‘libcamera’. But when I run libcamera-hello or libcamera-still the camera works fine (picture just isn’t focused). This is an Arducam 5MP 1080p Pan Tilt Zoom (PTZ) camera module for Raspberry Pi, model B0167.
My setup:
Raspberry Pi Zero 2 W running DietPi v8.9.2
Sensor reported as ov5647 10-0036 (5MP OV5647)
Python 3.9
OpenCV 4.6.0 (opencv-python 4.6.0.66)
Working directory: /home/PTZ-Camera-Controller/
I followed this guide: Redirecting.... I run it with python3 FocuserExample.py. I honestly never got it to work. Maybe the problem is that I have python3 installed and the focuser scripts are written in python 2.x? Any idea what I’m doing wrong?
Thanks for the detailed info. Just to confirm the basics first: the B0167 PTZ module uses the OV5647 sensor over the standard libcamera stack, so libcamera-hello working is a good sign.
Let’s go step by step. A couple of questions:
After the AttributeError, did you make any changes? You mentioned libcamera-hello works again now, so can you describe the current state exactly - does FocuserExample.py launch at all?
Can you paste the current output of the script and any traceback you see?
Also, since you’re running Python 3.9, no need to switch to Python 2.x - the Arducam PTZ controller repo is Python 3. Let’s see the actual error first.
OK here’s the current status. libcamera-hello is working again. When I run FocuserExample.py the program starts and I can control the camera with the defined keyboard buttons, but there is no picture. So it’s half working - the key handling seems OK but the preview never shows up.
If I press ‘c’ to take a snapshot I get a cv2.imwrite error. Let me paste both errors.
Log 1 - no picture, GTK backend error while running FocuserExample.py:
Unable to init server: Could not connect: Connection refused
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 "/home/PTZ-Camera-Controller/RpiCamera.py", line 40, in capture_and_preview_thread
cv2.imshow(self.window_name,buf)
cv2.error: OpenCV(4.6.0) /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/highgui/src/window_gtk.cpp:635: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'
Traceback (most recent call last):
File "/home/PTZ-Camera-Controller/FocuserExample.py", line 213, in <module>
main()
File "/home/PTZ-Camera-Controller/FocuserExample.py", line 205, in main
curses.wrapper(draw_menu, camera, 1)
File "/usr/lib/python3.9/curses/__init__.py", line 94, in wrapper
return func(stdscr, *args, **kwds)
File "/home/PTZ-Camera-Controller/FocuserExample.py", line 180, in draw_menu
parseKey(k,focuser,auto_focus,camera)
File "/home/PTZ-Camera-Controller/FocuserExample.py", line 144, in parseKey
cv2.imwrite("image{}.jpg".format(image_count), camera.getFrame())
cv2.error: OpenCV(4.6.0) /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/imgcodecs/src/loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'
Any suggestion? Maybe OpenCV should be updated as well?
The two errors are related. The Can't initialize GTK backend in function 'cvInitSystem' from cv2.imshow means OpenCV can’t create a display window, and the !_img.empty() failure in imwrite means the frame returned by camera.getFrame() is empty - so nothing is actually being captured to save.
A few things to check. First, the way you’re running it matters a lot for GUI code. Can you confirm how you’re connected to the Pi right now?
I did pull the latest code from the PTZ camera repo, and I installed picamera2 (before that I only had picamera). After that FocuserExample.py starts importing picamera2, but now I hit a new problem after upgrading the apps. Here’s the error:
Log 3 - ModuleNotFoundError for picamera2 after upgrading apps:
Traceback (most recent call last):
File "/home/PTZ-Camera-Controller/FocuserExample.py", line 27, in <module>
from RpiCamera import Camera
File "/home/PTZ-Camera-Controller/RpiCamera.py", line 1, in <module>
from picamera2 import Picamera2
ModuleNotFoundError: No module named 'picamera2'
So at this point picamera2 is not found at all. It gets worse from here.
Then I tried to install the Arducam libcamera bits and got a broken repo error. Log 4:
W: Repository is broken: libcamera-dev:armhf (= 0.0.10) has no Size information
I think the previous version of libcamera was removed now, but the new one cannot install because of this size issue. And now libcamera-hello is not working either (symbol lookup error).
And now anything that imports picamera2 fails with an ImportError for Size. Log 5:
root@robot:/home/PTZ-Camera-Controller# python3 FocuserExample.py
Traceback (most recent call last):
File "/home/PTZ-Camera-Controller/FocuserExample.py", line 27, in <module>
from RpiCamera import Camera
File "/home/PTZ-Camera-Controller/RpiCamera.py", line 1, in <module>
from picamera2 import Picamera2
File "/usr/lib/python3/dist-packages/picamera2/__init__.py", line 1, in <module>
from .configuration import CameraConfiguration, StreamConfiguration
File "/usr/lib/python3/dist-packages/picamera2/configuration.py", line 1, in <module>
from .controls import Controls
File "/usr/lib/python3/dist-packages/picamera2/controls.py", line 4, in <module>
from libcamera import ControlType, Size, Rectangle
ImportError: cannot import name 'Size' from 'libcamera' (/usr/lib/python3/dist-packages/libcamera/__init__.py)
So it looks like picamera2 and the installed libcamera are out of sync.
That makes sense. The python libcamera bindings that picamera2 imports from and the libcamera build have to match. If you upgraded picamera2 but the libcamera python bindings are older (or were removed), you get exactly that cannot import name 'Size' and the broken-repo warning is the clue that the new libcamera-dev didn’t fully install.
Let’s get it back to a coherent state. Please run the Arducam install script for both dev and apps:
The second command upgrades apps so it stays in sync with dev. Then paste the output of pip3 list and pip3 list | grep libcamera so I can see what landed.
OK I ran the script for libcamera_dev and then also ./install_pivariety_pkgs.sh -p libcamera_apps to upgrade the apps together with dev. I also installed the dependencies from the updated Picamera2-with-Arducam guide. Here’s the list now:
root@robot:~# libcamera-hello
Made DRM preview window
[0:12:11.861838781] [5206] INFO Camera camera_manager.cpp:293 libcamera v0.0.0+3572-e13ccdd5
[0:12:11.873125604] [5208] ERROR CameraSensor camera_sensor.cpp:591 'ov5647 10-0036': Camera sensor does not support test pattern modes.
[0:12:11.942671229] [5208] ERROR DelayedControls delayed_controls.cpp:87 Delay request for control id 0x009a090a but control is not exposed by device /dev/v4l-subdev0
[0:12:11.943076177] [5208] INFO RPI raspberrypi.cpp:1386 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[0:12:11.944291854] [5206] INFO Camera camera.cpp:1029 configuring streams: (0) 1296x972-YUV420
[0:12:11.945134875] [5208] INFO RPI raspberrypi.cpp:764 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 - Selected sensor format: 1296x972-SGBRG10_1X10 - Selected unicam format: 1296x972-pGAA
So the sensor is being registered fine (ov5647). The camera works, but the focus still won’t. I have also attached a few fresh arducam-camera-bug-report outputs. Any suggestion? Maybe OpenCV shall be updated as well?
Good, the sensor side looks healthy - Registered camera ... ov5647@36 and the streams are configuring. So the hardware and driver are fine.
Back to the original preview error, because that’s the real blocker: Can't initialize GTK backend in function 'cvInitSystem' is OpenCV failing to open a display window. That almost always happens when the code is run without an actual screen/display session. Can I ask you directly - are you using SSH remotely to run this?
If yes, can you try running it with a monitor connected to the Pi instead of over SSH? cv2.imshow needs a real display, and a plain remote SSH shell usually can’t create the GTK window, which is also why getFrame() comes back empty and the snapshot imwrite fails with !_img.empty(). Let’s rule that out first. We’ll take a look here as well and get back to you.
Thanks for trying that. Quick clarifying question: do you still use SSH after you connect the monitor? Connecting a monitor alone won’t help if the command is still being launched from the SSH session - the GUI process is still tied to a remote session with no display.
You need to do it directly on the monitor side. That means logging in / opening a terminal on the Raspberry Pi itself (local console or desktop), with the monitor attached, and running python3 FocuserExample.py there - not over SSH. When it’s launched locally the GTK/OpenCV window can initialize and frames will actually come through, so getFrame() won’t be empty and the ‘c’ snapshot won’t fail with imwrite.
Can you confirm whether the monitor attempt was still over SSH? If so, retry the example purely locally and let us know.