How to Install TensorFlow 2 and OpenCV on a Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i've got a few requests asking for an all-in-one opencv and tensorflow installation video so that's what this video is just a reminder that you can leave comments with requests for new videos from me i try to read them all and respond to some of them and i really appreciate them i'm going gonna try to show every step all the way from setting up the raspberry pi all the way to finishing with a fully installed tensorflow and opencv here's the standard list of what you need for most raspberry pi projects but the most important one here is a newer pi it's going to make it so much easier if you have a pi 4 that's 64-bit but say you don't so you have a pi 3 32-bit or you have a pi 4 running a 32-bit os that's okay too i can make all those work some people with pi 3s do have trouble but i will provide a method that has worked for some people in the past starting with setting up your pie we're gonna put raspberry pi os on the raspberry pi and thankfully raspberry pi has an imager all built in so scroll down to this link you can find everything in the description this link time stamps commands it's all in the description and i have windows so download for windows but i already have it installed this is what it looks like choosing os i'm running a pi 4b so i'm going to do a 64-bit os and define that os other and then scroll down to 64-bit awesome storage let's plug in my micro sd card to my computer and now when i click choose storage that is my micro sd be careful you're not overwriting some important hard drive or something and before i click right let's click this little gear and this is the best part we can set our host name so we don't need to find the ip address of our raspberry pi when we enable ssh we can set up our username and password we can configure wi-fi ahead of time that's very handy set the locale settings and then also some persistent fun settings like play a sound when finished save that and then click right this takes about 10 minutes awesome plugger in boot up the pie i'm going to be using ssh if you're just using your pi as a desktop feel free to open a terminal and we'll be at the same point all right ssh into your pie however you're comfortable pie at raspberry pi this is the first time yes password okay so everyone should have a terminal window open first things first cat etc os dash release where are we okay you scrolled up a bit i have version 11 bullseye that's the newest version maybe you have version 10 buster but if you have anything older than that things might not work how they should so i'd recommend going back following that setup process that i just did so you can get a newer version of raspberry pi os speaking of getting newer versions with any good tutorial you should sudo apt update okay and then sudo apt upgrade yes that'll take a few minutes but after that's done let's keep moving check your python version python3 dash capital v i'm running python 3.9.2 and then let's also check another thing you name dash m so arch 64. you might also say arm v7 l let's remember this that's important these two things python 3.9 and then ar64 or whatever your output was why are those things important follow me to this website i'll have a link to it in the description and this is all our tensorflow shell scripts that we can we can use tensorflow doesn't really support the raspberry pi but there are plenty of privately hosted shell scripts and wheel files that make it work and now let's figure out how to read this so this is the tensorflow version cp is your python version so 3.9 and then this is whatever the output of youname-m was arch 64 or arm v7l and what we want to do is match our results to this version so i had python 3.9 so i can select this one and then i also had arch 64. so i can select this shell script i'm going to open that in a new tab but notice that all these newer versions of tensorflow do not have any arm v7l so if you're running that 32-bit operating system you're going to have to go back to tensorflow 2.5 and then there's another limitation it's just up to python 3.7 you might not have python 37 you might have python 3.9 if that's the case you're either going to one go back and reinstall the 64-bit version or two change your python version i'm going to show you how to change your python version to 3.7 and i'm just going to open up which one am i going to open up i want this one so for those of you who are going to change your python version to 3.7 or maybe you found a match but if you're following along with me and need to change your python version to 3.7 this is the one you're going to use and i'm also going to help you change your python version back in the raspberry pi for those of you who do not need to change your python version you don't need to do anything special you found the right shell command that matched these outputs skip forward to the next time stamp in this video for those of you who need to change your python version sit tight we're going to do that how are we going to do that we're going to do that with this thing called pi m so first let's get curl sudo apt get install curl all right looks like i already have it and so now we're going to install pi m curl https colon forward slash forward slash pi m dot run and pipe that to bash now we gotta edit our dot bash rc file so sudo nano squiggly dash dot bash rc now we're in the nano editor and we're gonna add some lines to the bottom of this i'm scrolling all the way down i'm just gonna page down okay enter enter and i'm just gonna copy and paste this text in you can find this in the description or just type it in from here then ctrl s to save control x to exit let's restart our shell exec dollar sign in all caps shell awesome and now we have pi m installed but we don't have any new python versions installed before we can do any of that we need to install some system packages i have a big list for you in the description copy and paste that now let's update pi m pi m update probably already up to date if you just installed it now we're gonna install python version so pi m install dash dash list and we can see every python version we can install we are looking for 3.7.12 that's what we want and then where's the terminal oh gosh okay then we're gonna run pi m install 3.7.12 enter this is going to take a little while i'll installed my 32-bit people or whoever is watching this section to change the python version so now we're going to make a project directory mkdir project change directories into project you can title this whatever you'd like and now how does pi m work if i do python dash version right now it's going to say 3.9.2 but if i do pi m local and then whatever version i just downloaded 3.7.12 that changes this project directories python version to 3.7.12 so now when i do python dash version i'm going to see 3.7.12 i'm going to cd back out of that and now hello everybody we should all be reunited now if you're changing your python version and watch that last section thanks for sticking with it if you skip forward hello again for those of you who haven't let's make a project directory mkdir project i already have one so i'm just gonna cd into project okay before i create my virtual environment i'm just gonna check my python version dash capital v and so i changed it in that last part just to show people what it was but i was going to use python 3.9 so i'm just gonna do pi m local dash dash unset nobody needs to run this command except me just just so you all know python dash v and now we have python 3.9 so i got that 64-bit python 3.9 combo for others if you followed that pi m section then you got the 32-bit python 3.7 combo you can do this with any combo that has an appropriate tensorflow wheel shell command install but those are the two that i'm just going to walk through with you today let's install the virtual environment package dash m pip install virtual m let's create a virtual environment python 3-m virtual m and now let's activate that virtual environment source m bin activate so we got that little m in parentheses saying we're in this python container and we're not going to contaminate any other packages with the things that we do here double check your python version i got python 3.9.2 with the 64-bit architecture and then you could also have python 3.7.12 if you followed along with the pi m tutorial and that'll work nicely with 32-bit architecture let's install a bunch more system packages i'm going to put the big list in the description i'm going to paste them here hit enter now we're ready to start installing tensorflow go back to that shell file you selected so in my case i selected the python 3.9 with the arch-64 but if you were doing the 32-bit then you should have the 3.7 or whatever matching python version and then arm v7 click this button right here raw copy the link and let's go back to the terminal let's go w get and then paste that link web git is what that stands for so now if we ls we see we have that download file right here with that shell command we want to turn that into an executable ch mod oop we need sudo chmod plus x and then that file enter so now we're letting it run we're saying this is safe to run this is okay and let's run that period forward slash and then down i'm going to push tab to autocomplete enter awesome all those files are downloaded if we ls here we can see we have a wheel file now and we want to install tensorflow using that wheel file before we do that let's uninstall any versions of tensorflow we might have uninstalled enter i don't have any installed so it's going to tell me that and now do the same thing without the sudo just to be safe awesome and now we're ready to install tensorflow pip install and then it's going to be that name of this whl wheel file so rather than copy and pasting the whole thing i'm just going to press tab it should auto complete and then enter boom let's try to see if it works python3 to get into a python3 interpreter import tensorflow as tf successful import so far tf dot underscore underscore version ho ho we got it installed some of you might get an odd error like hd f5 or something i've had this issue with 32-bit 4b with the python 3.7 version and all you got to do is you look at this circled part that i have here and then run this command okay and match the circle part to where i have desired version okay quit to get back to my terminal hopefully everyone's up to speed got tensorflow working and we're going to move on to opencv if you're using a raspberry pi 3 i'm going to say right away you can try my method but it might fail what i would recommend is you go to this video go all the way up to step 17 and after step 17 then hop on to the command i'm going to do right here it's really just one command pip install opencv dash python if you're using a 4 this is going to work great if you're using a 4 with 64 bit this is going to take a couple minutes 32-bit it takes me a couple hours i can't even start to explain how long it's going to take for the pi 3. usually it's a long time and note there is another one you could do so i have opencv python there's also opencv dash contrib dash python and this installs even more like you have more access to libraries and different customizations so you could use that if you'd like i'm not that advanced of a user for opencv so i'm just going to do opencv dash python enter it was like 10 maybe 20 seconds and then if you have a pi camera run pip install quotes pi camera square bracket array square bracket quote i do not so i'm not going to execute that command let's test our opencv installation import cv2 cv2 dot double underscore version double underscore and it works let's double check that we also have tensorflow still imports tensorflow as tf tf dot double underscore version quit there you go leave a comment if you got stuck at any spot leave a comment you have a request for a video and also if you found this helpful leave a like or subscribe a like or subscription lets me know that you found this valuable it's also encouraging and happy coding
Info
Channel: Sam Westby Tech
Views: 49,621
Rating: undefined out of 5
Keywords: raspberry pi, raspberry pi tutorials, raspberry pi 4, tensorflow 2, how to install tensorflow 2 on raspberry pi, how to install opencv on raspberry pi, computer vision tutorial, opencv and tensorflow on raspberry pi, easy install tensorflow, ai on raspberry pi, opencv
Id: vekblEk6UPc
Channel Id: undefined
Length: 17min 21sec (1041 seconds)
Published: Tue Mar 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.