Beginner Tutorial: How to Stream Video from Raspberry Pi Camera to Local Computer using Python (P3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's going on everyone welcome back to the channel and today's tutorial part three of the Raspberry Pi camera series I'll be showing you how to stream video from your Raspberry Pi camera to your local computer in real time as you could see here with this stream I can go to on my local computer and view updates from my Raspberry Pi camera now for those you guys who have been following this series I showed how to do this exact same thing in part one however I showed how to do it with a Raspberry Pi official camera that fits into the Rasberry Pi slot of your Raspberry Pi instead in this video we'll be doing the exact same thing but we'll be using a USB based camera instead and this is per request for some of my viewers in the comment section they wanted to see this because the implementation is a little different to support USB based cameras so we'll be using the pi camera 2 Library as opposed to Pi camera one or zero and so I'll be walking you step byep on the code level and on the package level of how to set that up and this is the camera I'm using for your reference it is the ru cam 180p so it is on Amazon as you can see really cheap relatively to other cameras on the market and it is a USB based camera so you can see it does plug into the USB port on the Rasberry Pi so that being said if you are using a USB based camera and you want to use the pi camera 2 library to stream video in real time this is the video for you enough being said let's get started okay so the first step is we just want to sign into our Raspberry Pi and have some way of interacting with our Raspberry Pi to install the packages and write code right now I am using visual studio code from my local computer to SSH and write code and do terminal commands on my Raspberry Pi I have a YouTube series where I show you guys how to do this really you could just sign into your Raspberry Pi and do everything I'm showing you here I just prefer using visual studio code on my remote desktop to be able to code on the Raspberry p so imagine you are signed into your re P you just want to open a terminal I am in Visual Studio code and you could just go to new terminal if you are on your Raspberry Pi you could just open a terminal on the top left and it should be equivalent and once you are in that terminal we just want to install the system level packages we need to be able to run the code I'm about to show you to create that video stream so first things first we just want to update the system level installer on the Raspberry Pi so we could just go to pseudo apt update and this is always a good practice before you start any project on the Raspberry Pi to make sure you're up to date with your appt which is the Raspberry Pi system level installer for packages so we can see where you already are and we just want to install three packages so if you already followed part one you probably have one of these packages which is flask but we also want to install open CV which will allow us to do some bite level operations easily on the images so we can send bytes over the video stream to our local computer and we also want to use the pi camera to library which is obviously the most important Library here that will allow us to create that camera object to connect to our USB based camera and actually take continuous images to send over the video video stream so now that we have this command ready to go we just go ahead and click enter I already ran this command so it should be quick and if you guys still are having issues when you run the code that I show you in this video after in terms of the libraries you can actually do other methods to install these packages you can actually pip install these packages so if you if you are having issues let me know in the comment section down below I'll be more than happy to try to guide you there to let you know that you can do other means such as pip installing or other methods to actually get these libraries installed so now that we have those libraries installed the next thing you want to do is we just want to create a python file on your Raspberry Pi so you see I already have the python file already created it's called test UCam dopy and let me just zoom out a tiny bit so you guys can see this code at a higher level so it's really really simple code just like the first video essentially does the same thing so what we're doing in this code is we are creating a flask app so flask is just a a framework in Python that allows us to create servers really simply within a few lines of code so I'm not going to get into flask in detail in this video but we're we already talked about it in part one to some degree now piie camera 2 of course is the latest version of the pi camera library that is supported by the Raspberry Pi community so this is really important because as opposed to the pi camera Library we showed in part one that's going to be deprecated so we want to learn how to use Pi camera 2 so we just import that here as you can see really simply and we have the CV2 now CB2 is a a very in-depth Library you can do a lot of magic with that Library we're just using CV2 to Simply do some encoding on the image and convert it to bytes and send it over that that server endpoint to our local computer so that's all we're using CV2 for is to convert essentially the information from the image so we can create it in a format to send over to our local computer for our local computer to be able to interpret the format of the image so that's all you're going to have to know about CV2 in this video there is a lot you can do with that live we're not going to get into it in this tutorial so next we also create the flask app as you can see after doing all those Imports so this is just how you initialize a flask app and then we just want to create that camera object very simple and just configure the camera properties so this is essentially what we're telling how the the dimensions of the frame you want to see in other formats of the camera you can do a lot with this ardu cam I believe it has some night vision I think and some other properties you can play around with this and read more about this on online so I'm just going to zoom in a little bit just so you guys could see that so that is create preview configuration so you're just essentially messing with the configuration of the image on the screen that you guys saw at the beginning of this video then once we have that we just go ahead and we start the camera very simple and then we Define this generate frames function so what this generate frames function is doing it's just capturing an image from the camera converting it converting it to a format we can stream over the the URL to our local computer in doing some HTTP response format here once again we're not going to get into that just know this allows us to stream over that URL to our local computer so we have to format it like this and then we just to find an endpoint in our flask app so this is the endpoint we're going to go to on our Raspberry Pi or on our local computer I should say when we want to view the video stream on our local computer and what that endpoint does does it simply generates a response that we're going to get from the the flask app and that response contains this generate frames so it calls this generate frames function and just passes it as a mime multiart so we have to pass in it like that so we can interpret it in our local computer and render it on the screen and finally once that is all said and done and you have the code ready you could just go ahead and run this app on the port 5000 in the host 0.0.0.0 so in order to get this actually be viewable on your local computer on your Raspberry Pi in this video you actually have to be on the same local network as your Raspberry Pi as well so make sure your raspberry piie and your LC computer are both connected to the same Wi-Fi for this to be able to work I did show in part two how to be able to view video streams that aren't on your local network so you can go ahead and watch that I'll link that right here and once everything is said and done you have the libraries and you have this code ready and copied I'll have the code code in the description down below so you can just click that link and go view the code if you don't want to copy you can just go ahead and run this code so really as simple as that really simple code and we could just go ahead and run this code by typing in Python 3 or if you are logged into your Raspberry Pi you could just click the play button andth or whatever IDE you're using and we could just say Python 3 test ram. and let's just go ahead and run that so if we did everything properly here we should see it up and running so let's go ahead and close this so it is running and then we just want to see how that looks on our local computer all right so now that we have our flask app up and running we can just go to a browser on our local computer and just type in the IP address followed by colon Port 5000 and Then followed byv feed to actually go to the stream and our flask app if you guys don't know how to get this value right here you could just go to the terminal on your Raspberry Pi I already showed this in part one but I'll show it again just in case you haven't watched part one so you can just go to a terminal type in if conf big and then you just want to get this value right here so mine will probably be different than yours this is just the IP address of your Raspberry Pi on your local network you can just go ahead and copy that in and substitute it for this value here so once we have that we can just click enter and we could see if you did everything correctly you will see the video feed in real time which is awesome so looks like we got it to work so pat yourself on the back if you got it to work as well and if we go ahead and move the camera we'll show you how responsive it is and overall just a really crisp and robust video feed as opposed to part one where I think this one is a little better than the pi camera or the Raspberry Pi camera video feed so I think this camera is a little more expensive and just a little better than the first one so yeah looks like we got it to work if you guys did please consider donating to the channel in the link down below because I know uh time is money so if I did save you time and you got this to work please consider donating if not consider like comment and subscribe to the channel because that means a lot to me we just hit 2K subscribers on the channel so I just want to thank you guys a lot that is a big milestone for me so hopefully 5K and 10K to come within the next year or so with your continuous support if you guys are having any issues with this video at all whether it's the libraries or anything let me know in the comments section down below my subscribers have been very useful and responding to some of my fellow subscribers before I have in detail so we're developing a really nice community on this YouTube channel so you can just go ahead and ask questions and a lot of people will respond including myself to try and help you as best as we can if you guys want to see more videos with this camera series I'll be more than glad to do that because I'm really having a blast working with these Raspberry Pi cameras and I'm just having a good time this is part three hopefully we'll make a part four if you guys want to see something in fact this video itself came from a suggestion on my comments so I take comments very highly and readily from you guys and a lot of my videos are inspired from the comments so enough being said I do not want to take any more of your time stay tuned thanks for watching and I will see you on the next video everyone I love you all [Music]
Info
Channel: Shilleh
Views: 2,348
Rating: undefined out of 5
Keywords: Raspberry Pi Camera, Flask, Python Flask, Video Streaming, Backend Development, Raspberry Pi Camera Flask, Picamera tutorial, pip, python3, DIY Raspberry Pi, Raspberry Pi Beginner Tutorial, Raspberry Pi 4b, Arducam, Picamera2, Opencv, Apt, USB Camera Raspberry Pi, Video Stream Flask
Id: NOAY1aaVPAw
Channel Id: undefined
Length: 10min 40sec (640 seconds)
Published: Sat Mar 30 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.