ruha.camera icon indicating copy to clipboard operation
ruha.camera copied to clipboard

Update code for Bookworm

Open earboxer opened this issue 1 year ago • 1 comments

Trying to use picamera on bookworm fails:

>>> from picamera import PiCamera
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/picamera/__init__.py", line 72, in <module>
    from picamera.exc import (
  File "/usr/lib/python3/dist-packages/picamera/exc.py", line 41, in <module>
    import picamera.mmal as mmal
  File "/usr/lib/python3/dist-packages/picamera/mmal.py", line 49, in <module>
    _lib = ct.CDLL('libmmal.so')
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libmmal.so: cannot open shared object file: No such file or directory

now that I have a raspberry pi camera, I'm getting this error, so I'm not completely sure how to set it up in bookworm.

[0:23:50.367964185] [2423]  INFO Camera camera_manager.cpp:284 libcamera v0.2.0+46-075b54d5
Preview window unavailable
ERROR: *** no cameras available ***

(Presumably if I use some older version of raspberry pi OS, it will work?)

earboxer avatar Apr 07 '24 00:04 earboxer

[after a good amount of tinkering,] I have a workable proof of concept running in bookworm:

Edit config.txt

Edit your config.txt file, adding the dtoverlay for your screen * For my screen, I added dtoverlay=fbtft,st7789v,dc=25,cs=8,dc_pin=25,cs_pin=8,dc-gpios=25,height=240,width=240,cs-gpios=8 * (you should also have camera_auto_detect=1) * on reboot, you should have a /dev/fb1 device that appears eventually. You test this using con2fbmap 1 1, which will show your shell on that screen, but that's optional.

Python script

Have this python script (you need picamera2, and python3-opencv)

#!/usr/bin/env python3

# For YUV420/YUYV to RGB, do
# sudo apt install python3-opencv
from picamera2 import Picamera2, Preview
from gpiozero import Button
import time
picam2 = Picamera2()
shutter = Button(24)
quitter = Button(23)

# probably could be optimized better...
camera_config = picam2.create_still_configuration(main={"size": (1920, 1080)}, lores={"size": (240, 240)}, display="lores")
# "fps":10.0 # TODO: howto set fps???
picam2.configure(camera_config)
#picam2.start_preview(Preview.QTGL)
#picam2.start_preview(Preview.DRM) # Failed to find...
picam2.start_preview(Preview.QT)
picam2.start()

num=0
while True:
    shutter.wait_for_press()
    now = time.localtime()
    picam2.capture_file(
        f"{now.tm_year}{now.tm_mon:02}{now.tm_mday:02}_{now.tm_hour:02}{now.tm_min:02}{now.tm_sec:02}_{num}.jpg"
    )
    num = num + 1

(there's some backstory about raspberry pi zero not having DRM support/GPU acceleration)

Launching it with correct env values

QT_QPA_PLATFORM=linuxfb:fb=/dev/fb1 ./picamera.py

(this tells QT that you want it to render to the linuxfb at /dev/fb1)

Caveats

  • It takes a little over a minute to launch the script
  • the framebuffer mouse appears in the top left corner
  • After you take a picture, it freezes for a few hundred milliseconds
  • the screen dims itself after a bit

But the latency and framerate are not horrible...

Sources/useful references

If you're having trouble setting up your fbtft overlay, use e.g. journalctl -t kernel --system -S 2024-04-19\ 21:00 on raspberry pi os to see kernel messages, which might include useful fbtft information.

Don't use raspberry pi OS 'lite' image. It will probably not connect to WiFi. (probably you want to start with the "Raspberry Pi OS with desktop", then uninstall/disable the desktop environment.)

maybe this stackoverflow is useful information?

earboxer avatar Apr 20 '24 14:04 earboxer