Complete Guide: Installing and Building OpenCV with GPU for Python using VS Code and NVIDIA CUDA

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the new win in this computer vision tutorial in this video here i'm going to show you how we can install and build opencv with tpu support for python so in this video here we're going to focus on python and how we can set up python with opencv and build the source files from opencv with gpu support i already created another video with with cbos plus where we mainly focused on cbs plus like some of the same like installation and the build process and stuff like that is pretty much the same but we have some other different kind of tweaks and configurations for uh for python instead but when we're installing this python uh obviously gpu support here you can also use that for cpus plus but first of all we have to down the disco server i'll link to it down in the description here and you can join our community talk about computer vision deep learning and so on you can also become a member of the channel here if you want to support the channel more than you're currently doing by just watching with a small monthly fee and everything will go to create more ability quality content here on the channel so thank you guys so first of all here we have the obor what we're going to do in this video here so in the first couple of steps here we're going to do the installation of the opencv source files and opencv contrib folder here as well which i'm going to show you how we can download and how we can choose the exact version from opencv that we want to build the source files from and then we have to install visual studio with plus development tools because we're going to use that to actually like be able to build the binaries with opencv and cuda then we're going to install cuda and qdn uh dnn here and we're also going to install cmake but i already did this in another video so i'm going to link the video up here and you go check the installation process out and what we really need like what the prerequisites are to be able to like use cmake to configure what we're going to do in this uh video here where we're mainly going to focus on how we can configure opencv with gpu support for python in cmake so we just go down to step five here in this video here and if you want to know like how to install cmake cool that cool dnn and stuff like that i'll link to it down both in our description and also also put it put a link to it up here but in this way here we're going to configure opens that we would use gpus port in cmake for python so we have to choose like the interpreter that we want to use the python and in this video here we're going to use anaconda you can also use a based installation on your computer or laptop for python as well but in this video here i'm both going to show you how we can set up for a base a python uh environment and also for an anaconda environment which we can where we can use like a virtual environments and so on then we're going to build actual source files with cmake for opencv and and build them together with the uh cuda then we're going to set up and test opencv with gpu support in visual studio code for python here at the end of the video so first of all here we're just going to the github here for opencv where we can actually like download the source files for the specific omcb version that we want so up here we can just go into the text here actually and we can go into the tags here or like from the branches here then we can choose the version of opencv that we want and in this way here i'm going to focus on 4.5.2 which is your nearest release at this time i'm recording this video here so for opencv here which is the source files that we have in this folder here i'm just going to choose this version and we can see all the different kind of like source files we have here with both the includes and the different kind of modules that we have inside of opencv we also have some ecma configuration here that we're going to use later on with this with the cmake ui so to actually be able to install or like download the source file here we just go up to this code tab here and then we're just going to download the zip file here to our computer we also need the opencv contrib folder here which we're also we have to to choose the same exact um um version here of opencv that we're going to use so in this case here we're going to tags and we choose 4.5.2 because this is the version that we want to build almost we from so we just go up to the code tab here again and download the zip file so in this video here we're going to use anaconda for our python environment where we're actually going to build the source files with gpu support from as well with cmake so in this video here we're just going to download conde anaconda here from the website so we're just going to like the products and then the individual uh edition here i'll link to all of these different kind of links here for the download links down description so make sure to check those out and also if you want to go into more details on how to install visual studio uh with visual studio with siebel plus development tools and stuff like that make sure to check the other video out where i went over all the installation processes that we need for this video here as well first of all here we're just going to download anaconda here so we just go down here to the bottom and then we choose the operating system that we're on in this example here i'm going to install it on windows 10 and then we're going to download the python 3.8 environment here for or like the base environment here for anaconda and then i'm going to to choose the 64-bit version here graphical installer so i'm just going to hit this tab here and the download will now start so now the executable file here has downloaded to my computer so i can just hit to hit this tab down here and they will actually start installation process here for anaconda so directly installing conda here we need to choose some different kind of things so on this in this page here i'm just going to click next here and then we have the different kind of like license agreements here we just press agree and then we're installing it just for me here which is recommended uh we're not installing it for all users here and also requests like admin privileges so we're just going to hit next here and then we need to choose the destination folder where we actually want to download anaconda 2. so in this case here i'm just going to download it to my c directory into my user folder here and then i'm just going to specify this anaconda 3 here so this is where we're actually going to specify where we want to use the python interpreter and also the python like base environment when we're going to use cmake to actually like building the source files from opencv with gpu support later on so just hit next here and then when we come to here we need to add uh we need to add anaconda to my path environment variable so we don't have to add them ourselves because this is necessary when we're actually going to use them uh later on try to like import omsv with tpu support uh with anaconda so i'm going to check this out here so we add an account free to my path environment and variables and then we just hit install and it will now install anaconda based environment with python 3.8 here on my computer so now we're actually everything installed here to be able to actually like uh configure our and by build obviously from the source files so over here to left i just extracted the two zip files that we downloaded from the source files from uh from the opencv repository on github so here i have the the the source files here for opencv and i also have the contrib source files up here as well uh extracted search just go into one of them here which the source files from opencv we need to go over here and create a new directory or like a new folder where we actually want to have our build files so i just go into my local directory here and then to my users so i'm going to choose my own user directory here and then we're going to this user directory here and we create a new folder where we actually want to build the ohmsv source files here with gpu support so this is on this video here i'm just going to have opencv python i'll just call omcpython here in the previous video with uh with um with with the sibo floss we we did this is in this um obviously uh tutorial here but this way here we're going to do it in this opensv python here so i just go into this folder and then i'm just just going to copy this this the source files over here from opencv 4.52 and we're going to do the exact same thing with the contrib folders here as well i'm just going to drag this over here so we both have the opencv and the contrib and then we need to create a new folder here where we actually want to build our binaries into so i'm just going to call this a build in this example here so now we have actually everything installed where everything's set up here for the installation so we're now going to cmake and actually configure our our build and also then we're going to generate the build files and the binaries and then we can actually install omg with gpu support for python so we're now open up cma here where we're actually going to configure the different kind of stuff for the source files but first of all let's let's make sure here that our environmental variables is actually like set up correctly for the the anaconda environment that we're going to use so we're going to the environment variables here and then we just go into our path here and we can now see that this anaconda free here is actually like added to our environmental uh variables so this is all all this is set up here correctly and we can also see like the base installation of python on my computer here is python 3.8 if you want to just like install and build omsv with gpu support on our on our machine without using anaconda we can also use like a base installation of python on our computer or laptop so we just hit press ok here okay okay here because everything is set up correctly here so now we can just go in here specify where the source code is that we actually want to build so we're going to browse the source code here and we will go into our local disk and then we're going to find the directory where we just uh where we've just put in the the source files so we're just going to here opencv python and then we choose this opencv 4.52 which is the source file from opencv then we hit select folder and we now we need to specify where we actually want to build the binaries so this is the empty folder that we just created um that we just created so we're going to browse the building the build files here all the build folder and then we're going to chose this choose this build folder here which is empty right now and this is where we're going to build all the binaries by using or like doing these configuration here in cmake so we're going to select this folder here as well and now we can actually just hit configure here so we can get some different kind of options that we can do some configuration to our opencv build here in in cmake so first of all here we need to choose visual studios 1616 2019 here so this is why you need to install a visual studio here we just showed you in the siebel floss um video and then we're going to choose the platform here that we're using so in this case here we're using x64 and now we have actually set everything up here for our generator and we can now hit finis and it will configure our project so now the configuration here is done for the first time and we want we want to make sure that we're actually like using a python interpreter in this example here so we can see here that no python interpreter is now used here for the build we can see that we have this user anaconda free here but we don't have like the different kind of like libraries for python and also the interpreter that we're going to use so we need to make sure because this is one of the most common mistakes that that we do and one of the faults or like errors that we get when we're trying to build opencv here with gpu support for python because we need to specify what interpreter of python that we're going to use and if you scroll up here we can see that we have these different kind of like openc modules here and then we can see the unveilable here so we have some an unvariable here we can't use java we can't use python2 and we can't use python 3 because this is because we're using anaconda or like a base installation on our computer with python um we can't really use it right now and we just want to build and generate the source files here right now we wouldn't be able to run it in opencv and we'll get just get an error where we can't import a cv2 so first of all when we get this mistake here we need to open up an anaconda prompt so we're going to open up this anaconda prompt here and then we need to upgrade our non-pi version so we're just going to do it with this pip here and then we're going to have pip install and then we're going to upgrade upgrade numpy so we need to have the newest and newest version of numpy on our computer and we can see that the crime is already satisfied here so we're just successfully uninstalling and downloading the newest version of numpy in this example here so now we're gonna go up to this and search for python here so we're gonna actually like upgrade our non-pi version and we have actually reset the cma configuration here then we go in here and make sure that we actually like linking to the right uh directories here for anaconda and also python so we may need to make sure that we're pointing to this python executable file here in anaconda because we're using anaconda as the base environment here on a computer for python and not just a like a basic or like a standard based uh installation of python here on the computer so we're using anaconda which make sure that we have all these different kind of directories here linked so we both need to to point to the anaconda include directory and also the library file here for python 3.8 because anaconda on this base environment here is using uh 3.8 in in this example here so make sure we're linking to all like pointing to all these different kind of directories in this python3 tab here and when you have done that we can actually just go down here and hit configure again so if you get some errors and you can't really find the python 3 interpreter for your program make sure to upgrade the numpy um in in any kind of prompt so now we hit configure here again so now when the configuration is done we can now see that we have this python free tab here and if you just go up here first of all uh to the unviable cv opencv modules here we can now see the unreal modules that we have now is only java and python 2 so we you see we have now actually like an unavailable omg module for python 3 which was not the case before we upgraded numpy and we reset this cma configuration and we made sure that we are linking to all of these different kind of directories here in anaconda free so if we go down to the bottom here we can now see that we have this python 3 module here that we're going to configure our project or like build a binaries from the opencv source code with so we're going to specify the interpreter here so we're going to use the anaconda free interpreter of python and we can see the version number here as well and we can also see the numpy version here which is 1.20.3 so this is the newer version when i'm recording the video right now and this was the version that i upgraded my um my anaconda environment too and then we can also see the installation path here so where the where the installation path is from python three so now we have actually set everything up here to be able to build opencv with the use board for python and now we just need to specify some other different kind of configurations for how we can actually like build cuda into opencv where we're going to when we're actually going to install and build uh opencv with gpu support from uh from scratch so now we need to go up here after we have specified these different kind of directories for a python installation in anaconda then we also need to specify that we want to use cuba here in our installation so we go up here and and search for with underscore cuda we see here in this tab we have with cuda and we need to enable that over here to write in the value and we also need to have something called omcv omcv underscore cuda so we need to specify this build here so we just go in here it's not called opencv dnn and we go down here to the build and we need to make sure that this is checked out here as well we're going down to opencv here and then we need to specify that we want to use opencv dnn module with cuda so we can actually like use the neil network uh module in omcv with code as well and down here we don't need to specify anything here and then we also need to have something called uh opencv kuda which we just did and then we need the five maps here because we want to enable fat matte so we can actually like do really fast mathematical operations uh with our gpu later on so we need to enable that here as well before we're configuring here one more time then we also need the world here so we need to build opmcv world when we're actually configuring our source files here and then we also need to specify in here in the extras so where our actual like modules path is from our open opencv contrib file a folder so we need to go over into opencv contrib folder here and then we need to to point to this modules folder here because we're going to use these modules to actually like be able to build the binaries here from the source files so now we have done all these different kind of configurations we can now go down here and hit configure one more one more time so now one more time here the configuration is done we can see that we still have this python 3 interpreter here as well but if we scroll up here a bit we can actually see that we have now built a cuda and coo dnn into our opencv binaries here as well so we can now see here that we have nvidia cuda here and we can see the version so this thing in this video here we're using version 11.3 and then we also get the version down here with cool dnn and we can see that we're using this because we have this yes here and the version for crew dnn is 8.2.0 so now we have actually everything set up here and now after we have enabled five math we also need to like uh try to like build it into it when we're actually like uh generating our binaries later on so we need to go up here again and type for fast math and then we're going to enable or like we do need to check this out here uh cuda underscore fafma so in the last configuration we just enabled uh font math so we're going to check this value out here and then we need to go into the arc bin here uh augment here and we need to choose the version here for uh for our gpu so if you're going to the website here a website here we can just go into a cuda wikipedia website so i'll link to the website here in the description here as well but if you scroll down to the bottom here we get the different kind of versions for the gpus and this example here i'm on a gtx 1060 so my version is 6.1 here if you're using another version or like another gpu like for example at 20 70 or like 2060 you need to choose this version 7.5 here instead of instead of 6.1 which is the case in my example so if you just go back here to the cmg file here again we can just delete all of these things here instead except the version of our gpu so now we're actually like specified this thing and now we just need to specify some configuration for uh that we only want to use release mode when we're going to build uh the binaries here so now we have actually air set everything up here and we can configure the project or like the binaries here one more time before we actually going to generate them and install them here on the computer so now the configuration is done for the last time and we can just hit this generate button here and then it will generate all the different kind of like binaries inside of our build folder that we created at the start of the this video here like the empty build folder that we that we're pointing to up here and then they will actually have it in this install package inside of that build package that we can then get used later on to actually go into a terminal and then install and build the binaries here from opencv with gpu support so now generation of the binding binary files here is actually like done so now we're going to go in here and open up a new canoe kind of command prompt and then inside of this command prompt here i'm going to just type out a command here where we actually like going to to specify where i'll build files or like where our binaries are and some different kind of configuration or like uh installation and configuration that we need to do i'll link to this command here down the description so you can just go down there there and take it so we're going to use cmake here try to like build these binaries here and then we need to specify that we want to build them and then we need to specify the path where we actually have stored or like build these binaries too so we're just going to point to the to the directory with our build folder so in this example here it's inside my my user's directory here and then inside my own directory here i'm just going to type it out here and then we need to go into the opencv and then the folder that we call it what was opencv python and then we need to specify the build folder here as well so now we have actually specified the path where our build folder is where we actually like uh have uh like build the binaries out to so now we need to specify that we want to have this target so the target that we want so we want to actually want to install these binaries from the cma configurations and then we have this target here we need to to type this install and then we also need to specify the configuration which is release mode in this example here because we only specify that we want to use the release version when we did the configuration in cmake so we need to specify here and then we need to type release here at the end so now we actually have this command here and i'll link to it down the description so you can just copy paste it into the terminal here or like the command prompt and you can just hit enter and they will do everything for you so now i hit into here we can now see that we are starting the installation here of the binaries and now we can actually just wait here it takes up to one to two hours to actually like be able to build and install opencv here with gpu support for python on your computer you'll get a lot of different kind of errors while it's doing all these things here throughout like an hour or two just ignore these errors they will they will come and and and they're actually just warnings so we're now done installing omg here with gpu support and it took around like one to two hours so now we're going to the file explorer here where we can actually see and verify that we have actually built the binaries here from opencv so we're going to this pc here and then the local c drive and into the user directory then we can go into the opencv python folder here that we created and then inside the build folder here where we actually like have installed opencv with gpu support now so if we're going to build folder here uh we can see that all these files here have been downloaded and if we're going to the python loader here and oh into cv2 here this is the module that we're actually going to import when we're going to verify that we can use python with opencv here and gpu support so we can see we have this config here for the 3.8 version of python and we also have to load config here for python 3 as well so we can see that indeed we have installed the osv source files and we built them and we've installed it here in this folder so now we can just go in here and open up a new command prompt here with anaconda so if we're going to anaconda here and we just verify that we have this installation here so we're going to python here so we can activate a python environment here just in the base environment with anaconda and then we can import opencv here with import cv2 and then when i hit enter we can now see that import opencv here in in python and it is gpu supported so to verify that the gpu is bored we can go in here and then we can from cv2 here then we can actually like import the module cuda which is the module that we're going to use when we're going to use the different kind of methods and stuff like that from the gpu so from opencv 2 we can import cuda and then from kuda here we can actually use the different kind of methods so just to print out the device information about our gpu we're just going to call this cuda dot print cuda device info and then we're going to take the service device here because i only have one gpu on my computer so when we hit into here we're actually going to print out the information about the gpu that we have on our computer so now i hit into here and we get the different kind of information here or like the information from the cuda device that we have connected to our computer so we can see first of all here we get this device count here of one because i have one uh one one gpu here connected one nvidia gpu connected to my computer here and then we can also see some other information about the specific device that we actually have connected so in this case here i have a gtx 1060 here with six gigabytes of ram and then we also get some other different kind of information about here about the gpu so we both get like uh the total amount of global memory and we also get the cpu gpu clock speed here which is 1.8 gigahertz so we get all these different kind of information and we can just use all the different kind of functions within uh the kuder module that we can go in and find in the opencv documentation so now we can actually verify that we've installed and built this opencv source files with gpu support correctly so now i'm going to show you how we can actually set it up in visual studio code because we're just going to do the same thing so in here i just have a short sample program where we're going to do the exact same thing first of all here we're just going to import cv2 here cv2 here and then we from cv2 here we just import cuda as we just did and then we're going to write the same function here so from cuda here we're going to we're going to use the print cuda device info and we're going to take the save of the element here again so now we're just printing the exact same thing out i'm just i just want to show you like how we can set it up here in visual studio code so because we installed it in the in in the anaconda free and based environment we need to choose that interpreter here in visual studio code as well and that's the only thing we need to change to actually like be able to to use other ides with opencv with dbpo support so down here at the bottom we can actually like change the interpreter of of python here in the visual studio code so i can see i have both the base installation of python 3.95 here and i also have this base conde version up here which is three point eight point eight so i'm just going to choose this one here so render python program here we can actually see when we're when we're choosing this interpreter here and we run the program we will get the exact same information out here in the output here in visual studio code so you can use any of the ids you want you can just import cv and the different kind of modules from cuda or like uh from opencv the dnn module and then you can just use the different kind of functions you just need to choose the right interpreter of python that you have installed uh opencv with and to keep you support with so we can see down here we get the exact same information the device count um the specifications of the gpu the number of bytes gigabytes and stuff like that and then at the end here we can also see the cooler driver versions we have eleven point three point three so that's pretty much it for this video here guys we've been over like how we can uh build opencvs from the source files with gpu support here in python and i already created another video where we did it in cbs plus and went over all the installation steps for visual studio cmake and stuff like that so this would here we mainly focused on how we can set it up for python choose a different kind of interpreters where we used anaconda free in this example here and then also what errors it can cause when we're trying to do it in in python where we just had to upgrade the numpy uh the non-pi module to the newest version and then we can actually like do the configuration in cmake before real building the binaries and installing omsv gpu support here on a computer so thank you guys for watching this video here and wrapped it subscribe button notification under the video here and also like this video here if you like the content and you want more in the future it just really helps me and youtube channel out on a massive way i'm currently doing a computer vision tutorial and a deep learning tutorial where we're talking about like a basic uh about a basic stuff about like image processing techniques uh camera calibration stereo vision and stuff like that and in the deep learning tutorial we're talking about like neural networks the basic stuff about that how we can create neural networks from scratch training on a data set and then do predictions on data that i hadn't seen before so we're going to combine both computer vision and deep learning with cuda here and the gpu support with omcv and we're going to use the dnn module as well to do actual like neural networks inference together with opencv so i'll link to more tutorials up here or else i'll just see you next video guys bye for now
Info
Channel: Nicolai Nielsen
Views: 43,005
Rating: undefined out of 5
Keywords: opencv c++, opencv python, python opencv, opencv gpu, opencv gpu c++, opencv gpu python, gpu opencv, opencv cuda, opencv cuda c++, cuda gpu c++, opencv source code, build opencv source files, cmake configuration, cmake opencv, cmake opencv c++, cmake opencv gpu, opencv, NVIDIA, NVIDIA Cuda, NVIDIA cuDNN, The Coding Library, opencv nvidia, cuda opencv, cuda c++, cuDNN, install opencv, python, build opencv, build opencv python, install opencv python, cuda opencv python
Id: HsuKxjQhFU0
Channel Id: undefined
Length: 25min 47sec (1547 seconds)
Published: Sat May 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.