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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's going on everyone welcome back to the channel in today's quick tutorial I'll be showing you how to stream video from your Raspberry Pi straight to your local computer the example we'll be going over is this right here as you can see on the screen I'm simply viewing a live video feed from my Raspberry Pi that is streaming this video from a camera connected to the raspberry pi over the local Network and I could view it in Chrome as you can see I connected here and I can move my camera around and you can see it's pretty responsive in real time which is nice and overall this is a nice simple project for beginners to do because it's very simple on the software level and it's very simple on the hardware level all you'll need is a Raspberry Pi in a camera I will link those down in the description below and I'll be walking you through step by step in this video of how to do this so I do not want to waste any of your time before we get into it be sure to like comment subscribe and even better consider donating in the Donate link down below and let's let's get started okay so in order to get started the first thing obviously you need is the physical setup I'm using this camera here you can see it's really simple to set up because the rasperry pi itself has a camera slot built for these cameras so once you get the camera just be sure to lift the sides there of the slots insert the camera in the correct orientation and there's push the slots down and therefore your camera is connected physically and of course you do need a power supply but that's really all you need in terms of the physical setup so once you have that let's jump into the Raspberry Pi side of things I am going to assume already have the Raspberry Pi operating system installed it is the most basic thing you need to actually do anything on the Raspberry Pi I believe the operating system as of the making of this video is called raspian correct me if I'm wrong just make sure you have raspian installed as well and let's jump into the Raspberry Pi side of things to show you what libraries you need and what to do in the python code okay so now that you have your physical setup the next thing you want to do is simply log into your Raspberry Pi and we want to do some simple commands in the in the Shell or The Terminal to to uh install some packages and do some other brief setup that we have to do in order to get this to work so the first thing you want to do simply is you want to run pseudo apt update and for those of you guys who don't know apt is just the the package manager on this operating system so we are just updating it just so we have the latest packages when we go to pseudo app install or that sort of thing so that's just good practice to typically do that when you are working on a new project next thing you want to do is you want to enable the camera on the Raspberry Pi so it doesn't come enabled straight out of the box when you turn on the Raspberry Pi you actually have to enable it so in order to enable the camera you just want to type in pseudo raspy config okay and it's going to take you to the screen here and then you just simply want to go to interface options and then you want to go to Legacy camera and then you want to enable Legacy camera okay so you want to you want to select yes so I'm just going to do that okay so it's saying it's deprecated that's fine and then we just want to hit the Escape so we can escape this screen so we've escaped but the next thing you want to do is after you've enabled the camera you actually have to reboot your Raspberry Pi now I already enabled this I'm not going to reboot it but you can simply go here you can click log out and then there's an option to reboot so you can just go ahead and do that and that should enable your camera to connect to your Raspberry Pi and next thing we want to do is simply install the python packages the third party packages we need for the python script we're going to run in flask uh so flask is essentially Python's uh server framework because we are going to run a server in Python on our Raspberry Pi that will service information from our client in this case our local computer once we hit the URL in that server so we want to pip install flask and we also want to pip install Pi camera which is the library that allows us to do very easy camera operations some people have Pip three so if you have Pip 3 you want to type in PIP 3 install but I have Pip so I'm just going to type in PIP install and that just depends on how you set up your pip another thing you would want to do if you never installed pip before is you would actually want to pseudo appt install pip so pip is just the the python package manager so you just want to go type in pseudo appt install this is if you never used pip before most likely if you're watching this video that's a high chance you have if not you can simply install it with this operating system package manager and just install pip and you should be good to go to start using pip to install uh thirdparty python packages so that will come in handy if you are working on python a lot on this Raspberry Pi which I would imagine is a high chance you are now the last thing we have to do in the setup in this terminal is we simply want to get the IP address to to hit once we actually have the server running so we could just type in if config and that gives us a bunch of stuff here that looks like uh random numbers but really the only number we need here is this inet value so yours will probably be slightly different so you could just copy that value and save it for later we're not going to use it yet we're going to use this value on the the local computer and the URL once we want to access the server from our local computer so now you have the setup and we're going to jump into the python script to show you how to get that running okay so now that we're done with the system level thing we just want to create a python script and and run the script I'm going to show you here so we're just going to open th to use as our editor and I already have the script created so we're just going to open Tani here and if you haven't ever created a python script in t you could just go up here click plus new and create the script and name it whatever you like with a with a piy extension in this case I just named it stream. piy you can name it however you like as long as the code you have is the same as the code here so as you can see this is really simple code just 24 lines you know with some spaces here and simply what we're doing in this code is we have some imports here so we're using IO which is a standard python library to do some bite level operations and we also have the piie camera as we talked about just to do some simple tasks with the camera in this case uh capture photos with the camera so very easily we can use that package and the most important one here is flask so if you are going to be using python a lot as a server side framework flask is probably the most popular package in python or one of the most popular packages to do that and simply what I'm doing with flask is I am creating a flask app so that's how you initialize a flask app very simple and in this flask app simply what we're going to have is one uh URL extension which is the SL video feed and so when the user goes to this URL video feed what it's going to do it's going to return a response object so that is from The Flash library and that response object what it's going to do it's going to call this generate frames function which we defined above and it's going to define the MIM type so this pretty much tells the browser essentially how to read the data you're sending or how to interpret it so we're not going to get too much into mind types or what they are but we're calling this generate frames function which is which is the meat of the code here and we're just going to use this piie camera library to Simply Define some properties of the image we want to take in that frame in this case 640 by 480 but you can play around with those I would imagine the the more resolution want the slower it's going to be and of course same thing with the frame rate so we just have pretty standard stuff here nothing too fancy uh overall maybe a little low quality but it's fine as you saw in the beginning of the video it's still pretty crisp and then we're just going to create the stream object which we're going to continuously use to uh manage the the bites coming from uh the camera so the imag is taken from the camera and so at a high level what this what this for Loop is doing it's simply uh continuously processing those frames from the camera and sending them over that stream continuously in jpeg formats and that's really all it's doing here so continuously seek zero because it has to wait till it processes the full frame and then it starts from the beginning of the new frame and that's pretty much all it's doing so as long as you are on that video feed for slurl you are going to get a continuous stream of of jpeg frames which is what you want which allows you to get that continuous uh monitoring as you saw in the browser at the beginning of this video so that's all we're doing and then we have this if name equals main so that means if you just run this file it's going to run this app.run so it's going to host on 0.0.0.0 which is all publicly available IP addresses are accessible in that case and that's pretty much all you have to know at a high level what the script is doing so it's just creating this endpoint that you can hit and it's going to continuously send a frames of images using the library served on this flask application so I'm just going to to go ahead and run this so you could just run it once you get that code in here so very simple server it might be as simple as it gets to just one end point literally but you could do much more with flask so now that we have this running let's go back to our local computer hit that endpoint and show you one more time that it is indeed working okay so now that you have your flask app up and running and be sure by the way that your Raspberry Pi was connected to the internet and your local computer is also connected to the same network because in this video we're only showing you how to do this on the same local network so be sure the Raspberry Pi is connected to the internet and so is your computer and so once we have that we could just open any browser we want I'm just going to open Chrome and simply what you want to type in is the IP address followed by colon the the port that it's running on which is 5,000 and then for SLV video feed and that's really all we have to do so we should start getting that so you see it's black because the camera is facing down but really I could just move the camera around you can see point at my work desk and it is generally pretty responsive it's not the cleanest video but overall I think it can actually be used as a pretty decent security camera in real time time to see maybe what's going on in in your yard or that sort of thing so that's really as simple as it is guys I hope it was a cool DIY project for you beginners watching this video in the next video what we'll be doing is we'll be adding on to what we just saw here and we'll create an app that's actually viewable from any network not just the local network so I'll be showing you how to do that in the next video so stay tuned for that and be sure to like comment subscribe to the channel and even better consider donating to the channel in the link down below let me know if you have any questions as well in the comment section stay tuned thanks for watching and take it easy [Music] everyone
Info
Channel: Shilleh
Views: 16,781
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
Id: qs3KhLDUBmk
Channel Id: undefined
Length: 10min 40sec (640 seconds)
Published: Wed Jan 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.