raspberrypi / picamera2

New libcamera based python library
BSD 2-Clause "Simplified" License
814 stars 173 forks source link

[SUGGESTION][COMPATIBILITY] Make pip installing picamera2 work in different python versions with pyenv #964

Open Zanz2 opened 6 months ago

Zanz2 commented 6 months ago

I think it would be a good idea to put more attention to making picamera2 with libcamera python wrappers work also on other versions of python. I have bought a pi5, camera module 3 and google coral usb TPU all in the hopes that these will work on the newest hardware. Found out today that google coral only supports python 3.9, pi5 comes with python 3.11 out of the box, and since it has libcamera 0.2 it is impossible to pip install picamera2 in a separate python 3.9 environment.

Maybe i dont understand the proble, but is the only thing we would need not just python 3.9 wrappers/bindings for libcamera? Is there a plan to create this, or a good reason that we dont have them yet? The pi5 is supposed to be THE SBC for computer vision.

What i've tried and given up on:

davidplowman commented 6 months ago

Yes, this is all more difficult than you might expect because different libcamera versions are not binary compatible and so, if you're not using the standard latest versions that we distribute, you have to build everything to match carefully from scratch. Have you looked at the instructions here?

@will-v-pi Is there anything you'd add to those instructions for someone trying to build for Python 3.9 on a Pi 5?

will-v-pi commented 6 months ago

It should work for our November release of libcamera - I'm just making an update for the new release last week, as the vendor controls need to be manually defined in pylibcamera

will-v-pi commented 6 months ago

New release made, and tested with python3.9, so those instructions should work fine

Zanz2 commented 6 months ago

Installing does work, which is great, but if i import it (picamera2) inside a python script, i get an error that says no module named libcamera? Im guessing this is because libcamera is built against the systems python version (3.11) and not 3.9? Do i need to build libcamera from source inside my virtualenv to make it work

will-v-pi commented 6 months ago

That shouldn't be necessary, as the rpi-libcamera pip package builds the python bindings for whatever python is active - what's the output you get from running pip list inside your virtualenv?

Zanz2 commented 6 months ago

That shouldn't be necessary, as the rpi-libcamera pip package builds the python bindings for whatever python is active - what's the output you get from running pip list inside your virtualenv?

This is it, on python version 3.9 inside a venv:

(pycoral) zanza@5zero:~/picamera2/examples/tensorflow $ pip list
Package                      Version
---------------------------- --------------
absl-py                      2.1.0
astunparse                   1.6.3
av                           11.0.0
cachetools                   5.3.2
certifi                      2024.2.2
charset-normalizer           3.3.2
flatbuffers                  20181003210633
gast                         0.5.4
google-auth                  2.28.1
google-auth-oauthlib         1.2.0
google-pasta                 0.2.0
grpcio                       1.62.0
h5py                         3.10.0
idna                         3.6
importlib-metadata           7.0.1
keras                        2.15.0
libclang                     16.0.6
Markdown                     3.5.2
MarkupSafe                   2.1.5
ml-dtypes                    0.2.0
numpy                        1.26.4
oauthlib                     3.2.2
opencv-python                4.9.0.80
opt-einsum                   3.3.0
packaging                    23.2
picamera2                    0.3.17
pidng                        4.0.9
piexif                       1.1.3
Pillow                       9.5.0
pip                          24.0
protobuf                     4.25.3
pyasn1                       0.5.1
pyasn1-modules               0.3.0
python-prctl                 1.8.1
requests                     2.31.0
requests-oauthlib            1.3.1
rsa                          4.9
setuptools                   58.1.0
simplejpeg                   1.7.2
six                          1.16.0
tensorboard                  2.15.2
tensorboard-data-server      0.7.2
tensorflow-io-gcs-filesystem 0.36.0
termcolor                    2.4.0
tflite-runtime               2.15.0
typing_extensions            4.9.0
urllib3                      2.2.1
v4l2-python3                 0.3.3
Werkzeug                     3.0.1
wheel                        0.42.0
wrapt                        1.14.1
zipp                         3.17.0
(pycoral) zanza@5zero:~/picamera2/examples/tensorflow $ python
Python 3.9.18 (main, Feb 23 2024, 10:11:25) 
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import picamera2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/zanza/.pyenv/versions/pycoral/lib/python3.9/site-packages/picamera2/__init__.py", line 3, in <module>
    import libcamera
ModuleNotFoundError: No module named 'libcamera'
>>> exit()
will-v-pi commented 6 months ago

It looks like you haven't actually install rpi-libcamera and rpi-kms - you need to run these instructions from within your virtual environment, as detailed in the forum post

pip install --upgrade pip
pip install wheel
pip install rpi-libcamera rpi-kms picamera2
Zanz2 commented 5 months ago

