DEV Community

Cover image for Fruits' Classification With Orangelib
Olafenwa Ayoola
Olafenwa Ayoola

Posted on • Edited on

Fruits' Classification With Orangelib

Classification problem is one of the major challenges in machine learning. Computer vision is an important aspect of classification problem in which objects recognized by a computer can be classified into specific categories.
Implementing computer vision can be challenging, such as training a model to perform accurate prediction on an object. Training a model to accurately recognize a specific object can be tasking and it becomes more tasking to implement a computer vision model that will be able to classify the difference between one object and another.

Orangelib:

Orangelib is a simple library created to facilitate easy implementation of computer vision in real problems. It is a library for classifying three categories of fruits:

-Ripe and Unripe Oranges
-Ripe and Unripe Bananas
-Green and Red Apples

We can easily classify these three categories of fruits using few lines of code.

Install Orangelib with:

pip install orangelib

Classification of Oranges with Orangelib

Orangelib library can easily classify ripe and unripe oranges using few lines of code.

Code for Single Image prediction

from orangelib.model import OrangeClassifier

classifier = OrangeClassifier("orange_model.h5")
fruit_name, prediction_confidence = classifier.predict("path_to_image")
print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)

Enter fullscreen mode Exit fullscreen mode

Let's look into each line of code:

from orangelib.model import OrangeClassifier
Enter fullscreen mode Exit fullscreen mode

The class Orangeclassifier is imported from orangelib to classify ripe and unripe oranges.

classifier = OrangeClassifier("orange_model.h5")
Enter fullscreen mode Exit fullscreen mode

We load the trained model for performing predictions on images. The trained model can be downloaded as a release from github.

fruit_name, prediction_confidence = classifier.predict("path_to_image")
Enter fullscreen mode Exit fullscreen mode

The path to the image to be predicted is loaded.

print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)

Enter fullscreen mode Exit fullscreen mode

We print out the fruit name and the level of prediction confidence.

We shall perform predictions on some images using the class OrangeClassifier.

sample1

sample1

We intend to classify this image above.

from orangelib.model import OrangeClassifier

classifier = OrangeClassifier("orange_model.h5")
fruit_name, prediction_confidence = classifier.predict("sample1.jpg")
print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)
Enter fullscreen mode Exit fullscreen mode

Output1

Fruit Name:  ripe orange
Prediction Confidence: 99.45345  
Enter fullscreen mode Exit fullscreen mode

The fruit name and the level of prediction confidence is printed.
It is able to detect that the orange is ripe with a high prediction confidence of over 99%.

Sample2

sample2

fruit_name, confidence = classifier.predict(sample2.jpg) 
Enter fullscreen mode Exit fullscreen mode

Output2:

Fruit Name:  unripe orange
Prediction Confidence:  99.92031
Enter fullscreen mode Exit fullscreen mode

It successfully predicted the image as an unripe orange with a prediction confidence of over 99%.

Implementing Multiple predictions on images with OrangeClassifier:

Sample3

sample3

We may not need to stress ourselves predicting a single image at a time especially when we need to predict many images. Imagine we want to predict about three images at once.

We make use of this code below for multiple predictions.

from orangelib.model import OrangeClassifier

classifier = OrangeClassifier("orange_model.h5")
fruit_names_list, confidence_list = classifier.predictBatch(["sample1.jpg","sample2.jpg","sample3.jpg"])
for fruit_names, confidence in zip(fruit_names_list,confidence_list):
    print("Fruit Name: ",fruit_name)
    print("Prediction Confidence: ", confidence)
Enter fullscreen mode Exit fullscreen mode

We import in the class OrangeClassifier and load the trained_model on oranges.

The major differences are:

fruit_names_list, confidence_list = classifier.predictBatch(["sample1.jpg","sample2.jpg","sample3.jpg"])
Enter fullscreen mode Exit fullscreen mode

We perform predictions on an array of images using the predictBatch function.

Note: There is no limit to the number of images that can be predicted with the predictBatch function.

for fruit_names, confidence in zip(fruit_names_list,confidence_list):
  print("Fruit Name: ",fruit_name)
  print("Prediction Confidence: ", confidence)
Enter fullscreen mode Exit fullscreen mode

We loop through the array of results of the predictions on the images, print out the name and prediction confidence for each of the images.

Outputs for the images:

Fruit Name:  ripe orange
Prediction Confidence: 99.45345
Fruit Name:  unripe orange
Prediction Confidence:  99.92031
Fruit Name:  ripe orange
Prediction Confidence: 99.99149
Enter fullscreen mode Exit fullscreen mode

It achieved accurate predictions on the three images. It was able to produce the same results we got for sample1 and sample2 when predicted individually.

Implementing Prediction On A Single Image With BananaClassifier:

The class BananaClassifier is used to classify ripe and unripe bananas.

Sample4.jpg

sample4

We intend to predict the kind of fruit in this image above.

from orangelib.model import BananaClassifier

classifier = BananaClassifier("banana_model.h5")
fruit_name, prediction_confidence = classifier.predict("sample4.jpg")
print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)
Enter fullscreen mode Exit fullscreen mode
from orangelib.model import BananaClassifier
Enter fullscreen mode Exit fullscreen mode

The class BananaClassifier is imported from Orangelib.

