TechieYan Technologies

Whether the fruit is Ripened or Not Using YOLOV5

Abstract

Fruit Ripening Project is although available on the internet and people also know how to distinguish between ripe fruit and unripe Fruit, but we have proposed a method to distinguish a ripe fruit and unripe fruit by making machine learn the attributes/features associated with the images that we are passing, the features can be Colour or Spots on the fruit or Shape etc. The model is been trained on Around 300 images of both ripe and unripe and before training the model, we have to label the images in specific software which then makes a .txt file for each image we label. These text and image files are then passed onto our model which trains accordingly and allows to make predictions.

Algorithm Description

  1. YOLOv5 is an abbreviation word for You look only once, where this algorithm has shaken most of the developers all around the world in the past 5 years, due to its immense ability to quickly learn and remember any object we have trained or labelled it. There is a unique method to work with yolo models where we require the images to be labelled firstly using specific tools available online or installing it in your local machine. (Labelling software’s links are provided below). the labelling process might take a long time depending upon on your dataset. The images are labelled according to the specification made by the user, whether we’d like to detect multiple objects in an image are single object, depending on it, the text file is created with following parameters,

    + The class of the objects, starting from 0,1, 2.

    + The coordinates of the bounding box drawn.

    Reference

    1. https://machinelearningknowledge.ai/introduction-to-yolov5-object-detection-with-tutorial/
    2. https://pytorch.org/hub/ultralytics_yolov5/
    3. https://github.com/ultralytics/yolov5

      using-yolov5

    Sample Label File

    The Label file consists of following items.

    + The class of the object

    + Top left coordinate of the bounding box

    + Top right coordinate of the bounding box

    + Bottom left coordinate of the bounding box

    + Bottom right coordinate of the bounding box

    face graph

How to Execute?

Make sure you have checked the add to path tick boxes while installing python, anaconda.

Refer to this link, if you are just starting and want to know how to install anaconda.

If you already have anaconda and want to check on how to create anaconda environment, refer to this article set up jupyter notebook. You can skip the article if you have knowledge of installing anaconda, setting up environment and installing requirements.txt

 
  1. Install the prerequisites/software’s required to execute the code from reading the above blog which is provided in the link above.
  2. Press windows key and type in anaconda prompt a terminal opens up.
  3. Before executing the code, we need to create a specific environment which allows us to install the required libraries necessary for our project.
  • Type conda create -name “env_name”, e.g.: conda create -name project_1
  • Type conda activate “env_name, e.g.: conda activate project_1
  1. Go to the directory where your requirement.txt file is present.
  2. cd <>. E.g., If my file is in d drive, then
  3. d:

command d

      7.cd d:\License-Plate-Recognitionmain    #CHANGE PATH AS PER YOUR PROJECT, THIS IS JUST AN EXAMPLE

command d license plate    

     8. If your project is in c drive, you can ignore step 5 and go with step 6

     9. g., cd C:\Users\Hi\License-Plate-Recognition-main

    10. CHANGE PATH AS PER YOUR PROJECT, THIS IS JUST AN EXAMPLE

    11. Run pip install -r requirements.txt or conda install requirements.txt (Requirements.txt is a text file consisting of all the necessary libraries  required for executing this python file. If it gives any error while installing libraries, you might need to install them individually.)

pipe installation

     12. To run .py file make sure you are in the anaconda terminal with the anaconda path being set as your executable file/folder is being saved. Then type python main.py in the terminal, before running open the main.py and make sure to change the path of the dataset.

     13. If you would like to run .ipynb file, Please follow the link to setup and open jupyter notebook, You will be redirected to the local server there you can select which ever .ipynb file you’d like to run and click on it and execute each cell one by one by pressing shift+enter. 

     14. Run main.py in your anaconda terminal and make sure to change the path where your executable files are located.

Please follow the above links on how to install and set up anaconda environment to execute files.

Labelling the images

If you guys ever feel like bored and want to spend your time for something good, you can sit idle and label images, which might really help people around you to do some crazy things. (Whatever I’m saying it is done in python)

Steps to label images.

  1. Download the labelimg.zip file from the below link provided.

https://github.com/tzutalin/labelImg

  1. Open your anaconda prompt and create a new environment in the location where you have unzipped your labelling files.
  2. conda install pyqt=5
  3. conda install -c anaconda lxml
  4. After that, past the following line of code in your terminal and press enter.
  5. pyrcc5 -o libs/resources.py resources.qrc
  6. Run python labelImg.py, it will open the window, where you can start labelling.
  7. Run python labelImg.py [image_path_folder] [pre_defined_class_file], here pre_defined_class_file represents the number of classes you need to your data, so you need to change it before starting to label your images, you can find that file in data folder.
  8. Make sure to change the format to yolo on the left-hand side, where by default it might be in pascal file format.
  9. Finally, click on create bounding box icon and start drawing and make sure to provide class to your bounding box every time you draw a box in an image.

How to run a custom YOLOv5 model in google colab.

  1. Click on the mentioned official GitHub repository of yolov5 and either fork it or open it in google colab or you can even run it in your local machine by downloading the files if you have a good system specification.

https://github.com/ultralytics/yolov5

  1. Scroll down and open the file in google colab.
  2. Make sure your labelled dataset is your drive and ready to be connected with the yolov5 model.
  3. Run the first cell and see the magic at the left-hand side of the colab screen.
  4. Click on data folder, double click on coco128.yaml file and replace the path of the dataset with your dataset files saved in the drive and also make sure to change the number of classes, the class names should be in **same order** as you have given while labelling.
  5. Locate down to training cell and alter the code accordingly, such as number of epochs depending on the accuracy you are getting and also batch size.

!python train.py –img 640 –batch 16 –epochs 3 –data coco128.yaml –weights yolov5s.pt –cache

  1. Finally, the model will be saved in weights folder, which will be created separate folder runsalong with the results and evaluation metrics.

Data Description

The YOLOV5 model has been trained by labelling several images of Banana, the 3 classes which we have labelled are Ripe, Unripe and Overripe, so we draw 1 or more bounding boxes depending on the number of bananas which we have in the image. A specific .txt file is created and saved along with the image automatically. These files are then loaded on top of our YOLOv5 model which has certain specific rules and steps to be followed to train and save our model. After saving we can load our model in different machine and use it for actual production or to test the model by passing in different images and video files.

Final Results

banana overripe result
banana unripe
banana ripe result

Issues you may face while executing the code

  1. Training a custom yolo model might be confusing and time taking.
  2. We might face an issue while installing specific libraries.
  3. Make sure you have the latest or specific version of python, since sometimes it might cause version mismatch.
  4. Adding path to environment variables in order to run python files and anaconda environment in code editor, specifically in any code editor.
  5. Make sure to change the path in the code where your dataset/model is saved.

Refer to the Below links to get more details on installing python and anaconda and how to configure it.

http://techieyantechnologies.com/2022/07/how-to-install-anaconda/

http://techieyantechnologies.com/2022/06/get-started-with-creating-new-environment-in-anaconda-configuring-jupyter-notebook-and-installing-libraries-using-requirements-txt-2/

Note:

All the required data has been provided over here. Please feel free to contact me for model weights and if you face any issues.

Click Here For The Source Code And Associated Files.

https://www.linkedin.com/in/abhinay-lingala-5a3ab7205/

 

Yes, you now have more knowledge than yesterday, Keep Going.

+91 7075575787