Thanks, that does work, do you know if the GUI also works? I cannot use the preview because i cant build pyqt5, but maybe i should open a separate issue for this.

AlgorithmicAce commented 5 months ago

It looks like you haven't actually install rpi-libcamera and rpi-kms - you need to run these instructions from within your virtual environment, as detailed in the forum post

pip install --upgrade pip
pip install wheel
pip install rpi-libcamera rpi-kms picamera2

@will-v-pi I have problems when it shows "Preparing metadata (pyproject.toml)". This is the output: ~~~`Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting picamera2 Downloading https://www.piwheels.org/simple/picamera2/picamera2-0.3.17-py3-none-any.whl (76 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.4/76.4 kB 10.9 kB/s eta 0:00:00 Collecting rpi-libcamera Downloading rpi_libcamera-0.1a3.tar.gz (9.7 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error

× Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [19 lines of output]

× Encountered error while generating package metadata. ╰─> See above for output.

note: This is an issue with the package mentioned above, not pip. hint: See above for details.`~~~

But, I have tried to look for the meson-log.txt but I can't find it anywhere, not in the /tmp or in the ~/.pyenv, please let me know how I can fix this issue. I really need to make the picamera2 work in pyenv

AlgorithmicAce commented 5 months ago

@will-v-pi I tried installing rpi-kms and picamera2 using pip, and I was able to do it, but I have problems installing libcamera

philipperemy commented 5 months ago

Same. That's probably because of the virtual env.

philipperemy commented 5 months ago

#https://github.com/raspberrypi/picamera2/issues/503

AlgorithmicAce commented 5 months ago

Same. That's probably because of the virtual env.

you can install it if you're using the same python version inside venv as the system python version, but in my case, system Python is (3.11), version inside venv is (3.9, I have to use <3.10 because of dependancy issues). So, I can't use the --system-site-packages flag. I need a way to install picamera2 inside different version of Python inside pyenv

will-v-pi commented 5 months ago

@AlgorithmicAce That's odd, it looks like the script to clone libcamera is failing when installing rpi-libcamera. Could you clone the pylibcamera repository and try running the command ./clone-libcamera.sh https://github.com/raspberrypi/libcamera.git v0.2.0+rpt20240215 pypatch from inside that repository, as that should give you more output for debugging?

AlgorithmicAce commented 5 months ago

./clone-libcamera.sh https://github.com/raspberrypi/libcamera.git v0.2.0+rpt20240215 pypatch

This is the output


~/pylibcamera/libcamera ~/pylibcamera
Note: switching to 'v0.2.0+rpt20240215'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 075b54d5 RASPBERRYPI ONLY: Add a Github workflow to generate release tarballs
~/pylibcamera````
AlgorithmicAce commented 5 months ago

@will-v-pi I was trying to import libcamera in my pyenv after cloning the repo and running the command, it worked, but when I'm trying to import picamera2, I'm getting the following output:

>>> import picamera2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/pi/.pyenv/versions/myproject/lib/python3.9/site-packages/picamera2/__init__.py", line 5, in <module> from .configuration import CameraConfiguration, StreamConfiguration File "/home/pi/.pyenv/versions/myproject/lib/python3.9/site-packages/picamera2/configuration.py", line 1, in <module> from .controls import Controls File "/home/pi/.pyenv/versions/myproject/lib/python3.9/site-packages/picamera2/controls.py", line 4, in <module> from libcamera import ControlType, Rectangle, Size ImportError: cannot import name 'ControlType' from 'libcamera' (unknown location)

will-v-pi commented 5 months ago

./clone-libcamera.sh https://github.com/raspberrypi/libcamera.git v0.2.0+rpt20240215 pypatch

This is the output ...

The output from that command looks correct, and I have not been able to replicate the issue (using a python 3.9 virtual environment created with pyenv, on Raspberry Pi OS Bookworm), so I would suggest cleaning your virtual environment and any caches pyenv uses, and trying the installation again (possibly also with --no-cache in the pip commands). If that doesn't work then try from a clean installation of Raspberry Pi OS

AlgorithmicAce commented 5 months ago

@will-v-pi Hi, I haven't tried from Pi 5 but I have tried from my Pi 4 using Bookworm and it works, thanks. Could you guide me to any links that will help me install PyQt5 in the virtual environment? I will try again later with the Pi 5. Thank you so much

will-v-pi commented 5 months ago

@AlgorithmicAce I've had success running

sudo apt install qtbase5-dev
pip -v install --config-settings --confirm-license= pyqt5

but if that doesn't work you'll need to look at the PyQt5 documentation (which is where I got that command from)

AlgorithmicAce commented 5 months ago

@will-v-pi Hi, I have install PyQt5, yet I am not seeing any output video in a window when I'm running inside the virtual machine, any idea how to fix it?