question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Raspberry Pi 4: Cannot import name 'Size' or 'ControlType' from libcamera depending on version of libcamera

See original GitHub issue

Describe the bug I can’t seem to import from picamera2 regardless of the libcamera version I’m using.

When I enable “Legacy Camera Support” in raspi-config, picamera2 fails to import ‘Size’ from libcamera. When I install a more recent libcamera, picamera2 fails to import ‘ControlType’.

I also checked the libcamera API documentation (https://www.libcamera.org/api-html/classes.html) and I don’t see a ControlType class?

Is there a recommended version of libcamera to install?

To Reproduce Raspberry Pi 4 (4 GB RAM model) Raspberry PI OS Lite (32 bit, Buster-base Release 2022-04-04)

Sample program:

from picamera2 import PiCamera
# That's all that's required

Install picamera2 with no gui according to workaround here: https://github.com/raspberrypi/picamera2/issues/184 In sudo raspi-config Interface Options -> Legacy Camera, enable Legacy Camera suport. Reboot

Run sample program. Note “cannot import name ‘Size’” error

Install libcamera from wheel: https://github.com/ArduCAM/RaspberryPi/blob/master/Motorized_Focus_Camera/python/libcamera-1.0.2-cp39-cp39-linux_armv7l.whl python3 -m pip install ./libcamera-1.0.2-cp39-cp39-linux_armv7l.whl

Run sample program. Note “cannot import name ‘ControlType’”

Expected behaviour importing PiCamera works

Console Output, Screenshots

With “legacy camera”

Traceback (most recent call last):
  File "/home/pi/RaspberryPi/Motorized_Focus_Camera/python/picam2.py", line 1, in <module>
    from picamera2 import PiCamera
  File "/home/pi/.local/lib/python3.9/site-packages/picamera2/__init__.py", line 1, in <module>
    from .configuration import CameraConfiguration, StreamConfiguration
  File "/home/pi/.local/lib/python3.9/site-packages/picamera2/configuration.py", line 1, in <module>
    from .controls import Controls
  File "/home/pi/.local/lib/python3.9/site-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)

With more recent libcamera:

Traceback (most recent call last):
  File "/home/pi/RaspberryPi/Motorized_Focus_Camera/python/./Autofocus.py", line 5, in <module>
    from RpiCamera import Camera
  File "/home/pi/RaspberryPi/Motorized_Focus_Camera/python/RpiCamera.py", line 10, in <module> 
    from picamera2 import Picamera2, Preview
  File "/home/pi/.virtualenvs/moto_cam/lib/python3.9/site-packages/picamera2/__init__.py", line 1, in <module>
    from .configuration import CameraConfiguration, StreamConfiguration
  File "/home/pi/.virtualenvs/moto_cam/lib/python3.9/site-packages/picamera2/configuration.py", line 1, in <module>
    from .controls import Controls
  File "/home/pi/.virtualenvs/moto_cam/lib/python3.9/site-packages/picamera2/controls.py", line 4, in <module>
    from libcamera import ControlType, Size, Rectangle
ImportError: cannot import name 'ControlType' from 'libcamera' (/home/pi/.virtualenvs/moto_cam/lib/python3.9/site-packages/libcamera.cpython-39-arm-linux-gnu

**Hardware :**
Raspberry Pi 4
Arducam 5MP (Repros with RaspiCam v1.3 as well)

**Additional context**
With Legacy Camera support enabled, 

$ sudo ldconfig -v | grep camera libcamera-base.so.0 -> libcamera-base.so.0.0.0

    libcamera.so.0 -> libcamera.so.0.0.0

With it disabled, but installing a wheel:

$ sudo ldconfig -v | grep camera $

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
shineworldcommented, Jul 12, 2022

This is my list of commands JUST after installing a clean Raspberry Pi4 Bulleyes 64-bit (64-bit is a MUST to have all to work):

> sudo apt-get update
> sudo apt-get upgrade
> sudo apt-get full-upgrade
> sudo reboot

> sudo nano /boot/config.txt

   for IMX219:
    # Automatically load overlays for detected cameras
    camera_auto_detect=0
    dtoverlay=imx219

  for IMX477:
    # Automatically load overlays for detected cameras
    camera_auto_detect=0
    dtoverlay=imx477

> sudo reboot

Use dmesg to know if all working fine:

> dmesg | grep imx
[    8.527510] imx477 10-001a: Device found is imx477
[    8.528280] imx477 10-001a: Consider updating driver imx477 to match on endpoints

At this point libcamera-apps are already installed and working:

> libcamera-hello --version
libcamera-apps build: 35266e84043f 07-07-2022 (17:59:45)
libcamera build: v0.0.0+3700-f30ad033

Follow activity to enable camera focus motor management (if camera support it because motorized):

> sudo modprobe i2c-dev
> sudo nano /boot/config.txt

Add at end:

    [all]
    dtparam=i2c_vc=on

> sudo reboot

Packages to install to have Picamera2, OpenCV working:

> sudo apt-get update
> sudo apt-get upgrade
> sudo apt-get autoremove
    
> sudo apt install -y python3-libcamera python3-kms++
> sudo apt install -y python3-pyqt5 python3-prctl libatlas-base-dev ffmpeg python3-pip
> pip3 install numpy --upgrade
> pip3 install picamera2
    
> sudo apt install -y python3-opencv
> sudo apt install -y opencv-data
1reaction
trezerocommented, Sep 2, 2022

@shineworld I have been fighting to get my camera working for the past 3 weeks and your instructions are the first and only ones that actually worked. Thank you SOO Much! I wish I could buy you a beer (or 5!). Truly appreciate you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Picamera2 : No module named 'libcamera'
I am running this example https://github.com/raspberrypi/picamera ... _server.py and i get the following error :
Read more >
libcamera is missing in Ubuntu aarch 64, raspberry pi
I installed libcamera using this command: sudo apt install libcamera-dev however, this error still there: ModuleNotFoundError: No module named ' ...
Read more >
How is it possible to get autofocus working along with ...
[BUG] Raspberry Pi 4: Cannot import name 'Size' or 'ControlType' from libcamera depending on version of libcamera · Issue #218 ...
Read more >
Camera not working in opencv but does on libcamera
from libcamera import ControlType, Size, Rectangle ImportError: cannot import name 'ControlType' from 'libcamera' ...
Read more >
[1/6] staging: Import the BCM2835 MMAL-based V4L2 camera ...
Uses 3.6.10 timestamping - Camera power based on use - Uses immutable input mode on video encoder This code comes from the Raspberry...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found