penk / ruha.camera

3D Printable Retro-style Raspberry Pi HQ Camera
https://ruha.camera
MIT License
477 stars 45 forks source link

Update code for Bookworm #13

Open earboxer opened 5 months ago

earboxer commented 5 months ago

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 commented 5 months ago

[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

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

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?