Perform Object Detection Using YOLOv5 From Scratch In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome in this video we will perform object detection using yellow V5 which is developed by ultralight X in 2020. if the yellow is something new for you then let me tell you it is a very popular object detection algorithm which can detect the object from the images and videos the reason of its popularity is its high accuracy and speed in this video we will only focus on the YOLO V5 implementation and we will write the code from scratch in order to have some understanding what is actually going on but before starting there are some requirements let me show you this is the main repository of YOLO V5 provided by ultralatics and if I scroll down here it is mentioned that the required version of python is 3.7 or higher so make sure that you are using the correct version of python and if you are not sure then you can go to the code editor and there you can import this module and then print says dot version and just run the code and this will tell you the version of python you are using here you can see I am using python 3.10 after this we need to install the required packages for that we will go to the yellow V5 main Repository and here is a file requirement.txt just click on it and there you will find all the required packages to install them we will click on RAW button and then copy the URL after this we will come back to the code editor and in the terminal we will write pip install Dash R and then paste the URL and press enter just to save the time I have already installed these packages now once the required packages are installed then we can start writing the code first of all I will import the pi torch module and then we need opencv module after this we will load the yellow V5 model so for that I will write here model equals to rch dot hub dot load and then I will provide the repository from where the model will be downloaded so for that here I will go back and this is the Repository I will copy it and then I will paste it here now the next argument is the YOLO model which you want to use so let me show you here if I scroll down here you can find a number of yellow V5 models available the larger the model you will use more accurate it will be but also more time it will take to detect the objects because I don't have GPU so I will use this small model so here I will write yellow v5s and after loading the model we can run the inference so for that I need an image and this is the image which I have to read it I will write IMG equals to CV2 dot I am read and then I will provide the name of the file and after reading the image we can run the inference so for that I will write result equals to model and then I will give the image and let's print the result to see what we are getting now I will run the code right now because there is no model in the project directory so the first time we run the program it will be automatically downloaded from the repository which we have provided and here you can see our program is downloading the model it will take some time and once the model is downloaded then we can run the inference now the model is downloaded and here you can see the result it seems like a person and a car has been detected but this is not the complete information to get the complete information I will convert the result into pandas data frame so here I will write DF equals to result dot pandas and then I will write x y x y and then in bracket I will write 0. now let's print it and see what we are getting now I will run the code and here you can see we are getting our data frame and there are number of columns here x minimum and Y minimum are the top left corner of the bonding box and then we have X Max and Y Max which is the bottom right corner of the bonding box then we have confidence and then the label now we need to extract all of the information from this data frame so for that I will use a for Loop and I will write for end in DF dot index this will provide me the index of intro and using this index I will extract all the information to get the top left corner I will write X1 comma y1 equals to DF and then I will provide the column name which is X minimum for X1 and then I will give the index then for y1 I will write DF and the column for y1 is y minimum and then I will give the index in the similar way I will find the bottom right corner so here I will write X2 comma Y 2 equals to DF and the column for X2 is X Max and then I will give index and then I will write DF and this time the column is y Max and then again I will give index and after this we have confidence but just for now leave it and we will extract the label so for that I will write label equals to DF and the column is name and then again we will give index now after this I can put all of this information on the image and then display it so to draw the bonding box here I will use rectangle function from opencv and then I will provide image then I will provide the top left corner which is X1 comma y1 and this is basically the coordinate and this will be integer but here you can see it is not the integer so we need to convert it into integer otherwise this will give an error now the third argument is the bottom right corner which is X2 comma Y2 and then I will define the color of the rectangle and then the thickness now to put the label I will use put text function and here I will give image and then the label and then I will give the location then I will specify the font then font scale and then I will give the color and then the thickness now after this I will come out of for Loop and here I will use I'm sure method to display the image and then I will use weight key function and waiting time will be zero now if everything is fine then the code should work so let's run it and see if this is working okay the image is too big so I need to resize it so here I will write IMG equals to cv2.3 size then I will provide the image and then the size which I want now I will run the code and here you can see our model has detected a car and a person even if the person is not clear so I can say it is working very well now let's try it on a video file I will just comment this length of code and here to load the video I will write cap equals to CV2 dot video capture and then I will provide the name of the video file and after this to play it I will use while loop and then I will write IMG equals to cap dot rate and then I will give the index which is one after this I will check whether I get the image or not so I will use if statement and if the image is none then we will simply break the loop otherwise we can display the image on the screen so here I will use I'm sure method and then I will give the name to window and then image and then I will use the weight key function as usual and let's run the code and see okay this is the video file now let's run the inference on it for running the inference the procedure will remain same so here I will write result equals to model and then I will give the image and then I need to convert it into Data frame so for this I will copy this line and I will paste it here then I need to extract all the information so from here I will copy this for Loop and then I will paste it here and remember we did not use the confidence value before so let's use it this time I will write here conf equals to DF and the column is confidence and then I will give index then I will write text equals to label and then I will concatenate it with confidence and here I will replace the label with text now let's run the code okay I need to do one more thing here I will round up the confidence value up to two decimal place now I will run the code again and here you can see the result it is detecting the objects but it seems to be very slow so let's try a smaller model which is Yola V5 and I will run the code again now because this model is not in our project directory so it will be downloaded from the main Repository so it will take some time and here you can see the result it is faster than the previous one and it is also doing well now at this point you need to know that running the inference from Euler V5 does not only require the patent weights but also some other modules which we haven't added in the project directory so every time we run the program they are accessed from the YOLO V5 main repository this means while running the program we need an internet connection if you want to do it offline then you need to download the repository so for this we will go to this Repository and here you will click on code and then you can download the zip file from here and extract it into project directory otherwise we can copy this link and then we will come back to the code editor and then in the terminal we will write get clone and then we will give the URL and this will clone the repository in our project directory it will take some time so I will just pause the recording and when it is done we will continue from there okay now the repository has been cloned and here you can see a folder named as yellow V5 now in order to run it offline I will provide this folder name instead of Repository and then we need to give another argument which is Source equals to local this will tell the program that you need to assess all the required modules from this folder not from the main Repository now if I run the program then it should work so let's try it and here we go it is working so this is how we can perform the object detection using Euler V5 you can see how easy it is that's all for now thanks for watching
Info
Channel: AI Search
Views: 7,959
Rating: undefined out of 5
Keywords: Object detection using yolov5, Object detection with yolov5, Object detection using in python, Object detection with yolov5 using pytorch
Id: iQGS73aDWzE
Channel Id: undefined
Length: 16min 38sec (998 seconds)
Published: Sat Sep 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.