classifier = BananaClassifier(banana_model.h5)
Enter fullscreen mode Exit fullscreen mode

We make use of the trained model on Bananas. The model for predicting bananas can be downloaded as a release from github.

Output4:

Fruit Name:  ripe banana 
Prediction Confidence:  99.99490
Enter fullscreen mode Exit fullscreen mode

It successfully predicted the fruit as a ripe banana with a prediction confidence of over 99%.

Sample5

sample5

fruit_name, confidence = classifier.predict(sample5.jpg)
Enter fullscreen mode Exit fullscreen mode

Output5:

Fruit Name:  unripe banana 
Prediction Confidence:  99.99994
Enter fullscreen mode Exit fullscreen mode

It successfully predicted the image as an unripe banana with a prediction confidence of over 99%.

Implementing Multiple predictions on images with BananaClassifier:

Sample6

sample6

from orangelib.model import BananaClassifier

classifier = BananaClassifier("banana_model.h5")


fruit_names_list, confidence_list = classifier.predictBatch(["sample4.jpg","sample5.jpg","sample6.jpg"])

for fruit_names, confidence in zip(fruit_names_list,confidence_list):
    print("Fruit Name: ",fruit_names)
    print("Prediction Confidence: ", confidence)
Enter fullscreen mode Exit fullscreen mode

We import in the class BananaClassifier and load the trained_model on bananas.

fruit_names_list, confidence_list = classifier.predictBatch(["sample4.jpg","sample5.jpg","sample6.jpg"])
Enter fullscreen mode Exit fullscreen mode

We perform predictions on an array of images using the predictBatch function.

for fruit_names, confidence in zip(fruit_names_list,confidence_list):
    print("Fruit Name: ",fruit_names)
    print("Prediction Confidence: ", confidence)
Enter fullscreen mode Exit fullscreen mode

We loop through the array of results of the predictions on the images, print out the name and prediction confidence for each of the images.

Outputs for the images:

Fruit Name:  ripe banana 
Prediction Confidence:  99.99490 


Fruit Name:  unripe banana 
Prediction Confidence:  99.99994

Fruit Name:  ripe banana 
Prediction Confidence:  99.99983
Enter fullscreen mode Exit fullscreen mode

It achieved accurate predictions on the three images. It was able to produce the same results we got for sample5 and sample6 when predicted individually.

Implementing Prediction On A Single Image With AppleClassifier:

The class AppleClassifier is used to classify green and red apples.

Sample7

sample7

from orangelib.model import AppleClassifier

classifier = AppleClassifier("apple_model.h5")
fruit_name, prediction_confidence = classifier.predict("sample7.jpg")
print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)
Enter fullscreen mode Exit fullscreen mode
from orangelib.model import AppleClassifier
Enter fullscreen mode Exit fullscreen mode

The class AppleClassifier is imported from orangelib.

classifier = AppleClassifier(apple_model.h5)
Enter fullscreen mode Exit fullscreen mode

We make use of the trained model on Apples. The model for predicting apples can be downloaded as a release from github.

Output7:

Fruit Name:  green apple 
Prediction Confidence:  99.94303
Enter fullscreen mode Exit fullscreen mode

It successfully predicted sample7 as a green apple with over 99% prediction confidence.

Sample8

sample8

fruit_name, confidence = classifier.predict(sample8.jpg)
Enter fullscreen mode Exit fullscreen mode

Output8:

Fruit Name:  red apple  
Prediction Confidence:  100.0
Enter fullscreen mode Exit fullscreen mode

Wow! Successfully detected sample8 as a red apple with 100% accuracy.

Sample9

sample9

Implementing multiple predictions on images with AppleClassifier:

from orangelib.model import AppleClassifier

classifier = AppleClassifier("apple_model.h5")

#predict an array of images
fruit_names_list, confidence_list = classifier.predictBatch(["sample7.jpg","sample8.jpg","sample9.jpg"])

#loop over the array of results for each predictions
for fruit_names, confidence in zip(fruit_names_list,confidence_list):
    print("Fruit Name: ",fruit_names)
    print("Prediction Confidence: ", confidence)
Enter fullscreen mode Exit fullscreen mode

We import in the class AppleClassifier and load the trained_model on apples.

fruit_names_list, confidence_list = classifier.predictBatch(["sample7.jpg","sample8.jpg","sample9.jpg"])
Enter fullscreen mode Exit fullscreen mode

We perform predictions on an array of images using the predictBatch function.

for fruit_names, confidence in zip(fruit_names_list,confidence_list):
    print("Fruit Name: ",fruit_name)
    print("Prediction Confidence: ", confidence)
Enter fullscreen mode Exit fullscreen mode

We loop through the array of results of the predictions on the images, print out the name and prediction confidence for each of the images.

Outputs of predictions:

Fruit Name:  green apple 
Prediction Confidence:  99.94303 

Fruit Name:  red apple  
Prediction Confidence:  100.0

Fruit Name:  green apple 
Prediction Confidence:  99.21522 
Enter fullscreen mode Exit fullscreen mode

It achieved accurate predictions on the three images. It was able to produce the same results we got for sample7 and sample8 when predicted individually.

Excellent 👍 Results! with all the fruit classifiers available in Orangelib.

Install Orangelib and test it with as many images of oranges, bananas and apples as you desire.

Visit the official github repository of Orangelib.

Top comments (0)