Raspberry Pi Picamera2 - Updated libraries using libcamera - demo with HQ camera microscope

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Raspberry Pi have updated the Raspberry Pi OS used on the singleboard computer one of the new features is that the Raspberry Pi camera libraries have now been updated now known as Pi camera 2. this removes the need for the proprietary drivers it now uses lib camera which is an open source camera Library which can be interfaced with lots of projects in this I'm going to provide an introduction to the new cameras Library using a Raspberry Pi 3 and an official HQ high quality camera module in my case the camera is attached to a microscope lens it will also be the same for the other Raspberry Pi cameras although it's the high quality camera that needs special consideration due to the memory usage which I'll explain later for this I'm using a fresh installed of the latest Raspberry Pi OS you can install this on older versions but that involves more steps particularly if you enabled Legacy camera mode I recommend a fresh install if possible I'm going to look at the example camera software how to overcome the problem with the contiguous memory allocator and how you can fix that I'll be using VNC for the screen captures note that unlike the pi camera one I haven't enabled the direct camera mode as the new library makes use of the standard X11 rather than writing direct to the screen if you think this is going to be useful please give it a like at any point during the video that will help to share this so others get to find out about the new library and to see my examples first I'm going to perform a quick test to make sure the camera is detected correctly now that to use the pi camera 2 The Legacy camera mode should be disabled you do need I squared C enabled you can see that through the Raspberry Pi configuration under interfaces just make sure that that's turned on it hopefully it's by default anyway and you can now just run a preview by running lib camera Dash hello from the terminal as you can see there's a short preview displayed so we know that the camera is connected and it's been recognized correctly I'm now going to download some of the example software it's available on a git repository so we can just use git clone and it's https github.com and Raspberry Pi camera two Dot and this is going to download it all into a directory called Pi camera two on the computer we can change to Pi camera two and have a quick look and you'll see there's an examples folder and there's various programs in here and one of the things we can do is we can do python preview.py and that will give us a preview there we go so it's running that okay now there's also a tools folder so if we go to tools in fact sorry there's the Naps folder there's there are some tools which is some tests and things but it's the apps folder is the one I wanted so this is going to give you an application that can be used to control the camera right and this is a problem that I said is something I'm going to look at is that this is due to the memory allocation so it failed to allocate buffers the reason isn't because I've already run out memory but it needs a single allocation of contiguous memory space so we're going to have a look at how we can fix that for this to work we need to change the CMA memory that stands for contiguous memory allocator on a Raspberry Pi with one gigabyte of memory or less this is set to 256 megabytes on the Raspberry Pi 4 with two gigabytes or more then it's usually set to 320 megabytes for this I'm going to set mine to 320 megabytes remember this is Raspberry Pi 3 so it has one gigabyte of memory if you have a pi with more memory then you could increase that further say to 384 megabytes or even 512 megabytes now to do this we're going to edit the boot.com I'll use fly you could use the Nano editor if you prefer so it's slash boot slash config.txt and then we're looking for a DT overlay with vc4 KMS V 3D so if I just do search for that which is the video driver and what we want to do is just edit this line and just add CMA minus 320 and you could replace that with say 384 or 512 as I suggested and for that to take effect just save that we now need to reboot now let's rebooted we should be able to try that again and get to Pi camera two and perhaps and that fall py and as you can see that's working now so this gives you access to various different options and allows you to capture this in an interactive way so we could just call this test jpeg and we'll just take a photo if we close that the photo was saved in the directory that we were in I didn't need to put the file extension on but there we go so that's captured that photo it's quite useful so you could also just use the command line there is an example on here which I can show you so Pi camera two examples and then capture jpeg will capture as ajk a JPEG file it's going to produce a preview first and then it will just capture the file and as you can see there's a file now called test dot jpg jpeg the problem with this is that if you run that again then it's just going to overwrite that test.jpg file you can see it's not created another file it's just ever written that so what you could do is rename that before you run the capture again one thing I like to do particularly using my microscope is to capture multiple images one after the other so I've created my own Python program based on these example codes to allow me to do that and I'll show that next so here's the code I've created I'll open it in the Sony IDE and then we could have a quick look through it so it's not huge it's just just shot 50 lines long which includes some spaces and the comments import some libraries we'll see those in use in a bit and I'm using gpio0 at this because I've got a button physically connected to the Raspberry Pi and this makes it a lot more convenient rather than having to get to a keyboard I can set my microscope up focus it press the button move anything else I need and just keep pressing the button to create the captions so I use button and I've connect the button to gpio pin number four request is just to track whether I requested a capture or to quit the application which we'll see in a bit sets up a preview and runs the preview and this is all contained in one big loop so it keeps running then there's another internal Loop which checks if the button has been pressed if so then it says capture and break or if the key the Key Queue is pressed which stands for quit followed by the enter key then it'll detect that or if any other key is pressed which includes just hitting the enter button then it will assume that's a capture request if the request is to quit the application then it just breaks out that Loop and closes but if not then it runs this capture it captures to a directory which I've created in advance called captures and calls it microscoped followed by a timestamp based around the year month day hour of minutes and seconds dot JPEG file and then it repeats so the significant parts are it uses gpio0 for the button press and this is the the part that does that and it uses select dot select for the key input Python's a little bit frustrating and doesn't have an option just to look at whether any Keys have been pressed without locking the code up so that's what this does it just looks to see if a key's been pressed and if so then it uses read line the only thing with this it does need you to hit enter key you can't just press q and quit straight away but it does the job that I wanted for here we can try that by clicking run now you see it brings up this preview I think now one thing you can do you can just press the button which is really what it's designed for as you can see the preview disappears takes the photo and then Returns the preview or you can use a keyboard to take another and you can just hit enter into this shell here and that works the one thing that doesn't work is the queue option if I try and do that then it doesn't work on here so to stop when running in funny just press stop however it does work correctly on the command line so if you go to have logic microscope so we've got that running got the preview again press the button press the button it captures it if I want to use the keyboard I do have to click onto here but then I can click enter and if I want to quit I just press q and enter and there's just an initial program just to show you how this can be used I may look at creating something a bit better perhaps using pi game or something like that so it gives a better interface than this but this is basically provided me what I need for now and if I look in the captures folder you can see it's captured each of those and it's got a date and the time stamp of when it was captured so that way it's not overwriting any files I hope you found this video useful did you see a hint of what my next video will be the microscope was pointing at a budget 2040 which I'll be using for programming the game if you haven't yet subscribed please click the Subscribe button and click the notification icon to find out about that and other maker videos coming in the future I look forward to seeing you on a future video
Info
Channel: Penguin Tutor
Views: 10,276
Rating: undefined out of 5
Keywords: raspberry, pi, raspberry pi, raspberrypi, camera, hq, picamera, picamera2, microscope, programming, python, gui, app, code, capture, button, gpio, zero, gpiozero, cma, contiguous, memory, allocator, snap, shot, trigger, photo, photograph, image, jpeg, jpg, png, graphics, gimp, vnc, i2c
Id: wryZ9NH4IwA
Channel Id: undefined
Length: 13min 47sec (827 seconds)
Published: Mon Sep 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.