geerlingguy / 3d-printing

Documentation and tools for my 3D Printer setup.
MIT License
53 stars 4 forks source link

Get Camera Module 3 working on Pi 5 with OctoPrint #9

Open geerlingguy opened 3 weeks ago

geerlingguy commented 3 weeks ago

I'm setting up a new Pi 5 with OctoPrint—well, hopefully—but I've run into a snag. I have a Camera Module 3 attached, but the latest nightly (6-7-2024) image build isn't working out of the box.

I started debugging over in this issue: https://github.com/guysoft/OctoPi/issues/823#issuecomment-2155646805

But rather than use that issue for rubber-duck-debugging, I'll use this issue to not pollute the OctoPi repo further, until I have more to report.

To debug, I just set up a brand new Pi 5 with a brand new Pi OS Bookworm 64-bit Lite install.

I ran apt update/upgrade, and installed the libcamera-apps suite: sudo apt install libcamera-apps -y. I rebooted, and verified I could see the camera:

pi@pistream:~ $ libcamera-still --list-cameras
Available cameras
-----------------
0 : imx708 [4608x2592 10-bit RGGB] (/base/axi/pcie@120000/rp1/i2c@88000/imx708@1a)
    Modes: 'SRGGB10_CSI2P' : 1536x864 [120.13 fps - (768, 432)/3072x1728 crop]
                             2304x1296 [56.03 fps - (0, 0)/4608x2592 crop]
                             4608x2592 [14.35 fps - (0, 0)/4608x2592 crop]

Then I tried using go2rtc to stream to my browser:

wget https://github.com/AlexxIT/go2rtc/releases/download/v1.9.3/go2rtc_linux_arm64
chmod +x go2rtc_linux_arm64

---

# Put the following inside go2rtc.yaml
streams:
  picam_h264: exec:rpicam-vid -t 0 --codec h264 -o -
  picam_mjpeg: exec:rpicam-vid -t 0 --codec mjpeg -o -

---

# Then run go2rtc:
./go2rtc_linux_arm64

In the browser, I opened http://10.0.2.204:1984 and opened one of the two streams. I end up with a window like:

Screenshot 2024-06-07 at 6 31 28 PM

or for mjpeg:

Screenshot 2024-06-07 at 6 31 42 PM
geerlingguy commented 3 weeks ago

I found this thread: https://github.com/AlexxIT/go2rtc/issues/881 — it recommends using:

streams:
  picam_h264: exec:rpicam-vid -t 0 --inline --libav-format h264 -o -
  picam_mjpeg: exec:rpicam-vid -t 0 --codec mjpeg --libav-format mpegts -o -

But there was no difference. Still got the same result as above. I'm running Safari... could there be a weird issue there? Edit: Nope, same thing in Chrome and Firefox.

geerlingguy commented 3 weeks ago

If I try opening the MJPEG stream (http://10.0.2.204:1984/api/stream.mjpeg?src=picam_mjpeg) directly from VLC, I get:

18:41:06.303 ERR [api.mjpeg] add consumer error="streams: exec/pipe: %!w(<nil>)\n[0:01:09.318561982] [940] \x1b[1;32m INFO \x1b[1;37mCamera \x1b[1;34mcamera_manager.cpp:284 \x1b[0mlibcamera v0.2.0+120-eb00c13d\n[0:01:09.328919040] [945] \x1b[1;32m INFO \x1b[1;37mRPI \x1b[1;34mpisp.cpp:695 \x1b[0mlibpisp version v1.0.5 999da5acb4f4 17-04-2024 (14:29:29)\n[0:01:09.434117733] [945] \x1b[1;32m INFO \x1b[1;37mRPI \x1b[1;34mpisp.cpp:1154 \x1b[0mRegistered camera /base/axi/pcie@120000/rp1/i2c@88000/imx708@1a to CFE device /dev/media0 and ISP device /dev/media1 using PiSP variant BCM2712_C0\nPreview window unavailable\n[0:01:09.43900..."

And formatted:

18:41:06.303 ERR [api.mjpeg] add consumer error="streams: exec/pipe: %!w(<nil>)
[0:01:09.318561982] [940] \x1b[1;32m INFO \x1b[1;37mCamera \x1b[1;34mcamera_manager.cpp:284 \x1b[0mlibcamera v0.2.0+120-eb00c13d
[0:01:09.328919040] [945] \x1b[1;32m INFO \x1b[1;37mRPI \x1b[1;34mpisp.cpp:695 \x1b[0mlibpisp version v1.0.5 999da5acb4f4 17-04-2024 (14:29:29)
[0:01:09.434117733] [945] \x1b[1;32m INFO \x1b[1;37mRPI \x1b[1;34mpisp.cpp:1154 \x1b[0mRegistered camera /base/axi/pcie@120000/rp1/i2c@88000/imx708@1a to CFE device /dev/media0 and ISP device /dev/media1 using PiSP variant BCM2712_C0
Preview window unavailable
[0:01:09.43900..."
geerlingguy commented 3 weeks ago

Testing on a fresh Pi OS 12 install with picamera2-WebUI-Lite instead:

# Install dependencies.
sudo apt install -y git python3-pip
sudo apt install -y python3-picamera2 --no-install-recommends

# Make Python not hate me.
sudo rm -rf /usr/lib/python3.11/EXTERNALLY-MANAGED

# Clone the project.
git clone https://github.com/monkeymademe/picamera2-WebUI-Lite.git
cd picamera2-WebUI-Lite
pip3 install Flask Pillow

# Run the app.
export PATH=$PATH:/home/pi/.local/bin
python app.py

And that works! Nice level of control (though the UI could be simplified and have like a 'beginner' mode with just a few focus, exposure, and WB controls).

Screenshot 2024-06-07 at 10 23 46 PM

It's only using about 30% CPU load too:

Screenshot 2024-06-07 at 10 25 33 PM

That's at 720p resolution. If I kick it up to 1080p, it jumps to 50-60% CPU. 1440p(ish) hits 80% CPU, and 4K gets stuttery and pushes 250% CPU.

glossyio commented 2 weeks ago

To debug, I just set up a brand new Pi 5 with a brand new Pi OS Bookworm 64-bit Lite install.

@geerlingguy - I had to use Install Raspberry Pi OS Bookworm (Full Install). Full Install required for all components to run properly. Lite is missing H.264 codec for libcamera.

geerlingguy commented 2 weeks ago

Lite is missing H.264 codec for libcamera.

Ah... do you know what package installs it?

glossyio commented 2 weeks ago

I think you'd have to build rpicam-apps source code, and rpi docs. I opted to do the Full Install vs. the build, so I couldn't help you beyond that.