Raspberry Pi 4 PiCamera2 for Python3
To some people this information maybe old hat - if however, like me you've been a little busy for the past, well, the past "while" and have only just gotten back to your RPI4 projects, then this might be news to you - and if it is, I'll save you some "Googling"....
First of all, the RPi4 has been in short supply since the pandemic, so that was the first reason this has taken me so long to get around to, but alas I did indeed end up getting my RPi4 from pimoroni or was it thepihut?, either one is a great place to know about.
I purchased this little, well they call it a dog, but the head/ears make it look more like a cat to me, so I shall refer to it as a robot cat from now on.
https://github.com/Freenove/Freenove_Robot_Dog_Kit_for_Raspberry_Pi
I put it all together, connected all the servos, did all the wiring etc..etc... and then I did put an RPi3 on it I believe to initially set it up. I got the basics working from the github repo supplied python code. I even used a Windows 10 laptop to install/run the Server/Client code to remotely control the robot cat. But that was as far as I got.That version4 runs on RPi4, so maybe(?) it will have the same problem, as it is using ROS2 and you have the option to use Python3 with that.
So - what is the problem? Well, to use the camera all the docs tell you to use PiCamera library from Python3. Fair enough, this used to work absolutely fine and still will do - if you stick to the RPi4 OS 32-bit version. If however you install the RPi4 64-bit version, then the Python3 PiCamera will not work.
Little bit annoying when you've etup the robot cat, tested all the Python3 code and yep, everything works okay - except the camera.... can you live without using it? maybe... then 10 mins later, you're like me and say, "no, I need to have that working" - what do you then do?
Well, you then go into the rabbit-hole of stackoverflow links and dodgy forums wasting your time - or, you follow the steps below and all will be fine in 10 mins.
To summarise, if you used to use things like "raspistill" to capture an image from the camera or do some funky stuff with streaming, basically the library used for the camera has changed and these commands will no longer work.
Also, there is a lovely LONG page showing you all of the new commands for the new library - it is pretty awesome actually and something I intend to come back to and read in more detail later on.
Okay, that shows the low-level camera access changes for the RPi4, but we're using Python3, how do I get it to work with that?
Well, the Freenove sample code for camera.py looks like this:
and will throw you errors when you attempt to use PiCamera.
The solution, use PiCamera2! It is currently in BETA, but it works for basic things.
From that manual:
To install the PiCamera and dependencies, just do:
sudo apt install -y python3-pyqt5 python3-opengl
sudo apt install -y python3-picamera2
Then in your camera2.py code, insert the following code and run it - job done!
-------------------------------------------------------------------------------
from picamera2 import Picamera2, Preview
import time
picam2 = Picamera2()
camera_config = picam2.create_preview_configuration()
picam2.configure(camera_config)
picam2.start_preview(Preview.QTGL)
picam2.start()
time.sleep(2)
picam2.capture_file("test.jpg")
-------------------------------------------------------------------------------
That code will use the QT Library to show a preview in the UI (which is helpful) and then save the file.
As a bare minimum, you could just do the very basic:
-------------------------------------------------------------------------------
from picamera2 import Picamera2
picam2 = Picamera2()
picam2.start_and_capture_file("test.jpg")
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Right, where is that wander.py python code to make this robot cat autonomous?
....
oh, what was that very bad noise the batteries just made?.... oh, really? they've just started "screaming", is that due to over-charging? (sudo python test.py ADC was only show 5v) or are they just old now and need replacing - sigh! hopefully I can still get them online - they were a bit of a bugger to get last time..
oh, it never ends :-D
Comments
Post a Comment